MatchTemplate_Demo.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. /**
  24. * @function main
  25. */
  26. int main( int argc, char** argv )
  27. {
  28. if (argc < 3)
  29. {
  30. cout << "Not enough parameters" << endl;
  31. cout << "Usage:\n" << argv[0] << " <image_name> <template_name> [<mask_name>]" << endl;
  32. return -1;
  33. }
  34. //! [load_image]
  35. /// Load image and template
  36. img = imread( argv[1], IMREAD_COLOR );
  37. templ = imread( argv[2], IMREAD_COLOR );
  38. if(argc > 3) {
  39. use_mask = true;
  40. mask = imread( argv[3], IMREAD_COLOR );
  41. }
  42. if(img.empty() || templ.empty() || (use_mask && mask.empty()))
  43. {
  44. cout << "Can't read one of the images" << endl;
  45. return EXIT_FAILURE;
  46. }
  47. //! [load_image]
  48. //! [create_windows]
  49. /// Create windows
  50. namedWindow( image_window, WINDOW_AUTOSIZE );
  51. namedWindow( result_window, WINDOW_AUTOSIZE );
  52. //! [create_windows]
  53. //! [create_trackbar]
  54. /// Create Trackbar
  55. 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";
  56. createTrackbar( trackbar_label, image_window, &match_method, max_Trackbar, MatchingMethod );
  57. //! [create_trackbar]
  58. MatchingMethod( 0, 0 );
  59. //! [wait_key]
  60. waitKey(0);
  61. return EXIT_SUCCESS;
  62. //! [wait_key]
  63. }
  64. /**
  65. * @function MatchingMethod
  66. * @brief Trackbar callback
  67. */
  68. void MatchingMethod( int, void* )
  69. {
  70. //! [copy_source]
  71. /// Source image to display
  72. Mat img_display;
  73. img.copyTo( img_display );
  74. //! [copy_source]
  75. //! [create_result_matrix]
  76. /// Create the result matrix
  77. int result_cols = img.cols - templ.cols + 1;
  78. int result_rows = img.rows - templ.rows + 1;
  79. result.create( result_rows, result_cols, CV_32FC1 );
  80. //! [create_result_matrix]
  81. //! [match_template]
  82. /// Do the Matching and Normalize
  83. bool method_accepts_mask = (TM_SQDIFF == match_method || match_method == TM_CCORR_NORMED);
  84. if (use_mask && method_accepts_mask)
  85. { matchTemplate( img, templ, result, match_method, mask); }
  86. else
  87. { matchTemplate( img, templ, result, match_method); }
  88. //! [match_template]
  89. //! [normalize]
  90. normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );
  91. //! [normalize]
  92. //! [best_match]
  93. /// Localizing the best match with minMaxLoc
  94. double minVal; double maxVal; Point minLoc; Point maxLoc;
  95. Point matchLoc;
  96. minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
  97. //! [best_match]
  98. //! [match_loc]
  99. /// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
  100. if( match_method == TM_SQDIFF || match_method == TM_SQDIFF_NORMED )
  101. { matchLoc = minLoc; }
  102. else
  103. { matchLoc = maxLoc; }
  104. //! [match_loc]
  105. //! [imshow]
  106. /// Show me what you got
  107. rectangle( img_display, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );
  108. rectangle( result, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );
  109. imshow( image_window, img_display );
  110. imshow( result_window, result );
  111. //! [imshow]
  112. return;
  113. }