python logo


Tag: template matching

template matching python from scratch

Template matching is a technique for finding areas of an image that are similar to a patch (template). Its application may be robotics or manufacturing.

Related course:
Master Computer Vision with OpenCV

Introduction
A patch is a small image with certain features. The goal of template matching is to find the patch/template in an image.

template matching opencv Template matching with OpenCV and Python. Template (left), result image (right)

Download Code

To find them we need both:


  • Source Image (S) : The space to find the matches in

  • Template Image (T) : The template image


The template image T is slided over the source image S (moved over the source image), and the program tries to find matches using statistics.

Template matching example


Lets have a look at the code:

import numpy as np
import cv2

image = cv2.imread('photo.jpg')
template = cv2.imread('template.jpg')

# resize images
image = cv2.resize(image, (0,0), fx=0.5, fy=0.5)
template = cv2.resize(template, (0,0), fx=0.5, fy=0.5)

# Convert to grayscale
imageGray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
templateGray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)

# Find template
result = cv2.matchTemplate(imageGray,templateGray, cv2.TM_CCOEFF)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
top_left = max_loc
h,w = templateGray.shape
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(image,top_left, bottom_right,(0,0,255),4)

# Show result
cv2.imshow("Template", template)
cv2.imshow("Result", image)

cv2.moveWindow("Template", 10, 50);
cv2.moveWindow("Result", 150, 50);

cv2.waitKey(0)

Related course:
Master Computer Vision with OpenCV

Explanation


First we load both the source image and template image with imread().  We resize themand convert them to grayscale for faster detection:


image = cv2.imread('photo.jpg')
template = cv2.imread('template.jpg')
image = cv2.resize(image, (0,0), fx=0.5, fy=0.5)
template = cv2.resize(template, (0,0), fx=0.5, fy=0.5)
imageGray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
templateGray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)

We use the cv2.matchTemplate(image,template,method) method to find the most similar area in the image.  The third argument is the statistical method.

Template Matching Pick the right statistical method for your application. TM_CCOEFF (right), TM_SQDIFF(left)

This method has six matching methods: CV_TM_SQDIFF, CV_TM_SQDIFF_NORMED, CV_TM_CCORR, CV_TM_CCORR_NORMED, CV_TM_CCOEFF and CV_TM_CCOEFF_NORMED.
which are simply different statistical comparison methods

Finally, we get the rectangle variables and display the image.

Limitations


Template matching is not scale invariant nor is it rotation invariant. It is a very basic and straightforward method where we find the most correlating area. Thus, this method of object detection depends on the kind of application you want to build. For non scale and rotation changing input, this method works great.

You may like: Robotics or Car tracking with cascades.

Download Computer Vision Examples + Course