多模板匹配

2024-10-01 04:59:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试多模板匹配牙齿。在

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] 

在这一行之后,我得到了错误太多的值无法解压缩。 请帮忙。在

^{pr2}$

Tags: importnumpy模板imgfilesmyfilecv2users
2条回答

基于OpenCV-Python的多模板匹配-

图片来源谷歌图片

代码和图像文件的链接-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()

Thanks and Regards

如果图像是彩色的,temp.shape将返回一个包含3个值的元组。你不能把它分解成2个变量,你需要3个变量。在

也许你的代码中也有一个双冒号。在

 w,h=temp.shape[::-1] 

从最后一个元素中删除两个变量,这样就可以把它们解包了。在

^{pr2}$

相关问题 更多 >