2011-05-22 02:32:34 +08:00
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
|
|
#include "opencv2/contrib/contrib.hpp"
|
2010-11-30 09:34:39 +08:00
|
|
|
|
2011-08-10 19:36:55 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
2010-11-30 09:34:39 +08:00
|
|
|
using namespace cv;
|
|
|
|
using namespace std;
|
|
|
|
|
2012-06-15 21:04:17 +08:00
|
|
|
static void help()
|
2010-12-03 18:06:41 +08:00
|
|
|
{
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2011-08-10 19:36:55 +08:00
|
|
|
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";
|
2010-12-03 18:06:41 +08:00
|
|
|
}
|
2011-06-17 14:31:54 +08:00
|
|
|
|
2012-06-15 21:04:17 +08:00
|
|
|
const char* keys =
|
2011-08-10 18:41:38 +08:00
|
|
|
{
|
2012-06-15 21:04:17 +08:00
|
|
|
"{1| |logo_in_clutter.png|image edge map }"
|
|
|
|
"{2| |logo.png |template edge map}"
|
2011-08-10 18:41:38 +08:00
|
|
|
};
|
2011-06-17 14:31:54 +08:00
|
|
|
|
2011-08-10 18:41:38 +08:00
|
|
|
int main( int argc, const char** argv )
|
2010-11-30 09:34:39 +08:00
|
|
|
{
|
2011-06-17 14:31:54 +08:00
|
|
|
|
2012-06-15 21:04:17 +08:00
|
|
|
help();
|
|
|
|
CommandLineParser parser(argc, argv, keys);
|
2011-08-10 18:41:38 +08:00
|
|
|
|
2012-06-15 21:04:17 +08:00
|
|
|
string image = parser.get<string>("1");
|
|
|
|
string templ = parser.get<string>("2");
|
|
|
|
Mat img = imread(image.c_str(), 0);
|
|
|
|
Mat tpl = imread(templ.c_str(), 0);
|
2011-08-10 18:41:38 +08:00
|
|
|
|
2012-06-15 21:04:17 +08:00
|
|
|
if (img.empty() || tpl.empty())
|
2011-08-10 19:36:55 +08:00
|
|
|
{
|
|
|
|
cout << "Could not read image file " << image << " or " << templ << "." << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-11-30 09:34:39 +08:00
|
|
|
Mat cimg;
|
|
|
|
cvtColor(img, cimg, CV_GRAY2BGR);
|
2011-06-17 14:31:54 +08:00
|
|
|
|
2010-11-30 09:34:39 +08:00
|
|
|
// 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.
|
2011-06-17 14:31:54 +08:00
|
|
|
|
2010-11-30 09:34:39 +08:00
|
|
|
// Canny(img, img, 5, 50, 3);
|
|
|
|
// Canny(tpl, tpl, 5, 50, 3);
|
2011-06-17 14:31:54 +08:00
|
|
|
|
2010-11-30 09:34:39 +08:00
|
|
|
vector<vector<Point> > results;
|
|
|
|
vector<float> costs;
|
|
|
|
int best = chamerMatching( img, tpl, results, costs );
|
|
|
|
if( best < 0 )
|
|
|
|
{
|
2011-08-10 19:36:55 +08:00
|
|
|
cout << "matching not found" << endl;
|
2011-08-10 18:41:38 +08:00
|
|
|
return -1;
|
2010-11-30 09:34:39 +08:00
|
|
|
}
|
2011-06-17 14:31:54 +08:00
|
|
|
|
2010-11-30 09:34:39 +08:00
|
|
|
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);
|
|
|
|
}
|
2011-06-17 14:31:54 +08:00
|
|
|
|
2010-11-30 09:34:39 +08:00
|
|
|
imshow("result", cimg);
|
2011-06-17 14:31:54 +08:00
|
|
|
|
2010-11-30 09:34:39 +08:00
|
|
|
waitKey();
|
2011-06-17 14:31:54 +08:00
|
|
|
|
2010-11-30 09:34:39 +08:00
|
|
|
return 0;
|
|
|
|
}
|