当我在python 2.7
上执行给定代码时,会出现以下错误代码错误:C:\ build \ master_winpack-bindings-win32-vc14-static \ opencv \ modules \ imgproc \ src \ templmatch.cpp:1102:错误:( - 215)(深度== CV_8U ||深度== CV_32F)&& type == _templ.type()&&函数cv :: matchTemplate
中的_img.dims()< = 2未修复:
这个给定的解决方案 img.astype(np.float32) img.astype(np.uint8)
代码:
import cv2
list1=[]
grid1=[]
img=cv2.imread('index.jpeg')
C,H,W=img.shape[::-1]
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #convert color to gray scale
ret,thresh=cv2.threshold(gray,47,255,cv2.THRESH_BINARY) #removing text threshold
_,contours,_=cv2.findContours(thresh[:,0:H,],cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for i in range(len(contours)):
perimeter=cv2.arcLength(contours[i],True)
if perimeter > 150 and perimeter <200 :
#cv2.drawContours(img,contours,i,(0,255,0),2)
list1.append(i)
for x in list1:
M=cv2.moments(contours[x])
cx=int(M['m10']/M['m00'])
cy=int(M['m01']/M['m00'])
cropped=img[cy-4:cy+40,cx-40:cx+40]
w,h,c=cropped.shape
for i in range(10):
template=cv2.imread("no."+str(i)+".png")
result=cv2.matchTemplate(cropped,template,cv2.TM_CCOEFF_NORMED)
min_val,max_val,min_loc,max_loca=cv2.minMAXLoc(result)
if(max_val>0.9):
grid1.append(i)
print grid1
print list1
cv2.imshow("orignal",img)
cv2.waitKey(0)
cv2.destroyALLWindows()