更新时间:2023-11-26 11:46:28
您正在可视化结果,而不管算法执行匹配的确定性如何.模板匹配将始终为您提供输出-您要做的是尝试找出它是否有效.
You are visualizing the result regardless of the certainty with which the algorithm performed matching. Template matching will always give you an output - what you want to do is to try to figure out if it's valid or not.
尝试根据match_method
输出minVal
或maxVal
.在找到正确匹配的情况下和给您误报的情况下,您应该比较该值.这些实验应允许您建立一个阈值,以区分真假和假阳性.因此,您将能够说出maxVal的大小,例如,必须确保maxVal是匹配的.伪代码将如下所示:
Try outputing minVal
or maxVal
depending on the match_method
. You should compare the value in the cases when the correct match was found and in the cases when it gave you a false positive. Those experiments should allow you to establish a threshold, that distinguishes between true hits and false positives. Thus, you will be able to say how big - for example - the maxVal has to be to be sure that it was a match. Pseudo code would go something like this:
if maxVal > threshold:
match_found = true
match_position = maxLoc
现在,这是一种理论方法.由于您未提供任何图像,因此它可能会也可能不会解决您的问题.
Now that's a theoretical approach. Since you didn't provide any images, it might or might not be the solution for your problem.
如果您找不到确定的阈值(我认为,在大多数情况下,如果您保持质量,尺寸等,应该可以这样做),请尝试执行以下两种操作之一:
If you cannot find a definite threshold value (which in my opinion should be possible in most cases, if you maintain quality, size, etc), try doing one of two things:
minMaxLoc
之前尝试查看所有获得的结果,计算平均值,然后查看找到的maxVal
是否比真实阳性病例的平均值大得多.也许您可以将阈值定义为平均值的百分比,这样说:if maxVal > meanVal + meanVal * n%: match_found = true
minMaxLoc
, calculate the mean value and see if the maxVal
found is much bigger than the mean value in the true positive cases. Maybe you can define the threshold as the % of the mean value, thus saying: if maxVal > meanVal + meanVal * n%: match_found = true
由于您使用的match_method = 0,这意味着CV_TM_SQDIFF
.要对过程进行更多控制,请显式使用名称.在此处中找到有关方法的信息.
Since you're using match_method = 0, that means CV_TM_SQDIFF
. For more control over the process, use the name explicitly. Find information on the methods here.
另外,将cout放在if语句内,以便您打印正确的值,该值实际上是对匹配的识别(在您的情况下为minVal).
Also, put the cout inside the if statement, so that you print the correct value, that actually idicates the match (in your case, it's minVal).
if (match_method == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED)
{
matchLoc = minLoc;
std::cout << minVal << std::endl;
}
else
{
matchLoc = maxLoc;
std::cout << maxVal << std::endl;
}
再说一遍:合理调整的轮廓检测几乎可以肯定会在没有给您预期结果的情况下有所帮助.
And again: fairly tuned contours detection should almost certainly help if this doesn't give you the expected results.