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);
        }
示例#2
0
        private void FindStraightEdges()
        {
            // Use search direction selected.
            SearchDirection direction = (SearchDirection)Enum.Parse(typeof(SearchDirection), (string)searchDirection.SelectedItem);

            // Fill in the edge options structure from the controls on the form.
            EdgeOptions edgeOptions = new EdgeOptions();

            edgeOptions.ColumnProcessingMode = (ColumnProcessingMode)Enum.Parse(typeof(ColumnProcessingMode), (string)smoothing.SelectedItem);
            edgeOptions.InterpolationType    = (InterpolationMethod)Enum.Parse(typeof(InterpolationMethod), (string)interpolationMethod.SelectedItem);
            edgeOptions.KernelSize           = (uint)kernelSize.Value;
            edgeOptions.MinimumThreshold     = (uint)minimumThreshold.Value;
            edgeOptions.Polarity             = (EdgePolaritySearchMode)Enum.Parse(typeof(EdgePolaritySearchMode), (string)polarity.SelectedItem);
            edgeOptions.Width = (uint)width.Value;

            // Fill in the straight edge options structure from the controls on the form.
            StraightEdgeOptions straightEdgeOptions = new StraightEdgeOptions();

            straightEdgeOptions.AngleRange      = (double)angleRange.Value;
            straightEdgeOptions.AngleTolerance  = (double)angleTolerance.Value;
            straightEdgeOptions.HoughIterations = (uint)houghIterations.Value;
            straightEdgeOptions.ScoreRange.Initialize((double)minimumScore.Value, (double)maximumScore.Value);
            straightEdgeOptions.MinimumCoverage           = (double)minimumCoverage.Value;
            straightEdgeOptions.MinimumSignalToNoiseRatio = (double)minimumSignalToNoiseRatio.Value;
            straightEdgeOptions.NumberOfLines             = (uint)numberOfLines.Value;
            straightEdgeOptions.Orientation = (double)orientation.Value;
            straightEdgeOptions.SearchMode  = (StraightEdgeSearchMode)Enum.Parse(typeof(StraightEdgeSearchMode), (string)searchMode.SelectedItem);
            straightEdgeOptions.StepSize    = (uint)stepSize.Value;

            // Run the edge detection.
            StraightEdgeReport report = Algorithms.StraightEdge(imageViewer1.Image, imageViewer1.Roi, direction, edgeOptions, straightEdgeOptions);

            // Clear all overlays from previous run.
            imageViewer1.Image.Overlays.Default.Clear();

            // Overlay the straight edges.
            foreach (StraightEdgeReportItem item in report.StraightEdges)
            {
                imageViewer1.Image.Overlays.Default.AddLine(item.StraightEdge, Rgb32Value.RedColor);
            }

            // Overlay the search lines.
            foreach (SearchLineInfo info in report.SearchLines)
            {
                // First the line itself.
                imageViewer1.Image.Overlays.Default.AddLine(info.Line, Rgb32Value.BlueColor);
                // Then the found edges on each line.
                foreach (EdgeInfo edgeInfo in info.EdgeReport.Edges)
                {
                    imageViewer1.Image.Overlays.Default.AddOval(new OvalContour(edgeInfo.Position.X - 1, edgeInfo.Position.Y - 1, 3, 3), Rgb32Value.YellowColor, DrawingMode.PaintValue);
                }
            }
        }
示例#3
0
        public static short FitLine(VisionImage image, RectangleContour roi, double EdgeStength, int searchDir, int edgeMode, ref PointContour PointStart, ref PointContour PointEnd)//视觉抓边算法
        {
            SearchDirection        SearchDirection = SearchDirection.BottomToTop;
            EdgePolaritySearchMode EdgeMode        = EdgePolaritySearchMode.Falling;

            if (searchDir == 0)
            {
                SearchDirection = SearchDirection.LeftToRight;
            }
            else if (searchDir == 1)
            {
                SearchDirection = SearchDirection.RightToLeft;
            }
            else if (searchDir == 2)
            {
                SearchDirection = SearchDirection.TopToBottom;
            }
            else if (searchDir == 3)
            {
                SearchDirection = SearchDirection.BottomToTop;
            }

            if (edgeMode == 0)
            {
                EdgeMode = EdgePolaritySearchMode.Rising;
            }
            else if (edgeMode == 1)
            {
                EdgeMode = EdgePolaritySearchMode.Falling;
            }

            if (roi != null)
            {
                VisionImage imageTemp = new VisionImage();
                Algorithms.Extract(image, imageTemp, roi);
                Algorithms.ImageToImage(imageTemp, image, new PointContour(roi.Left, roi.Top));



                #region 抓边参数
                StraightEdgeOptions vaStraightEdgeOptions = new StraightEdgeOptions();
                vaStraightEdgeOptions.NumberOfLines             = 1;
                vaStraightEdgeOptions.SearchMode                = StraightEdgeSearchMode.FirstRakeEdges;
                vaStraightEdgeOptions.ScoreRange                = new Range(0, 1024);
                vaStraightEdgeOptions.Orientation               = 0;
                vaStraightEdgeOptions.AngleRange                = 90;
                vaStraightEdgeOptions.AngleTolerance            = 1;
                vaStraightEdgeOptions.StepSize                  = 3;
                vaStraightEdgeOptions.MinimumSignalToNoiseRatio = 0;
                vaStraightEdgeOptions.MinimumCoverage           = 25;
                vaStraightEdgeOptions.HoughIterations           = 5;
                // Set EdgeOptions
                EdgeOptions vaEdgeOptions = new EdgeOptions();
                vaEdgeOptions.Polarity             = EdgeMode;
                vaEdgeOptions.KernelSize           = 7;
                vaEdgeOptions.Width                = 3;
                vaEdgeOptions.MinimumThreshold     = EdgeStength;
                vaEdgeOptions.InterpolationType    = InterpolationMethod.ZeroOrder;
                vaEdgeOptions.ColumnProcessingMode = ColumnProcessingMode.Average;
                #endregion

                try
                {
                    StraightEdgeReport report1 = Algorithms.StraightEdge2(image, roi.ConvertToRoi(), SearchDirection, vaEdgeOptions, vaStraightEdgeOptions, false);
                    if (report1.StraightEdges.Count > 0)
                    {
                        PointStart = report1.StraightEdges[0].StraightEdge.Start;
                        PointEnd   = report1.StraightEdges[0].StraightEdge.End;
                        //image.Overlays.Default.Clear();
                        image.Overlays.Default.AddLine(new LineContour(report1.StraightEdges[0].StraightEdge.Start, report1.StraightEdges[0].StraightEdge.End));
                        return(0);
                    }
                }
                catch { }
            }

            PointStart.X = 0;
            PointStart.Y = 0;
            PointEnd.X   = 0;
            PointEnd.Y   = 0;
            return(1);
        }