예제 #1
0
        /// <summary>
        ///     获取匹配结果
        /// </summary>
        /// <param name="searchMat">对应的训练(模板)图像</param>
        /// <param name="resultMat">匹配结果</param>
        /// <param name="matchModes">匹配算法</param>
        /// <param name="argument">匹配参数</param>
        /// <returns></returns>
        private static TemplateMatchResult GetMatchResult(Mat searchMat, Mat resultMat, TemplateMatchModes matchModes, TemplateMatchArgument argument)
        {
            var threshold = argument.Threshold;
            var maxCount  = argument.MaxCount;

            var matchResult = new TemplateMatchResult();

            while (matchResult.MatchItems.Count < maxCount)
            {
                double value;
                Point  topLeft;
                Cv2.MinMaxLoc(resultMat, out var minValue, out var maxValue, out var minLocation, out var maxLocation);
                if (matchModes == TemplateMatchModes.SqDiff || matchModes == TemplateMatchModes.SqDiffNormed)
                {
                    value   = minValue;
                    topLeft = minLocation;
                }
                else
                {
                    value   = maxValue;
                    topLeft = maxLocation;
                }

                if (maxValue < threshold)
                {
                    break;
                }

                argument.OutputDebugMessage($"[TemplateMatch] The info of the match is ([{matchModes}][{threshold:F}][{matchResult.MatchItems.Count}]) = {value:F}.");
                var matchItem = new TemplateMatchResultItem()
                {
                    Value = value
                };
                var centerX = topLeft.X + (double)searchMat.Width / 2;
                var centerY = topLeft.Y + (double)searchMat.Height / 2;
                matchItem.Point     = new System.Drawing.Point((int)centerX, (int)centerY);
                matchItem.Rectangle =
                    new System.Drawing.Rectangle(topLeft.X, topLeft.Y, searchMat.Width, searchMat.Height);
                matchResult.MatchItems.Add(matchItem);

                //屏蔽已筛选区域
                if (matchModes == TemplateMatchModes.SqDiff || matchModes == TemplateMatchModes.SqDiffNormed)
                {
                    Cv2.FloodFill(resultMat, topLeft, double.MaxValue);
                }
                else
                {
                    Cv2.FloodFill(resultMat, topLeft, double.MinValue);
                }
            }

            matchResult.Success = matchResult.MatchItems.Any();
            return(matchResult);
        }
예제 #2
0
        /// <summary>
        ///     预览模版匹配结果(仅在开启了<see cref="MatchArgument.PreviewMatchResult"/>配置时)
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="matchResult"></param>
        /// <param name="sourceImage"></param>
        internal static void PreviewDebugTemplateMatchResult(this MatchArgument argument, TemplateMatchResult matchResult, Mat sourceImage)
        {
            if (!argument.IsExtensionConfigEnabled(MatchArgument.PreviewMatchResult))
            {
                return;
            }

            using var image = new Mat(sourceImage, 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);
                }
            }
            PreviewMatchResultImage(image);
        }