ab211f33a5fa22862eaf10fee0464ca6a1e8893b
[platform/upstream/opencv.git] / samples / cpp / chamfer.cpp
1 #include <opencv2/core/utility.hpp>
2 #include "opencv2/imgproc.hpp"
3 #include "opencv2/highgui.hpp"
4 #include "opencv2/contrib.hpp"
5
6 #include <iostream>
7
8 using namespace cv;
9 using namespace std;
10
11 static void help()
12 {
13
14    cout << "\nThis program demonstrates Chamfer matching -- computing a distance between an \n"
15             "edge template and a query edge image.\n"
16             "Usage: \n"
17             "./chamfer <image edge map> <template edge map>,"
18             " By default the inputs are logo_in_clutter.png logo.png\n";
19 }
20
21 const char* keys =
22 {
23     "{@logo1 |logo_in_clutter.png  |image edge map    }"
24     "{@logo2 |logo.png             |template edge map}"
25 };
26
27 int main( int argc, const char** argv )
28 {
29
30     help();
31     CommandLineParser parser(argc, argv, keys);
32
33     string image = parser.get<string>(0);
34     string templ = parser.get<string>(1);
35     Mat img = imread(image.c_str(), 0);
36     Mat tpl = imread(templ.c_str(), 0);
37
38     if (img.empty() || tpl.empty())
39     {
40         cout << "Could not read image file " << image << " or " << templ << "." << endl;
41         return -1;
42     }
43     Mat cimg;
44     cvtColor(img, cimg, COLOR_GRAY2BGR);
45
46     // if the image and the template are not edge maps but normal grayscale images,
47     // you might want to uncomment the lines below to produce the maps. You can also
48     // run Sobel instead of Canny.
49
50     // Canny(img, img, 5, 50, 3);
51     // Canny(tpl, tpl, 5, 50, 3);
52
53     vector<vector<Point> > results;
54     vector<float> costs;
55     int best = chamerMatching( img, tpl, results, costs );
56     if( best < 0 )
57     {
58         cout << "matching not found" << endl;
59         return -1;
60     }
61
62     size_t i, n = results[best].size();
63     for( i = 0; i < n; i++ )
64     {
65         Point pt = results[best][i];
66         if( pt.inside(Rect(0, 0, cimg.cols, cimg.rows)) )
67            cimg.at<Vec3b>(pt) = Vec3b(0, 255, 0);
68     }
69
70     imshow("result", cimg);
71
72     waitKey();
73
74     return 0;
75 }