MatchTemplate_Demo.cpp 3.8 KB

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