1

I am working with template matching but i change picture distance program doesn't work

Codes # -- coding: cp1254 -- import cv2 import numpy as 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()

  • It is quite unclear your question. Do you mean that template matching does not work if the object is moved farther or closer to the camera? If this is the case, it is due to the fact that Template matching does not cope with changed in scale or rotation.... probably another method will work better – api55 Dec 21 '17 at 09:23
  • I am student i teaching opencv and python yes my question is template matching does not work if the object is moved farther or closer to the camera. Sorry for my bad english – Berkan doğan Dec 21 '17 at 09:28
  • 2
    @Berkandoğan As api55 said, template matching is not robust when the object is scaled or rotated. Refer to https://en.wikipedia.org/wiki/Template_matching – Kinght 金 Dec 21 '17 at 09:34

0 Answers0