title: Determine if an image exists within a larger image, and if so, find it, using Python
body: <p>I need a Python program that I am working on to be able to take a small image, determine if it exists inside a larger image, and if so, report its location.  If not, report that.  (In my case, the large image will be a screenshot, and the small image an image that may or may not be on the screen, in an HTML5 canvas.)  Looking on line, I found out about template matching in OpenCV, which does have excellent Python bindings.  I tried the following, based on very similar code I found on line, using numpy as well:</p>
  
  <pre><code>import cv2
  import numpy as np
  image = cv2.imread("screenshot.png")
  template = cv2.imread("button.png")
  result = cv2.matchTemplate(image,template,cv2.TM_CCOEFF_NORMED)
  StartButtonLocation = np.unravel_index(result.argmax(),result.shape)
  </code></pre>
  
  <p>This doesn't do what I need it to do, because it ALWAYS returns a point in the larger image; the point where the match is closest, no matter how terrible a match it is.  I want something that finds an exact, pixel for pixel match of the smaller image in the larger image, and if none exists, raises an exception, or returns <code>False</code>, or something like that.  And, it needs to be fairly quick.  Does anyone have a good idea about how to do this?</p>