Template matching with cross-correlation between the image and template

In this example, we'll use cross-correlation with the eye template image (using a kernel with the image for cross-correlation) and the location of the eye in the raccoon-face image can be found as follows:

face_image = misc.face(gray=True) - misc.face(gray=True).mean()template_image = np.copy(face_image[300:365, 670:750]) # right eyetemplate_image -= template_image.mean()face_image = face_image + np.random.randn(*face_image.shape) * 50 # add random noisecorrelation = signal.correlate2d(face_image, template_image, boundary='symm', mode='same')y, x = np.unravel_index(np.argmax(correlation), correlation.shape) # find the matchfig, (ax_original, ax_template, ax_correlation) ...

Get Hands-On Image Processing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.