samples/cpp/tutorial_code/Histograms_Matching/MatchTemplate_Demo.cpp#
An example using Template Matching algorithm
1/**
2 * @file MatchTemplate_Demo.cpp
3 * @brief Sample code to use the function MatchTemplate
4 * @author OpenCV team
5 */
6
7#include "opencv2/imgcodecs.hpp"
8#include "opencv2/highgui.hpp"
9#include "opencv2/imgproc.hpp"
10#include <iostream>
11
12using namespace std;
13using namespace cv;
14
15//! [declare]
16/// Global Variables
17bool use_mask;
18Mat img; Mat templ; Mat mask; Mat result;
19const char* image_window = "Source Image";
20const char* result_window = "Result window";
21
22int match_method;
23int max_Trackbar = 5;
24//! [declare]
25
26/// Function Headers
27void MatchingMethod( int, void* );
28
29const char* keys =
30"{ help h| | Print help message. }"
31"{ @input1 | Template_Matching_Original_Image.jpg | image_name }"
32"{ @input2 | Template_Matching_Template_Image.jpg | template_name }"
33"{ @input3 | | mask_name }";
34
35/**
36 * @function main
37 */
38int main( int argc, char** argv )
39{
40 CommandLineParser parser( argc, argv, keys );
41 samples::addSamplesDataSearchSubDirectory( "doc/tutorials/imgproc/histograms/template_matching/images" );
42
43 //! [load_image]
44 /// Load image and template
45 img = imread( samples::findFile( parser.get<String>("@input1") ) );
46 templ = imread( samples::findFile( parser.get<String>("@input2") ), IMREAD_COLOR );
47
48 if(argc > 3) {
49 use_mask = true;
50 mask = imread(samples::findFile( parser.get<String>("@input3") ), IMREAD_COLOR );
51 }
52
53 if(img.empty() || templ.empty() || (use_mask && mask.empty()))
54 {
55 cout << "Can't read one of the images" << endl;
56 return EXIT_FAILURE;
57 }
58 //! [load_image]
59
60 //! [create_windows]
61 /// Create windows
62 namedWindow( image_window, WINDOW_AUTOSIZE );
63 namedWindow( result_window, WINDOW_AUTOSIZE );
64 //! [create_windows]
65
66 //! [create_trackbar]
67 /// Create Trackbar
68 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";
69 createTrackbar( trackbar_label, image_window, &match_method, max_Trackbar, MatchingMethod );
70 //! [create_trackbar]
71
72 MatchingMethod( 0, 0 );
73
74 //! [wait_key]
75 waitKey(0);
76 return EXIT_SUCCESS;
77 //! [wait_key]
78}
79
80/**
81 * @function MatchingMethod
82 * @brief Trackbar callback
83 */
84void MatchingMethod( int, void* )
85{
86 //! [copy_source]
87 /// Source image to display
88 Mat img_display;
89 img.copyTo( img_display );
90 //! [copy_source]
91
92 //! [create_result_matrix]
93 /// Create the result matrix
94 int result_cols = img.cols - templ.cols + 1;
95 int result_rows = img.rows - templ.rows + 1;
96
97 result.create( result_rows, result_cols, CV_32FC1 );
98 //! [create_result_matrix]
99
100 //! [match_template]
101 /// Do the Matching and Normalize
102 bool method_accepts_mask = (TM_SQDIFF == match_method || match_method == TM_CCORR_NORMED);
103 if (use_mask && method_accepts_mask)
104 { matchTemplate( img, templ, result, match_method, mask); }
105 else
106 { matchTemplate( img, templ, result, match_method); }
107 //! [match_template]
108
109 //! [normalize]
110 normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );
111 //! [normalize]
112
113 //! [best_match]
114 /// Localizing the best match with minMaxLoc
115 double minVal; double maxVal; Point minLoc; Point maxLoc;
116 Point matchLoc;
117
118 minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
119 //! [best_match]
120
121 //! [match_loc]
122 /// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
123 if( match_method == TM_SQDIFF || match_method == TM_SQDIFF_NORMED )
124 { matchLoc = minLoc; }
125 else
126 { matchLoc = maxLoc; }
127 //! [match_loc]
128
129 //! [imshow]
130 /// Show me what you got
131 rectangle( img_display, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );
132 rectangle( result, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );
133
134 imshow( image_window, img_display );
135 imshow( result_window, result );
136 //! [imshow]
137
138 return;
139}