//杂质检测 public static List <List <InspectionRect> > ForeignInspection(List <Mat> imgs, List <List <int> > liquilevel, ref List <Mat> retimgs, int inspection_count, int inspection_minarea, int netindex) { List <List <InspectionRect> > rectlist = new List <List <InspectionRect> >(); Mat mask = GetTimplateImage("mask.jpg"); Cv2.Resize(mask, mask, new OpenCvSharp.Size(64, 128)); foreach (var item in imgs) { PyTorchSDK.AddImage(Unet[netindex], item.Data); } PyTorchSDK.Test(Unet[netindex], false); var data = PyTorchSDK.GetOutPut(Unet[netindex]); int index = 0; while (data != IntPtr.Zero) { Mat img_tem = new Mat(128, 64, MatType.CV_8U, data); //要为奇数//膨胀 //Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(1, 2), new OpenCvSharp.Point(0, 0)); //Cv2.Dilate(img_tem, img_tem, se, new OpenCvSharp.Point(-1, -1), 1); img_tem = img_tem.Threshold(inspection_count, 255, ThresholdTypes.Binary); retimgs.Add(img_tem.Clone()); OpenCvSharp.Point[][] contours; HierarchyIndex[] hierarchyIndexes; Mat img_masked = new Mat(); img_tem.CopyTo(img_masked, mask); img_masked.FindContours(out contours, out hierarchyIndexes, RetrievalModes.CComp, ContourApproximationModes.ApproxSimple); rectlist.Add(new List <InspectionRect>()); for (int i = 0; i < contours.Length; i++) { var rect = Cv2.BoundingRect(contours[i]); if (rect.Width * rect.Height > inspection_minarea && (rect.Y > liquilevel[index][0] || rect.Y + rect.Height > liquilevel[index][0])) { rectlist[rectlist.Count - 1].Add(new InspectionRect(rect, rect.Width * rect.Height, rect.Width * rect.Height)); } } data = PyTorchSDK.GetOutPut(Unet[netindex]); index++; } return(rectlist); }
public static string CheckOrc(List <Mat> imgs, int index) { string orc_str = ""; if (imgs.Count != 0) { foreach (var img in imgs) { PyTorchSDK.AddImage(Ocrnet[index], img.Data); } PyTorchSDK.Test(Ocrnet[index], true); var data = PyTorchSDK.GetOutPutInt(Ocrnet[index]); while (data != -1) { orc_str += data; data = PyTorchSDK.GetOutPutInt(Ocrnet[index]); } } return(orc_str); }