OpenCV Object Detection using Template Matching Methods

Original article was published on Artificial Intelligence on Medium

OpenCV Object Detection using Template Matching Methods

The very basic form of object detection is using the Template Matching process of the OpenCV and can be a good learning point to proceed further. This process generally has two images one is the full image and the other is the subset part of the full image which can be called a template.

The Template Matching method simply scans the complete/full image and slides over the template/subset image across the full image and identifies the matching area as a heatmap. For the sake of discussion let us have a complete image of different vehicles along with ambulance on the road and the template image is part of the complete image containing only an ambulance.

Complete/Full image
Template/subste image

The template matching depends on different correlation metrics. Various matching methods available in OpenCV which can be used for the template matching process. For full documentation refer OpenCV Template Matching Methods.

OpenCV Template Matching methods

Import Required libraries and display the main and template image

import libraries and display the images

Let us have all the 6 template matching methods tested by having the names of the method stored in an array and iterate them using a for loop and apply those methods for template matching and visualize the results. The actual result of this template matching method is a heat map and they require a small bit of code to convert the min-max locations identified in the heat map and draw box around that identified target.

Template matching code

The results of this Template Matching process is as shown in the following image

Template Matching Process result of identifying the ambulance