gpt4 book ai didi

c# - OpenCVSharp3 MatchTemplate 中的多个结果

转载 作者:太空宇宙 更新时间:2023-11-03 21:04:01 36 4
gpt4 key购买 nike

我正在尝试查找图像中出现的图像。我编写了以下代码以使用 OpenCVSharp3 获得单个匹配项:

Mat src = OpenCvSharp.Extensions.BitmapConverter.ToMat(Resources.all);
Mat template = OpenCvSharp.Extensions.BitmapConverter.ToMat(Resources.img);
Mat result = src.MatchTemplate(template, TemplateMatchModes.CCoeffNormed);

double minVal, maxVal;
OpenCvSharp.Point minLoc, maxLoc;
result.MinMaxLoc(out minVal, out maxVal, out minLoc, out maxLoc);
Console.WriteLine("maxLoc: {0}, maxVal: {1}", maxLoc, maxVal);

如何根据阈值获得更多匹配项?

谢谢!

最佳答案

这是 C++ 代码的一部分。祝你好运!

using OpenCvSharp;
using OpenCvSharp.Util;
static void RunTemplateMatch(string reference, string template)
{
using (Mat refMat = new Mat(reference))
using (Mat tplMat = new Mat(template))
using (Mat res = new Mat(refMat.Rows - tplMat.Rows + 1, refMat.Cols - tplMat.Cols + 1, MatType.CV_32FC1))
{
//Convert input images to gray
Mat gref = refMat.CvtColor(ColorConversionCodes.BGR2GRAY);
Mat gtpl = tplMat.CvtColor(ColorConversionCodes.BGR2GRAY);

Cv2.MatchTemplate(gref, gtpl, res, TemplateMatchModes.CCoeffNormed);
Cv2.Threshold(res, res, 0.8, 1.0, ThresholdTypes.Tozero);

while (true)
{
double minval, maxval, threshold = 0.8;
Point minloc, maxloc;
Cv2.MinMaxLoc(res, out minval, out maxval, out minloc, out maxloc);

if (maxval >= threshold)
{
//Setup the rectangle to draw
Rect r = new Rect(new Point(maxloc.X, maxloc.Y), new Size(tplMat.Width, tplMat.Height));

//Draw a rectangle of the matching area
Cv2.Rectangle(refMat, r, Scalar.LimeGreen, 2);

//Fill in the res Mat so you don't find the same area again in the MinMaxLoc
Rect outRect;
Cv2.FloodFill(res, maxloc, new Scalar(0), out outRect, new Scalar(0.1), new Scalar(1.0));
}
else
break;
}

Cv2.ImShow("Matches", refMat);
Cv2.WaitKey();
}
}

关于c# - OpenCVSharp3 MatchTemplate 中的多个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32737420/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com