模板与OpenCV Python中的多个对象匹配 [英] Template matching with multiple objects in OpenCV Python

查看:484
本文介绍了模板与OpenCV Python中的多个对象匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据但是问题是单个对象返回的多个点的位置略有不同. 像这样:

But the problem is that multiple points returned for a single object with a slightly difference in position. Something like this:

我不想使用cv2.minMaxLoc(),因为图像中有多个模板. 我编写了一个删除未结头寸的函数,但我想知道是否有解决此问题的简单方法? 谢谢.

I dont want to use cv2.minMaxLoc() because there is multiple templates in image. I wrote a function that delete the close positions, but I want to know is there any straightforward solution for this problem? thanks.

推荐答案

查找多个匹配项的一种方法是覆盖找到的匹配项,然后再次运行匹配项. 编辑:查找多个匹配项的一种 更好 方法是在 结果 上写上.在第一个示例中,我们将结果的匹配部分填充为零(对于SQDIFF或CCORR_NORMED使用1),然后在循环中查找下一个匹配项.

One way to find multiple matches is to write over the found matches and run the match again. Edit: A better way to find multiple matches is to write over the results. In the first example we fill the matched part of results with zeroes (use ones for SQDIFF or CCORR_NORMED) , and then look for the next match in a loop.

import cv2
import numpy as np
import time

image = cv2.imread('smiley.png', cv2.IMREAD_COLOR )
template = cv2.imread('template.png', cv2.IMREAD_COLOR)

h, w = template.shape[:2]

method = cv2.TM_CCOEFF_NORMED

threshold = 0.90

start_time = time.time()

res = cv2.matchTemplate(image, template, method)

# fake out max_val for first run through loop
max_val = 1
while max_val > threshold:
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

    if max_val > threshold:
        res[max_loc[1]-h//2:max_loc[1]+h//2+1, max_loc[0]-w//2:max_loc[0]+w//2+1] = 0   
        image = cv2.rectangle(image,(max_loc[0],max_loc[1]), (max_loc[0]+w+1, max_loc[1]+h+1), (0,255,0) )

cv2.imwrite('output.png', image)

输入图片:

将眼睛用作模板图像(因为有一只以上的眼睛!)

use the eyes as template images (since there is more than one eye!)

输出:

这是我通过覆盖图像来实现的原始方式.这种方式的速度要慢得多,因为我们对n个匹配项进行了n + 1 matchTemplate个操作.从一项测量来看,该技术要慢1000倍.

And here is the original way I did it by writing over the image. This way is way is much slower because we do n+1 matchTemplate operations for n matches. By one measurement, this technique is 1000 times slower.

import cv2
import numpy as np

image = cv2.imread('smiley.png', cv2.IMREAD_COLOR )
template = cv2.imread('template.png', cv2.IMREAD_COLOR)

h, w = template.shape[:2]

method = cv2.TM_CCOEFF_NORMED

threshold = 0.95

# fake out max_val for first run through loop
max_val = 1
while max_val > threshold:
    res = cv2.matchTemplate(image, template, method)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

    # using top ranked score, fill in that area with green
    image[max_loc[1]:max_loc[1]+h+1:, max_loc[0]:max_loc[0]+w+1, 0] = 0    # blue channel
    image[max_loc[1]:max_loc[1]+h+1:, max_loc[0]:max_loc[0]+w+1, 1] = 255  # green channel
    image[max_loc[1]:max_loc[1]+h+1:, max_loc[0]:max_loc[0]+w+1, 2] = 0    # red channel


cv2.imwrite('output.png', image)

输出图像:

这篇关于模板与OpenCV Python中的多个对象匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆