OpenCV
Open Source Computer Vision
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Template Matching

Prev Tutorial: Back Projection
Next Tutorial: Finding contours in your image

Original author Ana Huamán
Compatibility OpenCV >= 3.0

Goal

In this tutorial you will learn how to:

  • Use the OpenCV function matchTemplate() to search for matches between an image patch and an input image
  • Use the OpenCV function minMaxLoc() to find the maximum and minimum values (as well as their positions) in a given array.

Theory

What is template matching?

Template matching is a technique for finding areas of an image that match (are similar) to a template image (patch).

While the patch must be a rectangle it may be that not all of the rectangle is relevant. In such a case, a mask can be used to isolate the portion of the patch that should be used to find the match.

How does it work?

  • We need two primary components:

    1. Source image (I): The image in which we expect to find a match to the template image
    2. Template image (T): The patch image which will be compared to the source image

    our goal is to detect the highest matching area:

  • To identify the matching area, we have to compare the template image against the source image by sliding it:
  • By sliding, we mean moving the patch one pixel at a time (left to right, up to down). At each location, a metric is calculated so it represents how "good" or "bad" the match at that location is (or how similar the patch is to that particular area of the source image).
  • For each location of T over I, you store the metric in the result matrix R. Each location (x,y) in R contains the match metric:

the image above is the result R of sliding the patch with a metric TM_CCORR_NORMED. The brightest locations indicate the highest matches. As you can see, the location marked by the red circle is probably the one with the highest value, so that location (the rectangle formed by that point as a corner and width and height equal to the patch image) is considered the match.

  • In practice, we locate the highest value (or lower, depending of the type of matching method) in the R matrix, using the function minMaxLoc()

How does the mask work?

  • If masking is needed for the match, three components are required:
    1. Source image (I): The image in which we expect to find a match to the template image
    2. Template image (T): The patch image which will be compared to the source image
    3. Mask image (M): The mask, a grayscale image that masks the template
  • Only two matching methods currently accept a mask: TM_SQDIFF and TM_CCORR_NORMED (see below for explanation of all the matching methods available in opencv).
  • The mask must have the same dimensions as the template
  • The mask should have a CV_8U or CV_32F depth and the same number of channels as the template image. In CV_8U case, the mask values are treated as binary, i.e. zero and non-zero. In CV_32F case, the values should fall into [0..1] range and the template pixels will be multiplied by the corresponding mask pixel values. Since the input images in the sample have the CV_8UC3 type, the mask is also read as color image.

Which are the matching methods available in OpenCV?

Good question. OpenCV implements Template matching in the function matchTemplate(). The available methods are 6:

  1. method=TM_SQDIFF

    R(x,y)=x,y(T(x,y)I(x+x,y+y))2

  2. method=TM_SQDIFF_NORMED

    R(x,y)=x,y(T(x,y)I(x+x,y+y))2x,yT(x,y)2x,yI(x+x,y+y)2

  3. method=TM_CCORR

    R(x,y)=x,y(T(x,y)I(x+x,y+y))

  4. method=TM_CCORR_NORMED

    R(x,y)=x,y(T(x,y)I(x+x,y+y))x,yT(x,y)2x,yI(x+x,y+y)2

  5. method=TM_CCOEFF

    R(x,y)=x,y(T(x,y)I(x+x,y+y))

    where

    T(x,y)=T(x,y)1/(wh)x,yT(x,y)I(x+x,y+y)=I(x+x,y+y)1/(wh)x,yI(x+x,y+y)

  6. method=TM_CCOEFF_NORMED

    R(x,y)=x,y(T(x,y)I(x+x,y+y))x,yT(x,y)2x,yI(x+x,y+y)2

Code

  • What does this program do?
    • Loads an input image, an image patch (template), and optionally a mask
    • Perform a template matching procedure by using the OpenCV function matchTemplate() with any of the 6 matching methods described before. The user can choose the method by entering its selection in the Trackbar. If a mask is supplied, it will only be used for the methods that support masking
    • Normalize the output of the matching procedure
    • Localize the location with higher matching probability
    • Draw a rectangle around the area corresponding to the highest match
  • Downloadable code: Click here
  • Code at glance:
    #include <iostream>
    using namespace std;
    using namespace cv;
    bool use_mask;
    Mat img; Mat templ; Mat mask; Mat result;
    const char* image_window = "Source Image";
    const char* result_window = "Result window";
    int match_method;
    int max_Trackbar = 5;
    void MatchingMethod( int, void* );
    const char* keys =
    "{ help h| | Print help message. }"
    "{ @input1 | Template_Matching_Original_Image.jpg | image_name }"
    "{ @input2 | Template_Matching_Template_Image.jpg | template_name }"
    "{ @input3 | | mask_name }";
    int main( int argc, char** argv )
    {
    CommandLineParser parser( argc, argv, keys );
    samples::addSamplesDataSearchSubDirectory( "doc/tutorials/imgproc/histograms/template_matching/images" );
    img = imread( samples::findFile( parser.get<String>("@input1") ) );
    templ = imread( samples::findFile( parser.get<String>("@input2") ), IMREAD_COLOR );
    if(argc > 3) {
    use_mask = true;
    mask = imread(samples::findFile( parser.get<String>("@input3") ), IMREAD_COLOR );
    }
    if(img.empty() || templ.empty() || (use_mask && mask.empty()))
    {
    cout << "Can't read one of the images" << endl;
    return EXIT_FAILURE;
    }
    namedWindow( image_window, WINDOW_AUTOSIZE );
    namedWindow( result_window, WINDOW_AUTOSIZE );
    const char* trackbar_label = "Method: \n 0: SQDIFF \n 1: SQDIFF NORMED \n 2: TM CCORR \n 3: TM CCORR NORMED \n 4: TM COEFF \n 5: TM COEFF NORMED";
    createTrackbar( trackbar_label, image_window, &match_method, max_Trackbar, MatchingMethod );
    MatchingMethod( 0, 0 );
    waitKey(0);
    return EXIT_SUCCESS;
    }
    void MatchingMethod( int, void* )
    {
    Mat img_display;
    img.copyTo( img_display );
    int result_cols = img.cols - templ.cols + 1;
    int result_rows = img.rows - templ.rows + 1;
    result.create( result_rows, result_cols, CV_32FC1 );
    bool method_accepts_mask = (TM_SQDIFF == match_method || match_method == TM_CCORR_NORMED);
    if (use_mask && method_accepts_mask)
    { matchTemplate( img, templ, result, match_method, mask); }
    else
    { matchTemplate( img, templ, result, match_method); }
    normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );
    double minVal; double maxVal; Point minLoc; Point maxLoc;
    Point matchLoc;
    minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
    if( match_method == TM_SQDIFF || match_method == TM_SQDIFF_NORMED )
    { matchLoc = minLoc; }
    else
    { matchLoc = maxLoc; }
    rectangle( img_display, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );
    rectangle( result, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );
    imshow( image_window, img_display );
    imshow( result_window, result );
    return;
    }
    Designed for command line parsing.
    Definition utility.hpp:890
    n-dimensional dense array class
    Definition mat.hpp:829
    void copyTo(OutputArray m) const
    Copies the matrix to another one.
    void create(int rows, int cols, int type)
    Allocates new array data if needed.
    int cols
    Definition mat.hpp:2155
    bool empty() const
    Returns true if the array has no elements.
    int rows
    the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
    Definition mat.hpp:2155
    _Tp y
    y coordinate of the point
    Definition types.hpp:202
    _Tp x
    x coordinate of the point
    Definition types.hpp:201
    void normalize(InputArray src, InputOutputArray dst, double alpha=1, double beta=0, int norm_type=NORM_L2, int dtype=-1, InputArray mask=noArray())
    Normalizes the norm or value range of an array.
    void minMaxLoc(InputArray src, double *minVal, double *maxVal=0, Point *minLoc=0, Point *maxLoc=0, InputArray mask=noArray())
    Finds the global minimum and maximum in an array.
    std::string String
    Definition cvstd.hpp:151
    #define CV_32FC1
    Definition interface.h:118
    void imshow(const String &winname, InputArray mat)
    Displays an image in the specified window.
    void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
    Draws a simple, thick, or filled up-right rectangle.
    void matchTemplate(InputArray image, InputArray templ, OutputArray result, int method, InputArray mask=noArray())
    Compares a template against overlapped image regions.
    int main(int argc, char *argv[])
    Definition highgui_qt.cpp:3
    Definition core.hpp:107
    STL namespace.

Explanation

  • Declare some global variables, such as the image, template and result matrices, as well as the match method and the window names:

    bool use_mask;
    Mat img; Mat templ; Mat mask; Mat result;
    const char* image_window = "Source Image";
    const char* result_window = "Result window";
    int match_method;
    int max_Trackbar = 5;
  • Load the source image, template, and optionally, if supported for the matching method, a mask:

    img = imread( samples::findFile( parser.get<String>("@input1") ) );
    templ = imread( samples::findFile( parser.get<String>("@input2") ), IMREAD_COLOR );
    if(argc > 3) {
    use_mask = true;
    mask = imread(samples::findFile( parser.get<String>("@input3") ), IMREAD_COLOR );
    }
    if(img.empty() || templ.empty() || (use_mask && mask.empty()))
    {
    cout << "Can't read one of the images" << endl;
    return EXIT_FAILURE;
    }
  • Create the Trackbar to enter the kind of matching method to be used. When a change is detected the callback function is called.

    const char* trackbar_label = "Method: \n 0: SQDIFF \n 1: SQDIFF NORMED \n 2: TM CCORR \n 3: TM CCORR NORMED \n 4: TM COEFF \n 5: TM COEFF NORMED";
    createTrackbar( trackbar_label, image_window, &match_method, max_Trackbar, MatchingMethod );
  • Let's check out the callback function. First, it makes a copy of the source image:

    Mat img_display;
    img.copyTo( img_display );
  • Perform the template matching operation. The arguments are naturally the input image I, the template T, the result R and the match_method (given by the Trackbar), and optionally the mask image M.

    bool method_accepts_mask = (TM_SQDIFF == match_method || match_method == TM_CCORR_NORMED);
    if (use_mask && method_accepts_mask)
    { matchTemplate( img, templ, result, match_method, mask); }
    else
    { matchTemplate( img, templ, result, match_method); }
  • We normalize the results:

    normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );
  • We localize the minimum and maximum values in the result matrix R by using minMaxLoc().

    double minVal; double maxVal; Point minLoc; Point maxLoc;
    Point matchLoc;
    minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
  • For the first two methods ( TM_SQDIFF and MT_SQDIFF_NORMED ) the best match are the lowest values. For all the others, higher values represent better matches. So, we save the corresponding value in the matchLoc variable:

    if( match_method == TM_SQDIFF || match_method == TM_SQDIFF_NORMED )
    { matchLoc = minLoc; }
    else
    { matchLoc = maxLoc; }
  • Display the source image and the result matrix. Draw a rectangle around the highest possible matching area:

    rectangle( img_display, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );
    rectangle( result, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );
    imshow( image_window, img_display );
    imshow( result_window, result );

Results

  1. Testing our program with an input image such as:

and a template image:

  1. Generate the following result matrices (first row are the standard methods SQDIFF, CCORR and CCOEFF, second row are the same methods in its normalized version). In the first column, the darkest is the better match, for the other two columns, the brighter a location, the higher the match.

  1. The right match is shown below (black rectangle around the face of the guy at the right). Notice that CCORR and CCDEFF gave erroneous best matches, however their normalized version did it right, this may be due to the fact that we are only considering the "highest match" and not the other possible high matches.