模板与多个模板匹配

时间:2018-02-05 18:22:58

标签: python-3.x opencv image-processing template-matching

我正在使用open-Cv进行基于模板匹配的项目。在这里,我需要使用多个模板检查我的源图像,以确定正确的模板。代码适用于单个模板,但对于多个图像,我需要关闭每个模板的输出窗口。虽然它匹配正确的各个template.kind卡在这里。以下是代码:

import cv2
import numpy as np
import os,sys
import os.path
import cv2, glob
images=glob.glob(r'D:\python\template\Country\*.jpg')

for image in images:
img_rgb = cv2.imread(r'D:\python\EURO-10-F.jpg')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)

template = cv2.imread(image,0)
w, h = template.shape[::-1]

print (img_gray.shape)
r = 500.0 / img_gray.shape[1]
dim = (500, int(img_gray.shape[0] * r))

resized1 = cv2.resize(img_rgb, dim, interpolation = cv2.INTER_AREA)    
resized2 = cv2.resize(img_gray, dim, interpolation = cv2.INTER_AREA)


res = cv2.matchTemplate(resized2,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.5
loc = np.where( res >= threshold)


for pt in zip(*loc[::-1]):
cv2.rectangle(resized1,pt,(pt[0]+w,pt[1]+h),(0,255,255),2)

cv2.imshow('detected',resized1)
cv2.waitKey(0)
cv2.destroyAllWindows()

我正在尝试for循环,但不知道如何实现它每次都会出错。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。您需要做的就是按如下方式添加循环:

if  a != ():
            cv2.imshow('detected',resized1)
            cv2.imshow('binary',binary)
break