Life2Coding
How to Do Template Matching using OpenCV in Python

This post will be helpful in learning OpenCV using Python programming. Here I will show how to implement OpenCV functions and apply it in various aspects using some examples. Then the output will be shown with some comparisons as well.

Requirements:

First, you need to setup your Python Environment with OpenCV. You can easily do it by following Life2Coding’s tutorial on YouTube: Linking OpenCV 3 with Python 3

Goals:

In this tutorial, I will show you how to match template with original images and find the exact match using OpenCV and Python coding.

Documentation:

Python: cv2.matchTemplate(image, templ, method[, result]) → result

Compares a template against overlapped image regions.

Parameters:          

  • image – Image where the search is running. It must be 8-bit or 32-bit floating-point.
  • templ – Searched template. It must be not greater than the source image and have the same data type.
  • result – Map of comparison results. It must be single-channel 32-bit floating-point. If image is W x H and templ is w x h, then result is (W-w+1) x (H-h+1)
  • method – Parameter specifying the comparison method.

There are six matching methods: CV_TM_SQDIFF, CV_TM_SQDIFF_NORMED, CV_TM_CCORR, CV_TM_CCORR_NORMED, CV_TM_CCOEFF and CV_TM_CCOEFF_NORMED.

Steps:

  • Load the Original and Template images using cv2.imread()
  • Converting the both images to gray using cv2.cvtColor()
  • Match the template using cv2.matchTemplate()
  • Draw boundary around the face using cv2.rectangle()
  • Display both images using cv2.imshow()
  • Wait for keyboard button press using cv2.waitKey()
  • Exit window and destroy all windows using cv2.destroyAllWindows()

Example Code:

import cv2

def templateMatch(originalImagePath, templatePath):

    # Read original and template image
    original_image = cv2.imread(originalImagePath)
    templateOri = cv2.imread(templatePath)

    # Convert to grayscale
    imageGray = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
    templateGray = cv2.cvtColor(templateOri,cv2.COLOR_BGR2GRAY)

    # assign width and height of template in w and h
    h, w = templateGray.shape
    # Perform match operations.
    res = cv2.matchTemplate(imageGray, templateGray, cv2.TM_CCOEFF_NORMED)

    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)
    cv2.rectangle(original_image, top_left, bottom_right, (222, 100, 35), 4)

    cv2.imshow('Template',templateOri)
    cv2.imshow('Detected Template', original_image)
    cv2.waitKey(0)

def main():
    originalImagePath,templatePath='oriImage.jpg','template.jpg'
    templateMatch(originalImagePath,templatePath)
    cv2.destroyAllWindows()


if __name__ == "__main__":
    main()

Output:

template3 How to Do Template Matching using OpenCV in Python

life2coding_icon [] How to Do Template Matching using OpenCV in Python

2 thoughts on “How to Do Template Matching using OpenCV in Python

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.