Beispiel #1
0
        public Line FindLine(HImage image, FindLineFeeding feeding, bool recordGraphics = true)
        {
            HObject       lineRegion, findLineRegion;
            HTuple        xsUsed = new HTuple();
            HTuple        ysUsed = new HTuple();
            HTuple        xsIgnored = new HTuple();
            HTuple        ysIgnored = new HTuple();
            HTuple        lineX1 = new HTuple();
            HTuple        lineY1 = new HTuple();
            HTuple        lineX2 = new HTuple();
            HTuple        lineY2 = new HTuple();
            List <double> ys, xs;


            // using pair
            HObject edges = new HObject();

            edges.GenEmptyObj();
            if (feeding.UsingPair)
            {
                if (feeding.FirstAttemptOnly)
                {
                    HalconScripts.FindLineGradiant_Pair(image, out findLineRegion, out lineRegion, feeding.Row,
                                                        feeding.Col, feeding.Radian, feeding.Len1, feeding.Len2, feeding.NumSubRects,
                                                        feeding.IgnoreFraction, feeding.Transition, feeding.Threshold, feeding.Sigma1,
                                                        feeding.WhichEdge, feeding.WhichPair, feeding.MinWidth, feeding.MaxWidth, out xsUsed,
                                                        out ysUsed, out xsIgnored, out ysIgnored, out lineX1, out lineY1, out lineX2, out lineY2);
                }
                else
                {
                    HalconScripts.VisionProStyleFindLineOneStep_Pairs(image, out findLineRegion, out lineRegion,
                                                                      feeding.Row, feeding.Col, feeding.Radian, feeding.Len1, feeding.Len2, feeding.Transition,
                                                                      feeding.NumSubRects, feeding.Threshold, feeding.Sigma1, feeding.Sigma2, feeding.WhichEdge,
                                                                      feeding.IsVertical, feeding.IgnoreFraction, feeding.WhichPair, feeding.MinWidth,
                                                                      feeding.MaxWidth, _width, _height, feeding.CannyHigh, feeding.CannyLow, "true",
                                                                      feeding.NewWidth, feeding.KernelWidth, feeding.LongestOnly, out xsUsed, out ysUsed, out xsIgnored, out ysIgnored,
                                                                      out lineX1, out lineY1,
                                                                      out lineX2, out lineY2);
                }

                xs = xsUsed.DArr.ToList();
                ys = ysUsed.DArr.ToList();
            } // using single edge
            else
            {
                if (feeding.FirstAttemptOnly)
                {
                    HalconScripts.FindLineGradiant(image, out findLineRegion, out lineRegion, feeding.Row, feeding.Col,
                                                   feeding.Radian, feeding.Len1, feeding.Len2, feeding.NumSubRects, feeding.IgnoreFraction,
                                                   feeding.Transition, feeding.Threshold, feeding.Sigma1, feeding.WhichEdge, out xsUsed,
                                                   out ysUsed, out xsIgnored, out ysIgnored, out lineX1, out lineY1, out lineX2, out lineY2);
                    ys = ysUsed.DArr.ToList();
                    xs = xsUsed.DArr.ToList();
                }
                else

                {
//                    HalconScripts.VisionProStyleFindLineOneStep(image, out findLineRegion, out lineRegion,
//                        feeding.Transition, feeding.Row, feeding.Col, feeding.Radian, feeding.Len1, feeding.Len2,
//                        feeding.NumSubRects, feeding.Threshold, feeding.WhichEdge, feeding.IgnoreFraction,
//                        feeding.IsVertical, feeding.Sigma1, feeding.Sigma2, _width, _height, feeding.NewWidth,
//                        feeding.CannyHigh, feeding.CannyLow, out lineX1, out lineY1, out lineX2, out lineY2, out xsUsed,
//                        out ysUsed, out xsIgnored, out ysIgnored);

                    var xsys = HalconHelper.FindLineSubPixel(image, feeding.Row, feeding.Col,
                                                             feeding.Radian, feeding.Len1, feeding.Len2, feeding.Transition.S,
                                                             feeding.NumSubRects.I, feeding.Threshold, feeding.WhichEdge.S, feeding.IgnoreFraction.D,
                                                             feeding.CannyLow, feeding.CannyHigh.D, feeding.Sigma1.D, feeding.Sigma2.D, feeding.NewWidth.I,
                                                             feeding.KernelWidth, feeding.LongestOnly,
                                                             out edges, out findLineRegion);

                    xs = xsys.Item1;
                    ys = xsys.Item2;
                }
            }


            IEnumerable <double> xsInlier, ysInlier;
            var line = HalconHelper.RansacFitLine(xs.ToArray(), ys.ToArray(), feeding.ErrorThreshold, feeding.MaxTrials,
                                                  feeding.IgnoreFraction, feeding.Probability, out xsInlier, out ysInlier);

            xs = xsInlier.ToList();
            ys = ysInlier.ToList();


            HalconScripts.GenLineRegion(out lineRegion, line.XStart, line.YStart, line.XEnd, line.YEnd, _width,
                                        _height);
            lineX1 = line.XStart;
            lineY1 = line.YStart;
            lineX2 = line.XEnd;
            lineY2 = line.YEnd;


            // Generate debugging graphics

            if (recordGraphics)
            {
                HObject crossesIgnored;
                HOperatorSet.GenCrossContourXld(out crossesIgnored, ysIgnored, xsIgnored, CrossSize, CrossAngle);

                // Critical section
                lock (this)
                {
                    CrossesUsed.Add(new Tuple <List <double>, List <double> >(xs, ys));
                    HOperatorSet.ConcatObj(_crossesIgnored, crossesIgnored, out _crossesIgnored);
                    HOperatorSet.ConcatObj(_findLineRects, findLineRegion, out _findLineRects);
                    HOperatorSet.ConcatObj(_lineRegions, lineRegion, out _lineRegions);
                    HOperatorSet.ConcatObj(Edges, edges, out _edges);
                }
            }


            return(new Line(lineX1.D, lineY1.D, lineX2.D, lineY2.D));
        }