OpenCV Template Matching
Template matching is a technique that is used to find the location of template images in a larger image. OpenCV provides the cv2.matchTemplates() function for this purpose. It simply slides the template images over the input image and compares the templates and patch under the input image.
There are various methods available for the comparison; we will discuss a few popular methods in further topics.
It returns a grayscale image, where every pixel represents the number of the neighborhood of that pixel match with the input templates.
Template matching in OpenCV
The templates matching consist of the following step:
Step – 1: Take the actual image and convert it into a grayscale image.
Step – 2: Select the template as a grayscale image.
Step – 3: Find the location where the accuracy level matches. It is done by template image slide over the actual image.
Step – 4: When the result is greater than the accuracy level, mark that position as detected.
Consider the following example:
Output:
Template Matching with Multiple Objects
In the above example, we searched image for template image that occurred only once in the image. Suppose a particular object that occur multiple times in particular image. In this scenario, we will use the thresholding because cv2.minMaxLoc() won’t give all location of template image. Consider the following example.
Output:
In the above program, we took an image of popular super Mario game as main image and coin image as template image. The coins occur multiple times in main image. When it find the coin in the image it draw rectangle on the coin.
Limitation of Templates Matching
There are few limitations in template matching given as follows:
- It is a time-consuming process to calculate the pattern correlation image for medium to large images.
- Pattern occurrence has to preserve the orientation of the reference template image.a
- Template matching doesn’t apply on the rotated or scaled version of the template as a change in shape/size/shear etc.