Beispiel #1
0
        private FeatureMatchResult GetMatchResult(IList <DMatch> matches, IList <KeyPoint> sourceKeyPoints,
                                                  IList <KeyPoint> searchKeyPoints)
        {
            //至少识别3个点才能得到一个几何图形
            var success     = matches.Count > 3;
            var matchResult = new FeatureMatchResult()
            {
                Success = success
            };

            if (!success)
            {
                return(matchResult);
            }

            var goodSourceKeyPoints = new List <KeyPoint>();
            var goodSearchKeyPoints = new List <KeyPoint>();

            var points = new List <System.Drawing.Point>();

            double x = double.MaxValue, x1 = 0, y = double.MaxValue, y1 = 0;

            foreach (var match in matches)
            {
                goodSourceKeyPoints.Add(sourceKeyPoints[match.QueryIdx]);
                goodSearchKeyPoints.Add(searchKeyPoints[match.TrainIdx]);

                var point = sourceKeyPoints[match.QueryIdx].Pt;
                points.Add(new System.Drawing.Point((int)point.X, (int)point.Y));

                x  = Math.Min(point.X, x);
                x1 = Math.Max(point.X, x1);
                y  = Math.Min(point.Y, y);
                y1 = Math.Max(point.Y, y1);
            }

            var leftTop = new System.Drawing.Point((int)Math.Min(x, x1), (int)Math.Min(y, y1));
            var size    = new System.Drawing.Size((int)Math.Abs(x - x1), (int)Math.Abs(y - y1));

            matchResult.MatchItems.Add(new FeatureMatchResultItem()
            {
                Point = new System.Drawing.Point((int)(x + (double)size.Width / 2),
                                                 (int)(y + (double)size.Height / 2)),
                FeaturePoints = points,
                Rectangle     = new Rectangle(leftTop, size),
            });
            return(matchResult);
        }
Beispiel #2
0
        /// <summary>
        ///     预览匹配结果(仅在开启了<see cref="MatchArgument.PreviewMatchResult"/>配置时)
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="matchResult"></param>
        /// <param name="sourceMat"></param>
        /// <param name="searchMat"></param>
        /// <param name="keySourcePoints"></param>
        /// <param name="keySearchPoints"></param>
        /// <param name="goodMatches"></param>
        internal static void PreviewDebugFeatureMatchResult(this MatchArgument argument, FeatureMatchResult matchResult, Mat sourceMat, Mat searchMat,
                                                            IEnumerable <KeyPoint> keySourcePoints, IEnumerable <KeyPoint> keySearchPoints,
                                                            IEnumerable <DMatch> goodMatches)
        {
            if (!argument.IsExtensionConfigEnabled(MatchArgument.PreviewMatchResult))
            {
                return;
            }

            using var image = new Mat(sourceMat, OpenCvSharp.Range.All);
            if (matchResult.Success)
            {
                foreach (var matchItem in matchResult.MatchItems)
                {
                    var rectangle = matchItem.Rectangle;
                    Cv2.Rectangle(image, new Point(rectangle.X, rectangle.Y), new Point(rectangle.Right, rectangle.Bottom), Scalar.RandomColor(), 3);
                }
            }

            using var imgMatch = new Mat();
            Cv2.DrawMatches(image, keySourcePoints, searchMat, keySearchPoints, goodMatches, imgMatch, flags: DrawMatchesFlags.NotDrawSinglePoints);
            PreviewMatchResultImage(imgMatch);
        }