Introduction to OpenCV matchTemplate
The following article provides an outline for OpenCV matchTemplate. OpenCV is a machine learning algorithm which helps in processing images. It is a library in Python which is used for analytical purposes of images. It is used for solving problems in Computer Vision. Of all the different functions this library provides matchTemplate() function is used for matching different template of images. This means it searches for matches between different image patches and the input image which will be provided. It searches and finds the location of any template image into a larger image. This function will take the underlying image and simply slide the template image over the input image. It will then patch the input image with the template image.
The syntax for using matchTemplate is as below.
To see if the image matches or not, we have different types of functions in matchTemplate.
res = cv.matchTemplate(img,template,method)
The above res variable is the result variable where the result of the image will be stored. We then have the keyword for matchTemplate.
The parameters passed to this image are:
- img: This will store the input which needs to be matched with a template image.
- template: This will store the template image, which will show us the base of comparison.
- method: There are different methods which can be used needs to be passed here.
How matchTemplate Function Works in OpenCV?
OpenCV library in Python is used for matching images and finding the similarity in the two images keeping one image as a base. These images can be matched and used for further analysis. The working of this function is in a way where it simply slides template image over the image, which is passed as an input. This image will be a 2D image which will be a convolutional image. It will then compare the template with the input image in the form of patches. The input image is then placed under the template image. Once the template image is compared with the input image, then the end result will be a grayscale image. This grayscale image will denote how much the neighborhood of the pixel will match with the template image.
The methods that can be passed as an argument to the function are as below:
- SQDIFF: Squared Difference
- SqDiffNormed: Normalized Squared Difference
- CCorr: Cross Correlation
- CCorrNormed: Normalized Cross-Correlation
- CCoeff: Cosine Coefficient
- CCoeffNormed: Normalized Cosine Coefficient
These methods help in matching the images as needed. The template image will be getting a slide across the input image. Then the metric will be calculated for every location of pixel present. This calculated metric will show the similarity index between the template and the particular input image. For every location of pixel P in the image I, the metric will be getting stored in the result R. Each location in the result will have the matching metrics. This process will be similar to convolution, where the output of the given input image shrinks.
The above figure represents different formulas used to match the images and get a matching metric between template and input image.
Example of OpenCV matchTemplate
Given below is the example of OpenCV matchTemplate:
import cv2 as cv
import numpy as np
from matplotlib import pyplot as pltimg
image_rgb = cv.imread('edulogo.png',0)
image_gray = img.copy()
img_template = cv.imread('edulogo.png',0)
#We store the width and height of the template image
w, h = img_template.shape[::-1] # We will use all six methods in order to check how they work
methods = ['cv.TM_CCOEFF', 'cv.TM_CCOEFF_NORMED', 'cv.TM_CCORR',
'cv.TM_CCORR_NORMED', 'cv.TM_SQDIFF', 'cv.TM_SQDIFF_NORMED'] for match_method in methods:
image = image_gray.copy()
method = eval(match_method)
# Here we will perform the match operation with template image
res = cv.matchTemplate(image_rgb,img_template,method)
minval, maxval, minloc, maxloc = cv.minMaxLoc(res)
# When we use method as TM_SQDIFF or TM_SQDIFF_NORMED then take minimum of the metric value
if method in [cv.TM_SQDIFF, cv.TM_SQDIFF_NORMED]:
topleft = minloc
topleft = maxloc
btm_right = (topleft + w, topleft + h)
cv.rectangle(image_rgb,top_left, btm_right, 255, 2)
pltimg.subplot(121),pltimg.imshow(res,cmap = 'gray')
pltimg.title('Result that matches'), pltimg.xticks(), pltimg.yticks()
pltimg.subplot(122),pltimg.imshow(image_rgb,cmap = 'gray')
pltimg.title('Detection Point of image'), pltimg.xticks(), pltimg.yticks()
Here we will import the cv library, which is the OpenCV library. We also import the numpy library and matplotlob library. We then read the image by using the imread function. This will be the input image which will be getting matched. The second image, which will be a template image, is stored in the temp variable. After that, we store all the methods that we want to use in the ‘methods’ variable. We will try all six methods mentioned above. We then have user-defined function meth to evaluate and apply matchTemplate function. We store minimum, maximum values and location of pixels and take minimum values for SQDIFF and SQDIFF_NORMED functions. We take pixel values of min_loc and max_loc.
Once this is done, we are storing the top left values with width and height. We then change the pixel to a rectangle and change the image to grayscale. We then plot these images using matplotlib. We show the match frequency along with the detection point of the match.
It can be observed from the above output that we get six results. As per the above formulas, the squared difference and normalized squared difference is taken. We also have cross-correlation and normalized cross-correlation. The cosine and normalized cosine coefficient image are also matched and stored. The images show the pixels where there is a match and also the detection point in the image. All the images are store in grayscale format. Due to which you can observe that black is the pixel value where there was a match found; it can be said that the patch was matching.
The OpenCV library has many image processing functions which help in analysing and getting information from images. The matchTemplate function helps in comparing two images where one image is a base for comparison while the other will be the input. By matching different patches from the images, you can find a match and compare the two images. This function is hence helpful in detecting similarities in images. The methods also help in processing the images in different ways and provide accurate results.
This is a guide to OpenCV matchTemplate. Here we discuss the introduction, how matchTemplate function works in OpenCV? and example, respectively. You may also have a look at the following articles to learn more –