我正在使用模板匹配但我更改图片距离程序不起作用
代码 # - - 编码:cp1254 - - 导入cv2 导入numpy为np
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
template = cv2.imread('template.jpg',0)
w, h = template.shape[::-1]
x,y = 0,0
while(True):
ret, frame = cap.read()
i= cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
res = cv2.matchTemplate(i,template,3)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
x = (top_left[0] + bottom_right[0])/2
y = (top_left[1] + bottom_right[1])/2
#Draw Rectangle Match
if x>0:
p1=open('kn.txt','w')
p1.write('p1')
print "p1"
p1.flush()
p1.close()
cv2.rectangle(frame, top_left, bottom_right, (255, 125, 125), 3)
cv2.putText(frame, "["+ str(x)+","+str(y)+"]", (bottom_right[0]+10,bottom_right[1]+10), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,125,0), 2)
cv2.imshow('cikis',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
cap.release()
break
cv2.destroyAllWindows()
的