【发布时间】:2018-07-15 16:35:48
【问题描述】:
我正在使用 open-Cv 做一个基于模板匹配的项目。在这里,我需要使用多个模板检查我的源图像以识别正确的模板。该代码适用于单个模板,但对于多个图像,我需要关闭每个模板的输出窗口。尽管它与相应的模板正确匹配,但卡在这里。以下是代码:
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 循环,但不知道如何实现它每次都会出错。
【问题讨论】:
标签: python-3.x opencv image-processing template-matching