cv2.matchTemplate(): Object Detection From Image using Python OpenCV – Python OpenCV Tutorial

By | October 20, 2020

We have introduced six object detection algorithms in opencv.

Understand OpenCV Template Matching Algorithm: A Completed Guide

In this tutorial, we will use these six algorithms to detect object from an image.

How to detect object from images in python opencv?

The simplest way is to use opencv cv2.matchTemplate() function to detect object.

This function is defined as:

cv2.matchTemplate(img, template, method)


img is source image, the data type is numpy ndarray.

template is the object image, the data type is numpy ndarray.

method is the object detection algorithm

This function can tell you wether or where template is in img. It will return a numpy ndarray, which is the result computed by method based on img and template.

How to use cv2.matchTemplate() to detect object?

In this tutorial, we will use an example to show you how to do.

First, we should read an source image and template image.

import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('comment_source.jpg', cv2.IMREAD_COLOR)
img2 = img.copy()
template = cv2.imread('comment_target_2.jpg', cv2.IMREAD_COLOR)
w = template.shape[0]
h = template.shape[1]

The source image and template image are:

Source image

cv2.matchTemplate() source image

Template image

cv2.matchTemplate() template image

There are some methods to read an image in python opencv, you can refer:

Python OpenCV Read an Image to NumPy NdArray: A Beginner Guide

Then we can use cv2.matchTemplate() to detect object.

methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
           'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']

methods contains six algorithms we will use, we will test the effect of each algorithm.

for meth in methods:
    img = img2.copy()
    method = eval(meth)
    #use each algorithm
    res = cv2.matchTemplate(img, template, method)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
        top_left = min_loc
        top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)
    cv2.rectangle(img, top_left, bottom_right, 255, 2)

We will use cv2.minMaxLoc() to find the object position. In order to understand cv2.minMaxLoc(), you can refer:

Learn Python OpenCV cv2.minMaxLoc() by Examples – OpenCV Tutorial

Meanwhile, we can use cv2.rectangle() to draw the object.

Finally, we can use the matplotlib to show the effect of each algorithm.

    plt.title('Matching Result'),
    plt.title('Detected Point'),

In order to understand how to show image using python matplotlib, you can view:

Understand matplotlib.pyplot.imshow(): Display Data as an Image – Matplotlib Tutorial

Run this code, we will get these effect:


The effect of cv2.matchTemplate() cv2.TM_CCOEFF


The effect of cv2.matchTemplate() cv2.TM_CCOEFF_NORMED


The effect of cv2.matchTemplate() cv2.TM_CCORR

We can find cv2.TM_CCORR does not match the object.


The effect of cv2.matchTemplate() cv2.TM_CCORR_NORMED


The effect of cv2.matchTemplate() cv2.TM_SQDIFF


The effect of cv2.matchTemplate() cv2.TM_SQDIFF_NORMED

From example we can find: we only match one object from source image, how to match multiple objects? We will discuss in next tutorial.