public override VisionResult Detected(VisionImage image, Dictionary <string, VisionResult> Result = null, VisionFlow parent = null, Shape newRoi = null) { VisionResult rtn = new VisionResult(); rtn.State = VisionResultState.WaitCal; try { using (HImage hImage = VisionHelper.Image(image)) { var roi = this.ROI; if (newRoi != null) { roi = newRoi; } RectangleContour rect = roi as RectangleContour; using (HImage reduceImage = hImage.ReduceDomain( new HRegion(rect.Top, rect.Left, rect.Top + rect.Height, rect.Left + rect.Width))) { HRegion hRegion = new HRegion(); hRegion = reduceImage.Threshold(this.MinThreshold, this.MaxThreshold); rtn.Area = hRegion.Area.D; this.AddVisionResc(rtn, $"面积侦测成功 面积值:{rtn.Area:N3}"); rtn.State = VisionResultState.OK; } } //using (VisionImage imageMask = new VisionImage(ImageType.U8, 7)) //{ // using (VisionImage image2Process = new VisionImage(ImageType.U8, 7)) // { // //Algorithms.Copy(image, image2Process); // Algorithms.Threshold(image, image2Process, new Range(this.Threshold, 255), true, 255); // PixelValue fillValue = new PixelValue(255); // Range intervalRange = new Range(0, 0); // var roi = this.ROI; // if (newRoi != null) // roi = newRoi; // Algorithms.RoiToMask(imageMask, roi.ConvertToRoi(), fillValue, image2Process); // HistogramReport a = new HistogramReport(); // a = Algorithms.Histogram(image2Process, 256, intervalRange, imageMask); // image2Process?.Dispose(); // imageMask?.Dispose(); // if (a.Histogram.Count >= 256) // { // if (CheckWhite) // rtn.Area = a.Histogram[255];//白色区域面积 // else // rtn.Area = a.Histogram[0];//黑色区域面积 // } // this.AddVisionResc(rtn, $"面积:{rtn.Area}"); // rtn.State = VisionResultState.OK; // } //} } catch (VisionException ex) { this.AddVisionResc(rtn, ex.Message); rtn.State = VisionResultState.NG; } return(rtn); }
public override VisionResult Detected(VisionImage image, Dictionary <string, VisionResult> Result = null, VisionFlow parent = null, Shape newRoi = null) { VisionResult rtn = new VisionResult(); rtn.State = VisionResultState.WaitCal; try { lock (locked) { using (VisionImage temp = new VisionImage()) { var roi = this.ROI; if (newRoi != null) { roi = newRoi; } using (HImage hImage = VisionHelper.Image(image)) { RectangleContour rect = roi as RectangleContour; using (HImage reduceImage = hImage.ReduceDomain( new HRegion(rect.Top, rect.Left, rect.Top + rect.Height, rect.Left + rect.Width))) { HTuple numLevels = new HTuple(); numLevels.TupleAdd(this.MaxNumLevels); numLevels.TupleAdd(this.MinNumLevels); HTuple row, col, scaleX, scaleY, score, angle; reduceImage.FindAnisoShapeModel(this.ShapeModel, this.StartAngle, this.EndAngle - this.StartAngle, this.YMinScale, this.YMaxScale, this.XMinScale, this.XMaxScale, this.MinScore, 1, this.MaxOverlap, this.Pixel.ToString(), 0, this.Greediness, out row, out col, out angle, out scaleY, out scaleX, out score); if (score.Length > 0) { rtn.State = VisionResultState.OK; image.Overlays.Default.AddLine (new LineContour(new PointContour(col.D - 50, row.D), new PointContour(col.D + 50, row.D)), Rgb32Value.RedColor); image.Overlays.Default.AddLine (new LineContour(new PointContour(col.D, row.D - 50), new PointContour(col.D, row.D + 50)), Rgb32Value.RedColor); rtn.Point = new PointContour(col.D, row.D); rtn.Angle = angle.TupleDeg().D; this.AddVisionResc(rtn, $"轮廓匹配成功 角度:{rtn.Angle:N3} 分数:{score[0].D:N3}"); } else { rtn.State = VisionResultState.NG; this.AddVisionResc(rtn, $"没有找到匹配模板"); } } } } } } catch (HalconException ex) { this.AddVisionResc(rtn, $"轮廓匹配失败 {ex.GetErrorMessage()}"); } return(rtn); }
public override VisionResult Detected(VisionImage image, Dictionary <string, VisionResult> Result = null, VisionFlow parent = null, Shape newRoi = null) { VisionResult rtn = new VisionResult(); rtn.State = VisionResultState.WaitCal; if (ROI == null || image == null) { rtn.State = VisionResultState.NG; return(rtn); } try { using (VisionImage temp = new VisionImage()) { var roi = this.ROI; if (newRoi != null) { roi = newRoi; } using (HImage hImage = VisionHelper.Image(image)) { RectangleContour rect = roi as RectangleContour; using (HImage reduceImage = hImage.ReduceDomain( new HRegion(rect.Top, rect.Left, rect.Top + rect.Height, rect.Left + rect.Width))) { string code = string.Empty; switch (CodeType) { case CodeType.Code_2D_Mat: code = "Data Matrix ECC 200"; break; case CodeType.Code_QR: code = "QR Code"; break; } using (HDataCode2D code2D = new HDataCode2D(code, "default_parameters", "maximum_recognition")) { HTuple result = new HTuple(); HTuple resultString = new HTuple(); code2D.FindDataCode2d(reduceImage, "stop_after_result_num", 1, out result, out resultString); if (resultString.Length > 0) { rtn.BarCode = resultString.SArr[0]; this.AddVisionResc(rtn, $"条码寻找成功:{rtn.BarCode}"); rtn.State = VisionResultState.OK; image.Overlays.Default.AddText(rtn.BarCode, new PointContour(rect.Left, rect.Top), Rgb32Value.BlueColor, new OverlayTextOptions("Consolas", 128)); } else { rtn.State = VisionResultState.NG; this.AddVisionResc(rtn, $"没有找到条码"); } } } } } } catch (HalconException ex) { this.AddVisionResc(rtn, $"没有找到条码 {ex.GetErrorMessage()}"); } return(rtn); }