我正在尝试在牙齿上进行多个模板匹配。
import numpy as np
import cv2
import glob
X_data=[]
files = glob.glob ('C:/Users/amuly/Pictures/Saved Pictures/template/*.jpg')
img=cv2.imread('C:/Users/amuly/Pictures/Saved Pictures/teeth.jpeg')
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
for myfile in files:
temp=cv2.imread(myfile)
w,h=temp.shape[::-1]
我在此行之后收到错误太多值以解压缩。 请帮助。
res=cv2.matchTemplate(gray,temp,cv2.TM_CCOEFF_NORMED)
threshold=0.86
loc=np.where(res>=threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(img,pt,(pt[0]+w,pt[1]+h),(0,255,255),2)
cv2.imshow('Detected',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
答案 0 :(得分:0)
如果您的图片是彩色的,temp.shape
将返回一个包含3个值的元组。你不能将它解包为2个变量,你需要三个。
也许你的代码中也偶然出现了双冒号。
w,h=temp.shape[::-1]
这样可以从元组中删除最后一个元素,这样就可以解压缩到两个变量。
w,h=temp.shape[:-1]
答案 1 :(得分:0)
图片来源-Google图片
代码和图像文件的链接-https://github.com/Pawankumar2925/multiple-template-matching
裁剪软件-Windows Paint
import cv2
import numpy as np
count=[0 ,0 ,0,0]
threshold=[0.9,0.9,0.85,0.9]
img_bgr= cv2.imread('myphoto.png')
img_gray=cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
templates = [ cv2.imread('template'+str(i)+'.png',cv2.IMREAD_GRAYSCALE) for i in
range(1,5) ]
w1,h1=templates[0].shape[::-1]
w2,h2=templates[1].shape[::-1]
w3,h3=templates[2].shape[::-1]
w4,h4=templates[3].shape[::-1]
result1=cv2.matchTemplate(img_gray,templates[0],cv2.TM_CCOEFF_NORMED)
result2=cv2.matchTemplate(img_gray,templates[1],cv2.TM_CCOEFF_NORMED)
result3=cv2.matchTemplate(img_gray,templates[2],cv2.TM_CCOEFF_NORMED)
result4=cv2.matchTemplate(img_gray,templates[3],cv2.TM_CCOEFF_NORMED)
loc1=np.where(result1>=threshold[0])
loc2=np.where(result2>=threshold[1])
loc3=np.where(result3>=threshold[2])
loc4=np.where(result4>=threshold[3])
for pt1 in zip(*loc1[::-1]):
cv2.rectangle(img_bgr,pt1,(pt1[0]+w1,pt1[1]+h1),(0,255,0),2)
count[0]=count[0]+1
for pt2 in zip(*loc2[::-1]):
cv2.rectangle(img_bgr,pt2,(pt2[0]+w2,pt2[1]+h2),(255,0,0),2)
count[1]=count[1]+1
for pt3 in zip(*loc3[::-1]):
cv2.rectangle(img_bgr,pt3,(pt3[0]+w3,pt3[1]+h3),(0,0,255),2)
count[2]=count[2]+1
for pt4 in zip(*loc4[::-1]):
cv2.rectangle(img_bgr,pt4,(pt4[0]+w3,pt4[1]+h3),(0,0,255),2)
count[3]=count[3]+1
print "Circle"
print count[0]
print "Square"
print count[1]
print "Star"
print count[2]
print "Triangle"
print count[3]
cv2.imshow("image",img_bgr)
cv2.waitKey(0)
cv2.destroyAllWindows()
感谢和问候