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:
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.
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: