public static OneDCodeData GetOneDCode(Mat img, int x, int y, int w, int h, int s1, int s2) { img = img.Clone() * (s2 / 1000); img = img.CvtColor(ColorConversionCodes.BGR2GRAY); img = img.Threshold(s1, 255, ThresholdTypes.Binary); OneDCodeData codedata = new OneDCodeData(); var code = img.SubMat(y, y + h, x, x + w); code = code.Resize(new OpenCvSharp.Size(code.Width * 0.4, code.Height * 0.4)); string[] data = BarcodeScanner.Scan(OpenCvSharp.Extensions.BitmapConverter.ToBitmap(code), BarCodeType.Code128); if (data.Length != 0) { codedata.code = data[0]; } codedata.code = codedata.code.Replace("-", ""); codedata.code_img = code; codedata.full_img = img; return(codedata); }
public void Explain(Mat img) { if (img == null) { return; } tobjs.Clear(); is_inspection_ok.Clear(); is_ulever_ok.Clear(); is_dlever_ok.Clear(); is_tube_ok.Clear(); is_color_ok.Clear(); string[] colorstr = { "错色", "黄色", "蓝色", "无色" }; List <Mat> retimgs = new List <Mat>(); if (bloodParameter.is_onedcode) { orcnumber = Tools.GetOrcNumber(img, bloodParameter.orcnumber_x, bloodParameter.orcnumber_y, bloodParameter.orcnumber_w, bloodParameter.orcnumber_h, bloodParameter.orcnumber_s1, bloodParameter.onedcode_s2, ID); } var imgs = Tools.MyMatchTemplate(img, new Rect(bloodParameter.tut_x, bloodParameter.tut_y, bloodParameter.tut_w, bloodParameter.tut_h), "template.jpg", 0.4f, bloodParameter.tube_y); List <OpenCvSharp.Rect> arect_list = new List <Rect>(); is_iocn = false; var aimgs = Tools.MyMatchTemplate(img, new Rect(bloodParameter.at_x, bloodParameter.at_y, bloodParameter.at_w, bloodParameter.at_h), "a.jpg", 0.5f, 0, arect_list); if (aimgs.Count == 0) { aimgs = Tools.MyMatchTemplate(img, new Rect(bloodParameter.icon_x, bloodParameter.icon_y, bloodParameter.icon_w, bloodParameter.icon_h), "icon.jpg", 0.5f, 0); is_iocn = true; } if (bloodParameter.is_onedcode) { one_d_code_data = Tools.GetOneDCode(img, bloodParameter.onedcode_x, bloodParameter.onedcode_y, bloodParameter.onedcode_w, bloodParameter.onedcode_h, bloodParameter.onedcode_s1, bloodParameter.onedcode_s2); } if (imgs.Count < 8) { Console.WriteLine("模板未检测:" + imgs.Count); return; } var color = Tools.ColorSort(imgs, imgs[0].Width / 2 - 25, imgs[0].Height / 2 - 25, 50, 50, bloodParameter.yhmin, bloodParameter.yhmax, bloodParameter.ysmin, bloodParameter.ysmax, bloodParameter.yvmin, bloodParameter.yvmax, bloodParameter.bhmin, bloodParameter.bhmax, bloodParameter.bsmin, bloodParameter.bsmax, bloodParameter.bvmin, bloodParameter.bvmax, bloodParameter.lnhmin, bloodParameter.lnhmax, bloodParameter.lnsmin, bloodParameter.lnsmax, bloodParameter.lnvmin, bloodParameter.lnvmax); foreach (var item in imgs) { Cv2.Resize(item, item, new OpenCvSharp.Size(64, 128)); Cv2.CvtColor(item, item, ColorConversionCodes.BGR2GRAY); } foreach (var item in aimgs) { Cv2.CvtColor(item, item, ColorConversionCodes.BGR2GRAY); } var liquilevel = new List <List <int> >(); var foregrects = new List <List <InspectionRect> >(); upliqcv = 0; downliqcv = 0; upliqmaxcv = 0; downliqmaxcv = 0; is_ulevercv_ok = true; is_dlevercv_ok = true; is_ulevermaxcv_ok = true; is_dlevermaxcv_ok = true; if (bloodParameter.is_uinspection || bloodParameter.is_dinspection) { liquilevel = Tools.LiquidLevel(imgs, bloodParameter.lever_step, 0, 0, imgs[0].Width, (int)(imgs[0].Height * 0.7f)); upliqcv = Tools.BianYiXiShu(liquilevel, 0); upliqmaxcv = Tools.Extremal(liquilevel, 0); downliqcv = Tools.BianYiXiShu(liquilevel, 1); downliqmaxcv = Tools.Extremal(liquilevel, 1); is_ulevercv_ok = upliqcv <= bloodParameter.lever_ucv; is_dlevercv_ok = downliqcv <= bloodParameter.lever_dcv; is_ulevermaxcv_ok = upliqmaxcv <= bloodParameter.lever_maxucv; is_dlevermaxcv_ok = downliqmaxcv <= bloodParameter.lever_maxdcv; } else { for (int i = 0; i < 8; i++) { liquilevel.Add(new List <int>()); liquilevel[i].Add(0); liquilevel[i].Add(0); } } if (bloodParameter.is_inspection) { foregrects = Tools.ForeignInspection(imgs, liquilevel, ref retimgs, bloodParameter.inspection_count, bloodParameter.inspection_minarea, ID); } else { for (int i = 0; i < 8; i++) { foregrects.Add(new List <InspectionRect>()); } } List <int> space = new List <int>(); if (bloodParameter.is_space) { space = Tools.LabelSpace(img, bloodParameter.label_x, bloodParameter.label_y, bloodParameter.label_w, bloodParameter.label_h, bloodParameter.label_s1, bloodParameter.label_s2); } if (space.Count != 0) { space1 = space[0]; space2 = space[1]; space3 = space[2]; space4 = space[3]; space5 = space[4]; space6 = space[5]; } for (int i = 0; i < imgs.Count; i++) { tobjs.Add(new BloodObjs()); var tube = tobjs[tobjs.Count - 1]; if (bloodParameter.is_show_space) { Mat notimgs = new Mat(); Mat nimg = new Mat(); Cv2.BitwiseNot(retimgs[i], notimgs); Cv2.BitwiseNot(imgs[i], nimg, notimgs); tube.img = nimg; } else { tube.img = imgs[i]; } tube.miny = liquilevel[i][0]; tube.maxy = liquilevel[i][1]; tube.foreigs = foregrects[i]; tube.color = (int)color[i][0]; tube.color_h = color[i][1]; tube.color_s = color[i][2]; tube.color_v = color[i][3]; tube.infomation = colorstr[tube.color] + "\nh:" + String.Format("{0:F}", tube.color_h) + "\ns:" + String.Format("{0:F}", tube.color_s) + "\nv:" + String.Format("{0:F}", tube.color_v); } for (int i = 0; i < aimgs.Count; i++) { tobjs.Add(new BloodObjs()); var tube = tobjs[tobjs.Count - 1]; tube.img = aimgs[i]; } //标签 is_space_ok = bloodParameter.is_space == false || aimgs.Count != 0 && (space1 >= bloodParameter.label_minw && space1 <= bloodParameter.label_maxw && space2 >= bloodParameter.label_minw && space2 <= bloodParameter.label_maxw); //标签是否反转 is_labelfilp_ok = bloodParameter.is_labelfilp == false || is_iocn ? tobjs[0].color != 2 : tobjs[0].color == 2; //一维码判断 is_onedcode_ok = bloodParameter.is_onedcode == false || is_iocn || orcnumber.numberstr == one_d_code_data.code; //杂质判断 for (int i = 0; i < foregrects.Count; i++) { is_inspection_ok.Add(foregrects[i].Count == 0); } //上清液 for (int i = 0; i < liquilevel.Count; i++) { is_ulever_ok.Add(liquilevel[i][0] >= bloodParameter.lever_umin && liquilevel[i][0] <= bloodParameter.lever_umax); } //下清液 for (int i = 0; i < liquilevel.Count; i++) { is_dlever_ok.Add(liquilevel[i][1] >= bloodParameter.lever_dmin && liquilevel[i][1] <= bloodParameter.lever_dmax); } //颜色 int[] tube_colors = { bloodParameter.tbc1, bloodParameter.tbc2, bloodParameter.tbc3, bloodParameter.tbc4, bloodParameter.tbc5, bloodParameter.tbc6, bloodParameter.tbc7, bloodParameter.tbc8 }; for (int i = 0; i < tube_colors.Count(); i++) { is_color_ok.Add(bloodParameter.is_color == false || tobjs[i].color == tube_colors[i]); } //综合判断 for (int i = 0; i < imgs.Count; i++) { is_tube_ok.Add((bloodParameter.is_inspection == false || is_inspection_ok[i]) && (bloodParameter.is_uinspection == false || (is_ulever_ok[i] && is_ulevercv_ok && is_ulevermaxcv_ok)) && (bloodParameter.is_dinspection == false || (is_dlever_ok[i] && is_dlevercv_ok && is_dlevermaxcv_ok)) && (bloodParameter.is_color == false || is_color_ok[i])); } //综合判断 is_all_ok = is_space_ok && is_labelfilp_ok && is_onedcode_ok; for (int i = 0; i < is_tube_ok.Count; i++) { is_all_ok = is_all_ok && is_tube_ok[i]; } }