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
            {
                if (parent.Detects.Count > 0 && Result != null)
                {
                    if (Result.ContainsKey(this.Line1ID) &&
                        Result.ContainsKey(this.Line2ID))
                    {
                        rtn.Point = Algorithms.FindIntersectionPoint(Result[this.Line1ID].Line, Result[this.Line2ID].Line);
                        image.Overlays.Default.AddPoint(rtn.Point, Rgb32Value.RedColor, new PointSymbol(PointSymbolType.Cross));
                        this.AddVisionResc(rtn, $"焦点({rtn.Point.X:n2},{rtn.Point.Y:n2})");
                        rtn.State = VisionResultState.OK;
                    }
                }
                else
                {
                    rtn.State = VisionResultState.NG;
                }
            }
            catch (Exception 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;
            if (parent != null && Result != null)
            {
                if (Result.ContainsKey(this.StartPt) && Result.ContainsKey(this.EndPt))
                {
                    rtn.Line = new LineContour(Result[this.StartPt].Point, Result[this.EndPt].Point);
                    image.Overlays.Default.AddLine(rtn.Line, Rgb32Value.GreenColor);
                    rtn.Angle = Common.MathHelper.GetAngle(rtn.Line.Start.X, rtn.Line.Start.Y, rtn.Line.End.X, rtn.Line.End.Y);
                }

                this.AddVisionResc(rtn, "拟合直线成功");
                rtn.State = VisionResultState.OK;
            }
            else
            {
                this.AddVisionResc(rtn, "拟合直线失败");
                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();

            try
            {
                var roi = this.ROI.ConvertToRoi();
                if (parent != null && Result != null && !string.IsNullOrEmpty(parent.OrgCrood) && Result.ContainsKey(parent.OrgCrood))
                {
                    CoordinateSystem old      = new CoordinateSystem(parent.OrgPoint, parent.BaseAngle);
                    CoordinateSystem newCrood = new CoordinateSystem(Result[parent.OrgCrood].Point, Result[parent.OrgCrood].Angle);
                    Algorithms.TransformRoi(roi, new CoordinateTransform(old, newCrood));
                }

                var op2 = (this.OptionList[0] as DetectEdgeOptions).Options;
                var op1 = (this.OptionList[1] as DetectStraightEdgeOptions).Options;

                StraightEdgeReport report = Algorithms.StraightEdge2(image, roi, this.SearchDir, op2, op1, this.OptimizedMode);

                image.Overlays.Default.AddRectangle(roi.GetBoundingRectangle(), Rgb32Value.BlueColor);
                if (report.StraightEdges.Count > 0)
                {
                    rtn.Line = report.StraightEdges[0].StraightEdge;
                    image.Overlays.Default.AddLine(rtn.Line, Rgb32Value.YellowColor);
                    rtn.Angle = Common.MathHelper.GetAngle(rtn.Line.Start.X, rtn.Line.Start.Y, rtn.Line.End.X, rtn.Line.End.Y);

                    if (this.SearchDir == SearchDirection.LeftToRight || this.SearchDir == SearchDirection.RightToLeft)
                    {
                        if (rtn.Angle > 0)
                        {
                            rtn.Angle = 90 - rtn.Angle;
                        }
                        else if (rtn.Angle < 0)
                        {
                            rtn.Angle = 90 + rtn.Angle;
                        }
                    }
                    else
                    {
                        rtn.Angle = -rtn.Angle;
                    }

                    this.AddVisionResc(rtn, $"侦测直线成功 角度:{rtn.Angle}");
                    rtn.State = VisionResultState.OK;
                }
                else
                {
                    this.AddVisionResc(rtn, "侦测直线失败");
                    rtn.State = VisionResultState.NG;
                }
            }
            catch (Exception ex)
            {
                this.AddVisionResc(rtn, ex.Message);
                rtn.State = VisionResultState.NG;
            }

            return(rtn);
        }
Exemplo n.º 4
0
        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
            {
                if (parent != null && image != null && Result != null)
                {
                    Collection <PointContour> pt = new Collection <PointContour>();

                    if (CrossList.Count >= 3)
                    {
                        for (int i = 0; i < CrossList.Count; ++i)
                        {
                            if (Result.ContainsKey(this.CrossList[i]))
                            {
                                pt.Add(Result[this.CrossList[i]].Point);
                            }
                            else
                            {
                                return(rtn);
                            }
                        }

                        FitCircleReport report = Algorithms.FitCircle(pt);
                        rtn.Point = report.Center;
                        this.AddVisionResc(rtn, "拟合成功");
                        image.Overlays.Default.AddOval(report.Circle, Rgb32Value.RedColor);
                        image.Overlays.Default.AddPoint(rtn.Point, Rgb32Value.RedColor);
                        image.Overlays.Default.AddRoi(report.Circle.ConvertToRoi());
                        rtn.State = VisionResultState.OK;
                    }
                    else
                    {
                        this.AddVisionResc(rtn, "拟合点数据少于3");
                        rtn.State = VisionResultState.NG;
                    }
                }
                else
                {
                    throw new Exception("没有找到对应结果");
                }
            }
            catch (Exception ex)
            {
                this.AddVisionResc(rtn, ex.Message);
                rtn.State = VisionResultState.NG;
            }

            return(rtn);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 计算结果
        /// </summary>
        /// <returns></returns>
        public VisionResult Detect(VisionImage image, Shape InitRoi = null, int textOffset = 0)
        {
            VisionResult rtn = new VisionResult();

            rtn.State = VisionResultState.WaitCal;

            Dictionary <string, VisionResult> Result = new Dictionary <string, VisionResult>();

            foreach (Detect func in this.Detects)
            {
                var result = func.Detected(image, Result, this, InitRoi);
                Result.Add(func.UnitID, result);
                rtn.VisionDesr.AddRange(result.VisionDesr);
                if (result.State == VisionResultState.NG)
                {
                    rtn.State = VisionResultState.NG;
                    return(rtn);
                }
            }

            OverlayTextOptions TEXT = new OverlayTextOptions();

            TEXT.FontName = "Consolas";
            TEXT.FontSize = 128;
            if (!string.IsNullOrEmpty(this.ResultX) && Result.ContainsKey(this.ResultX))
            {
                rtn.Point   = new PointContour();
                rtn.Point.X = Result[this.ResultX].Point.X;
                image.Overlays.Default.AddText($"X:{rtn.Point.X:N2}", new PointContour(50 + textOffset, 100), Rgb32Value.BlueColor, TEXT);
            }

            if (!string.IsNullOrEmpty(this.ResultX) && Result.ContainsKey(this.ResultY))
            {
                rtn.Point.Y = Result[this.ResultY].Point.Y;
                image.Overlays.Default.AddText($"Y:{rtn.Point.Y:N2}", new PointContour(50 + textOffset, 180), Rgb32Value.BlueColor, TEXT);
            }

            if (!string.IsNullOrEmpty(this.Angle) && Result.ContainsKey(this.Angle))
            {
                rtn.Angle = Result[this.Angle].Angle;
                image.Overlays.Default.AddText($"Angle:{rtn.Angle:N2}", new PointContour(50 + textOffset, 260), Rgb32Value.BlueColor, TEXT);
            }

            if (!string.IsNullOrEmpty(this.Area) && Result.ContainsKey(this.Area))
            {
                rtn.Area = Result[this.ResultX].Area;
                image.Overlays.Default.AddText($"Area:{rtn.Area:N2}", new PointContour(50 + textOffset, 340), Rgb32Value.BlueColor, TEXT);
            }

            rtn.State = VisionResultState.OK;
            return(rtn);
        }
Exemplo n.º 6
0
        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
            {
                var op1 = (this.OptionList[0] as DetectCurveOptions).Options;

                var roi = this.ROI;
                if (newRoi != null)
                {
                    roi = newRoi;
                }

                Collection <GeometricEdgeBasedPatternMatch> report = null;
                lock (locked)
                {
                    report = Algorithms.MatchGeometricPatternEdgeBased(image, this.Temp, op1, DetectGeometric.MatchOptions, roi.ConvertToRoi());
                }

                if (report.Count > 0)
                {
                    report[0].Rotation = this.AdjuestAngle(report[0].Rotation);
                    rtn.Point          = report[0].Position;
                    rtn.Angle          = report[0].Rotation;

                    image.Overlays.Default.AddPoint(rtn.Point, Rgb32Value.RedColor, new PointSymbol(PointSymbolType.Cross));

                    foreach (var match in report)
                    {
                        image.Overlays.Default.AddPolygon(new PolygonContour(match.Corners), Rgb32Value.BlueColor);
                    }

                    this.AddVisionResc(rtn, $"边缘匹配成功 角度:{rtn.Angle}");
                    rtn.State = VisionResultState.OK;
                }
                else
                {
                    this.AddVisionResc(rtn, "边缘匹配失败");
                    rtn.State = VisionResultState.NG;
                }
            }
            catch (Exception 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
            {
                Collection <RotationAngleRange> list = new Collection <RotationAngleRange>();
                list.Add(new RotationAngleRange(this.MinAngle, this.MaxAngle));
                list.Add(new RotationAngleRange(0, 0));

                var roi = this.ROI;
                if (newRoi != null)
                {
                    roi = newRoi;
                }
                Collection <PatternMatchReport> report = null;
                lock (locked)
                {
                    report = Algorithms.MatchPattern3(image, this.Temp, this.MatchingAlgorithm, 1, this.MinScore, list, roi.ConvertToRoi(), PatternOption);
                }

                if (report.Count > 0)
                {
                    rtn.Point = report[0].Position;
                    rtn.Angle = this.AdjuestAngle(report[0].Rotation);

                    foreach (PatternMatchReport match in report)
                    {
                        image.Overlays.Default.AddPolygon(new PolygonContour(match.Corners), Rgb32Value.BlueColor);
                    }
                    image.Overlays.Default.AddPoint(report[0].Position);
                    rtn.Point.X = report[0].Position.X;
                    rtn.Point.Y = report[0].Position.Y;
                    this.AddVisionResc(rtn, $"灰度匹配成功 角度:{rtn.Angle}");

                    rtn.State = VisionResultState.OK;
                }
                else
                {
                    this.AddVisionResc(rtn, "匹配失败");
                    rtn.State = VisionResultState.NG;
                }
            }
            catch (Exception ex)
            {
                this.AddVisionResc(rtn, ex.Message);
                rtn.State = VisionResultState.NG;
            }

            return(rtn);
        }
        /// <summary>
        /// 找圆
        /// </summary>
        /// <param name="image"></param>
        /// <param name="roi"></param>
        /// <returns></returns>
        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
            {
                var op1 = (this.OptionList[0] as DetectCurveOptions).Options;

                var roi = this.ROI.ConvertToRoi();
                if (parent != null && Result != null && !string.IsNullOrEmpty(parent.OrgCrood) && Result.ContainsKey(parent.OrgCrood))
                {
                    CoordinateSystem old      = new CoordinateSystem(parent.OrgPoint);
                    CoordinateSystem newCrood = new CoordinateSystem(Result[parent.OrgCrood].Point);
                    Algorithms.TransformRoi(roi, new CoordinateTransform(old, newCrood));
                }
                else if (newRoi != null)
                {
                    roi = newRoi.ConvertToRoi();
                }

                CircleDescriptor dsec = new CircleDescriptor(new Range(this.MinRadius, this.MaxRadius));
                var circles           = Algorithms.DetectCircles(image, dsec, roi, op1);

                if (circles.Count > 0)
                {
                    image.Overlays.Default.AddOval(circles[0].Circle, Rgb32Value.RedColor);
                    image.Overlays.Default.AddPoint(circles[0].Center, Rgb32Value.RedColor);
                    this.AddVisionResc(rtn, $"半径[{circles[0].Radius:N2}] 侦测到个数{circles.Count}");
                    rtn.Point = circles[0].Center;
                    rtn.State = VisionResultState.OK;
                }
                else
                {
                    this.AddVisionResc(rtn, "没有侦测到圆");
                    rtn.State = VisionResultState.NG;
                }
            }
            catch (VisionException ex)
            {
                this.AddVisionResc(rtn, ex.Message);
                rtn.State = VisionResultState.NG;
            }
            return(rtn);
        }
Exemplo n.º 9
0
        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
            {
                if (parent.Detects.Count > 0 && Result != null)
                {
                    if (parent.ResultX != string.Empty &&
                        parent.ResultY != string.Empty &&
                        parent.Angle != string.Empty &&
                        Result.ContainsKey(parent.ResultX) &&
                        Result.ContainsKey(parent.ResultY) &&
                        Result.ContainsKey(parent.Angle))
                    {
                        PointContour center = new PointContour(Result[parent.ResultX].Point.X, Result[parent.ResultX].Point.Y);
                        PointContour offset = new PointContour(center.X + this.OffsetX, center.Y + this.OffsetY);
                        PointContour output = new PointContour();

                        this.PtRotate(offset, center, Result[parent.Angle].Angle - this.BaseAngle, out output);

                        Result[parent.ResultX].Point.X = output.X;
                        Result[parent.ResultY].Point.Y = output.Y;
                        rtn.Point = output;

                        image.Overlays.Default.AddPoint(output, Rgb32Value.YellowColor, new PointSymbol(PointSymbolType.Cross));
                        this.AddVisionResc(rtn, $"焦点({rtn.Point.X:n2},{rtn.Point.Y:n2})");
                        rtn.State = VisionResultState.OK;
                    }
                }
                else
                {
                    rtn.State = VisionResultState.NG;
                }
            }
            catch (Exception 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
            {
                image.Type = ImageType.U16;
                PixelValue va1 = this.SetPixelValue(image.Type, (float)this.Gain, 0, 0, 0);
                Algorithms.Multiply(image, va1, image);
                PixelValue va2 = this.SetPixelValue(image.Type, (float)this.Offset, 0, 0, 0);
                Algorithms.Subtract(image, va2, image);
                Algorithms.Cast(image, image, ImageType.U8, -1);
                this.AddVisionResc(rtn, "增强成功");
                rtn.State = VisionResultState.OK;
            }
            catch (Exception 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;
            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);
        }
Exemplo n.º 13
0
 public void AddVisionResc(VisionResult rtn, string desr)
 {
     rtn.VisionDesr.Add($"[{this.UnitID}]---{desr}");
 }
        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);
        }