我如何在OpenCV中找到模板的所有匹配项

How do I find all matches of a template in OpenCV?

本文关键字:OpenCV      更新时间:2023-10-16

我正在尝试使用 matchTemplate函数在源图像中找到公司的徽标。

但是我的问题是:

源图像可能包含与模板匹配的几乎相同的徽标,以及其他公司的徽标(无与伦比)。

如何找到所有匹配不仅是最佳匹配,就像detectMultiscale一样,它在向量中提供输出。

在一个段循环中运行模板匹配的情况,在此条件下,您询问是否找到了匹配项(有一定确定的阈值)。在每个循环结束时,保存比赛并掩盖了该区域,以免再次找到它。当确定性下降到水平下时,请从循环中脱颖而出。

伪代码:

while true:
    maxLoc, maxVal = template_matching()  # perform the matching
    if(maxVal < threshold)  # check if it is a good match and break if not
        break
    list.append(maxLoc)  # save the location of a good match
    mask_out(src, (maxLoc.x + template.cols , maxLoc.y + template.rows)) # mask out the area, so that it's not found again
draw_all_rectangles(list)

请记住,根据所使用的模板匹配方法,您将使用MINVAL和MINLOC或MAXVAL和MAXLOC。使用Minval,条件将使用相反的不等式符号。我将实施给您。祝你好运。