Ejemplo n.º 1
0
        public Line TryFindLine(string name, HImage image, FindLineFeeding feeding, bool recordGraphics = true)
        {
            Line line = new Line();

            try
            {
                line = FindLine(image, feeding, recordGraphics);
            }
            catch (Exception e)
            {
                Debugger.Break();
                PromptUserInvoke($"Line {name} not found! {Environment.NewLine} {e.Message}");
            }

            return(line);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// If key not exist, add one find line feeding
        /// </summary>
        /// <param name="name"></param>
        /// <param name="findLineFeedings"></param>
        private void TryAddFindLineFeedings(string name, Dictionary <string, FindLineFeeding> findLineFeedings)
        {
            FindLineFeeding output;
            string          key;

            // No "-" means one find line rect for one line result
            if (!name.Contains("-"))
            {
                key = name;
                var param    = FindLineParamsDict[name];
                var location = _findLineLocationsAbsDict[name];

                output = new FindLineFeeding()
                {
                    Row              = location.Y,
                    Col              = location.X,
                    Radian           = MathUtils.ToRadian(location.Angle),
                    Len1             = location.Len1,
                    Len2             = location.Len2,
                    Transition       = location.Polarity == FindLinePolarity.Positive ? "positive" : "negative",
                    NumSubRects      = param.NumSubRects,
                    IgnoreFraction   = param.IgnoreFraction,
                    Threshold        = param.Threshold,
                    Sigma1           = param.Sigma1,
                    Sigma2           = param.Sigma2,
                    WhichEdge        = param.WhichEdge == EdgeSelection.First ? "first" : "last",
                    WhichPair        = param.WhichPair == PairSelection.First ? "first" : "last",
                    NewWidth         = param.NewWidth,
                    MinWidth         = param.MinWidth,
                    MaxWidth         = param.MaxWidth,
                    FirstAttemptOnly = param.FirstAttemptOnly(),
                    UsingPair        = param.UsingPair(),
                    ImageIndex       = location.ImageIndex,
                    IsVertical       = location.IsVertical,
                    CannyHigh        = param.CannyHigh,
                    CannyLow         = param.CannyLow,
                    MaxTrials        = param.MaxTrials,
                    ErrorThreshold   = param.ErrorThreshold,
                    Probability      = param.Probability,
                    KernelWidth      = param.KernelWidth,
                    LongestOnly      = param.LongestOnly ? "true" : "false"
                };
            }
            else
            {
                key = name.Substring(0, name.IndexOf("-"));
                // If the key has already added... for example 2 for 2-left and 2-right
                if (findLineFeedings.ContainsKey(key))
                {
                    return;
                }

                var locations  = _findLineLocationsAbsDict.Where(pair => pair.Key.Contains(key)).Select(pair => pair.Value).ToList();
                var parameters = FindLineParamsDict.Where(pair => pair.Key.Contains(key)).Select(pair => pair.Value).ToList();

                if (locations.Count != parameters.Count)
                {
                    throw new InvalidOperationException($"Location count {locations.Count} != parameter count {parameters.Count}");
                }

                output = new FindLineFeeding()
                {
                    Row              = locations.Select(l => l.Y).ToArray(),
                    Col              = locations.Select(l => l.X).ToArray(),
                    Radian           = locations.Select(l => MathUtils.ToRadian(l.Angle)).ToArray(),
                    Len1             = locations.Select(l => l.Len1).ToArray(),
                    Len2             = locations.Select(l => l.Len2).ToArray(),
                    Transition       = locations[0].Polarity == FindLinePolarity.Positive ? "positive" : "negative",
                    NumSubRects      = parameters[0].NumSubRects,
                    IgnoreFraction   = parameters[0].IgnoreFraction,
                    Threshold        = parameters.Select(p => p.Threshold).ToArray(),
                    Sigma1           = parameters[0].Sigma1,
                    Sigma2           = parameters[0].Sigma2,
                    WhichEdge        = parameters[0].WhichEdge == EdgeSelection.First ? "first" : "last",
                    WhichPair        = parameters[0].WhichPair == PairSelection.First ? "first" : "last",
                    NewWidth         = parameters[0].NewWidth,
                    MinWidth         = parameters[0].MinWidth,
                    MaxWidth         = parameters[0].MaxWidth,
                    FirstAttemptOnly = parameters[0].FirstAttemptOnly(),
                    UsingPair        = parameters[0].UsingPair(),
                    ImageIndex       = locations[0].ImageIndex,
                    IsVertical       = locations[0].IsVertical,
                    CannyHigh        = parameters[0].CannyHigh,
                    CannyLow         = parameters[0].CannyLow,
                    MaxTrials        = parameters[0].MaxTrials,
                    ErrorThreshold   = parameters[0].ErrorThreshold,
                    Probability      = parameters[0].Probability,
                    KernelWidth      = parameters[0].KernelWidth,
                    LongestOnly      = parameters[0].LongestOnly ? "true" : "false"
                };
            }
            findLineFeedings[key] = output;
        }
Ejemplo n.º 3
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));
        }