|
| 1 | +// Example 13-3. Template matching |
| 2 | + |
| 3 | +#include <opencv2/opencv.hpp> |
| 4 | +#include <iostream> |
| 5 | + |
| 6 | +using namespace std; |
| 7 | + |
| 8 | +void help( char** argv ){ |
| 9 | + cout << "\n" |
| 10 | + <<"Example of using matchTemplate(). The call is:\n" |
| 11 | + <<"\n" |
| 12 | + <<argv[0] <<" template image_to_be_searched\n" |
| 13 | + <<"\n" |
| 14 | + <<" This routine will search using all methods:\n" |
| 15 | + <<" cv::TM_SQDIFF 0\n" |
| 16 | + <<" cv::TM_SQDIFF_NORMED 1\n" |
| 17 | + <<" cv::TM_CCORR 2\n" |
| 18 | + <<" cv::TM_CCORR_NORMED 3\n" |
| 19 | + <<" cv::TM_CCOEFF 4\n" |
| 20 | + <<" cv::TM_CCOEFF_NORMED 5\n" |
| 21 | + <<"\n"; |
| 22 | +} |
| 23 | + |
| 24 | +// Display the results of the matches |
| 25 | +// |
| 26 | +int main( int argc, char** argv ) { |
| 27 | + |
| 28 | + if( argc != 3) { |
| 29 | + help( argv ); |
| 30 | + return -1; |
| 31 | + } |
| 32 | + |
| 33 | + cv::Mat src, templ, ftmp[6]; // ftmp is what to display on |
| 34 | + |
| 35 | + // Read in the template to be used for matching: |
| 36 | + // |
| 37 | + if((templ=cv::imread(argv[1], 1)).empty()) { |
| 38 | + cout << "Error on reading template " << argv[1] << endl; |
| 39 | + help( argv );return -1; |
| 40 | + } |
| 41 | + |
| 42 | + // Read in the source image to be searched: |
| 43 | + // |
| 44 | + if((src=cv::imread(argv[2], 1)).empty()) { |
| 45 | + cout << "Error on reading src image " << argv[2] << endl; |
| 46 | + help( argv );return -1; |
| 47 | + } |
| 48 | + |
| 49 | + // Do the matching of the template with the image |
| 50 | + for(int i=0; i<6; ++i){ |
| 51 | + cv::matchTemplate( src, templ, ftmp[i], i); |
| 52 | + cv::normalize(ftmp[i],ftmp[i],1,0,cv::NORM_MINMAX); |
| 53 | + } |
| 54 | + |
| 55 | + // Display |
| 56 | + // |
| 57 | + cv::imshow( "Template", templ ); |
| 58 | + cv::imshow( "Image", src ); |
| 59 | + cv::imshow("SQDIFF", ftmp[0] ); |
| 60 | + cv::imshow("SQDIFF_NORMED", ftmp[1] ); |
| 61 | + cv::imshow("CCORR", ftmp[2] ); |
| 62 | + cv::imshow("CCORR_NORMED", ftmp[3] ); |
| 63 | + cv::imshow("CCOEFF", ftmp[4] ); |
| 64 | + cv::imshow("CCOEFF_NORMED", ftmp[5] ); |
| 65 | + |
| 66 | + // Let user view results: |
| 67 | + // |
| 68 | + cv::waitKey(0); |
| 69 | +} |
| 70 | + |
0 commit comments