title: How to do the template matching in real time
body: <p>I'm new to OpenCV. I'm trying to do the template matching in real time. But it seems the program I did is not functional as I expected. I coded the program as follow. </p>
  
  <pre><code>import cv2
  import numpy as np
  
  template = cv2.imread('scratch_test.png',0)
  w, h = template.shape[::-1]
  video_capture = cv2.VideoCapture(0)
  
  while True:
      ret, frame = video_capture.read()
  
      gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
      if gray.shape[0]&gt;template.shape[0] and gray.shape[1]&gt;template.shape[1]:
          res = cv2.matchTemplate(gray,template,cv2.TM_CCOEFF_NORMED)
          threshold = 0.8
          loc = np.where( res &gt;= threshold)
          for pt in zip(*loc[::-1]):
              cv2.rectangle(frame, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
          cv2.imshow('orginal', frame)
          if cv2.waitKey(30) &amp; 0xFF == ord('q'):
              break
      else:
          print "Error : Template and image size are: ", template.shape, gray.shape
  </code></pre>
  
  <p>How can I solve this problem</p>