且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

OpenCV C ++-查找图像中包含的图像?

更新时间: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输出minValmaxVal.在找到正确匹配的情况下和给您误报的情况下,您应该比较该值.这些实验应允许您建立一个阈值,以区分真假和假阳性.因此,您将能够说出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:

  1. minMaxLoc之前尝试查看所有获得的结果,计算平均值,然后查看找到的maxVal是否比真实阳性病例的平均值大得多.也许您可以将阈值定义为平均值的百分比,这样说:if maxVal > meanVal + meanVal * n%: match_found = true
  2. 常见的情况是,模板匹配在边缘上比在真实图像上效果更好.同样,您没有提供示例,因此很难说这种方法的可靠性.但是,如果您有足够高的频率,可以使用Canny Edges照亮图像,则可能会为您区分真假阳性提供一个更清晰的阈值.
  1. Try looking at all obtained results, before 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
  2. It is a common situation, that template matching works better with edges than with the real image. Again, you haven't provided samples, so it's hard to say how reliable will that approach be here. But if you have enough high frequencies, to light up an image with Canny Edges, that might give you a much clearer threshold for discriminating between true and false positives.

由于您使用的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.