reverted samples with new command argument parser. will be continued after OpenCV...
[profile/ivi/opencv.git] / samples / cpp / chamfer.cpp
1 #include "opencv2/imgproc/imgproc.hpp"
2 #include "opencv2/highgui/highgui.hpp"
3 #include "opencv2/contrib/contrib.hpp"
4
5 #include <iostream>
6
7 using namespace cv;
8 using namespace std;
9
10 void help()
11 {
12         cout <<
13                         "\nThis program demonstrates Chamfer matching -- computing a distance between an \n"
14                         "edge template and a query edge image.\n"
15                         "Call:\n"
16                         "./chamfer [<image edge map> <template edge map>]\n"
17                         "By default\n"
18                         "the inputs are ./chamfer logo_in_clutter.png logo.png\n"<< endl;
19 }
20 int main( int argc, char** argv )
21 {
22     if( argc != 1 && argc != 3 )
23     {
24         help();
25         return 0;
26     }
27     Mat img = imread(argc == 3 ? argv[1] : "logo_in_clutter.png", 0);
28     Mat cimg;
29     cvtColor(img, cimg, CV_GRAY2BGR);
30     Mat tpl = imread(argc == 3 ? argv[2] : "logo.png", 0);
31     
32     // if the image and the template are not edge maps but normal grayscale images,
33     // you might want to uncomment the lines below to produce the maps. You can also
34     // run Sobel instead of Canny.
35     
36     // Canny(img, img, 5, 50, 3);
37     // Canny(tpl, tpl, 5, 50, 3);
38     
39     vector<vector<Point> > results;
40     vector<float> costs;
41     int best = chamerMatching( img, tpl, results, costs );
42     if( best < 0 )
43     {
44         cout << "not found;\n";
45         return 0;
46     }
47     
48     size_t i, n = results[best].size();
49     for( i = 0; i < n; i++ )
50     {
51         Point pt = results[best][i];
52         if( pt.inside(Rect(0, 0, cimg.cols, cimg.rows)) )
53            cimg.at<Vec3b>(pt) = Vec3b(0, 255, 0);
54     }
55     imshow("result", cimg);
56     waitKey();
57     return 0;
58 }