我有一些这样的代码来查找结果图像中模板的所有实例。
Image<Gray, Byte> templateImage = new Image<Gray, Byte>(bmpSnip);
Image<Gray, float> imgMatch = sourceImage.MatchTemplate(templateImage, Emgu.CV.CvEnum.TM_TYPE.CV_TM_CCOEFF_NORMED);然后循环通过imgMatch.Data,,属性检查分数是否超过阈值(比方说> 0.75),并在图像上放置关于匹配的标记。但这些匹配没有任何意义,我怀疑我把坐标搞错了。
float[,,] matches = imgMatch.Data;
for (int x = 0; x < matches.GetLength(0); x++)
{
for (int y = 0; y < matches.GetLength(1); y++)
{
double matchScore = matches[x, y, 0];
if (matchScore > 0.75)
{
Rectangle rect = new Rectangle(new Point(x,y), new Size(1, 1));
imgSource.Draw(rect, new Bgr(Color.Blue), 1);
}
}
}如果我使用如下所示的MinMax:
double[] min, max;
Point[] pointMin, pointMax;
imgMatch.MinMax(out min, out max, out pointMin, out pointMax);并设置一个标记(矩形)来突出显示一个匹配,我得到了一个非常好的结果。因此,我非常确定这与确定imgMatch.Data的坐标有关,
你知道我哪里错了吗?
发布于 2011-09-14 20:51:31
答案实际上是:
由于您使用的是Emgu.CV.CvEnum.TM_TYPE.CV_TM_CCOEFF_NORMED,因此结果取决于模板大小。你得到的任何结果,即匹配点,实际上都是对模板左上角的引用,所以要找到匹配的中心,只需从X减去模板宽度的一半,从Y减去模板高度的一半。
if (matchScore > 0.75)
{
Rectangle rect = new Rectangle(new Point(x - templateImage.Width ,y - templateImage-Height), new Size(1, 1));
imgSource.Draw(rect, new Bgr(Color.Blue), 1);
}除此之外,您仅使用0.75的阈值,0.9或更高的阈值将产生更理想的结果。要准确评估所需的值阈值,请直接查看imgMatch中的结果,或者查看数据的直方图结构。
保重,克里斯
发布于 2011-10-14 16:22:32
您在数组中放错了X和Y坐标。试试这个:
float[, ,] matches = imgMatch.Data;
for (int y = 0; y < matches.GetLength(0); y++)
{
for (int x = 0; x < matches.GetLength(1); x++)
{
double matchScore = matches[y, x, 0];
if (matchScore > 0.75)
{
Rectangle rect = new Rectangle(new Point(x,y), new Size(1, 1));
imgSource.Draw(rect, new Bgr(Color.Blue), 1);
}
}
}https://stackoverflow.com/questions/3047210
复制相似问题