chamfer.cpp 1.88 KB
Newer Older
1 2 3 4
#include <opencv2/core/utility.hpp>
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/contrib.hpp"
5

6 7
#include <iostream>

8 9 10
using namespace cv;
using namespace std;

11
static void help()
Gary Bradski's avatar
Gary Bradski committed
12
{
13

14 15 16 17 18
   cout << "\nThis program demonstrates Chamfer matching -- computing a distance between an \n"
            "edge template and a query edge image.\n"
            "Usage: \n"
            "./chamfer <image edge map> <template edge map>,"
            " By default the inputs are logo_in_clutter.png logo.png\n";
Gary Bradski's avatar
Gary Bradski committed
19
}
20

21
const char* keys =
22
{
23 24
    "{@logo1 |logo_in_clutter.png  |image edge map    }"
    "{@logo2 |logo.png             |template edge map}"
25
};
26

27
int main( int argc, const char** argv )
28
{
29

30 31
    help();
    CommandLineParser parser(argc, argv, keys);
32

33 34
    string image = parser.get<string>(0);
    string templ = parser.get<string>(1);
35 36
    Mat img = imread(image.c_str(), 0);
    Mat tpl = imread(templ.c_str(), 0);
37

38
    if (img.empty() || tpl.empty())
39 40 41 42
    {
        cout << "Could not read image file " << image << " or " << templ << "." << endl;
        return -1;
    }
43 44
    Mat cimg;
    cvtColor(img, cimg, CV_GRAY2BGR);
45

46 47 48
    // if the image and the template are not edge maps but normal grayscale images,
    // you might want to uncomment the lines below to produce the maps. You can also
    // run Sobel instead of Canny.
49

50 51
    // Canny(img, img, 5, 50, 3);
    // Canny(tpl, tpl, 5, 50, 3);
52

53 54 55 56 57
    vector<vector<Point> > results;
    vector<float> costs;
    int best = chamerMatching( img, tpl, results, costs );
    if( best < 0 )
    {
58
        cout << "matching not found" << endl;
59
        return -1;
60
    }
61

62 63 64 65 66 67 68
    size_t i, n = results[best].size();
    for( i = 0; i < n; i++ )
    {
        Point pt = results[best][i];
        if( pt.inside(Rect(0, 0, cimg.cols, cimg.rows)) )
           cimg.at<Vec3b>(pt) = Vec3b(0, 255, 0);
    }
69

70
    imshow("result", cimg);
71

72
    waitKey();
73

74 75
    return 0;
}