2010-06-29 00:03:01 +08:00
|
|
|
#include "opencv2/calib3d/calib3d.hpp"
|
|
|
|
#include "opencv2/features2d/features2d.hpp"
|
2010-11-17 00:34:27 +08:00
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
2012-03-15 22:36:01 +08:00
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
|
|
|
#include "opencv2/nonfree/nonfree.hpp"
|
2010-11-17 00:34:27 +08:00
|
|
|
|
2010-06-29 00:03:01 +08:00
|
|
|
#include <cstdio>
|
2010-06-10 15:59:18 +08:00
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
using namespace std;
|
2010-06-10 15:59:18 +08:00
|
|
|
using namespace cv;
|
|
|
|
|
2012-06-08 01:21:29 +08:00
|
|
|
static void help()
|
2010-11-25 10:48:18 +08:00
|
|
|
{
|
2011-06-23 20:15:35 +08:00
|
|
|
printf("Use the SURF descriptor for matching keypoints between 2 images\n");
|
|
|
|
printf("Format: \n./generic_descriptor_match <image1> <image2> <algorithm> <XML params>\n");
|
|
|
|
printf("For example: ./generic_descriptor_match ../c/scene_l.bmp ../c/scene_r.bmp FERN fern_params.xml\n");
|
2010-11-25 10:48:18 +08:00
|
|
|
}
|
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
Mat DrawCorrespondences(const Mat& img1, const vector<KeyPoint>& features1, const Mat& img2,
|
|
|
|
const vector<KeyPoint>& features2, const vector<DMatch>& desc_idx);
|
2010-06-10 15:59:18 +08:00
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
if (argc != 5)
|
|
|
|
{
|
2012-06-08 01:21:29 +08:00
|
|
|
help();
|
2010-06-10 15:59:18 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string img1_name = std::string(argv[1]);
|
|
|
|
std::string img2_name = std::string(argv[2]);
|
|
|
|
std::string alg_name = std::string(argv[3]);
|
|
|
|
std::string params_filename = std::string(argv[4]);
|
|
|
|
|
2010-11-25 23:59:37 +08:00
|
|
|
Ptr<GenericDescriptorMatcher> descriptorMatcher = GenericDescriptorMatcher::create(alg_name, params_filename);
|
2013-08-13 22:49:29 +08:00
|
|
|
if( !descriptorMatcher )
|
2010-06-10 15:59:18 +08:00
|
|
|
{
|
|
|
|
printf ("Cannot create descriptor\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//printf("Reading the images...\n");
|
2013-04-08 02:45:38 +08:00
|
|
|
Mat img1 = imread(img1_name, IMREAD_GRAYSCALE);
|
|
|
|
Mat img2 = imread(img2_name, IMREAD_GRAYSCALE);
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-06-10 15:59:18 +08:00
|
|
|
// extract keypoints from the first image
|
|
|
|
SURF surf_extractor(5.0e3);
|
|
|
|
vector<KeyPoint> keypoints1;
|
|
|
|
|
|
|
|
// printf("Extracting keypoints\n");
|
|
|
|
surf_extractor(img1, Mat(), keypoints1);
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-06-10 15:59:18 +08:00
|
|
|
printf("Extracted %d keypoints from the first image\n", (int)keypoints1.size());
|
|
|
|
|
|
|
|
vector<KeyPoint> keypoints2;
|
|
|
|
surf_extractor(img2, Mat(), keypoints2);
|
|
|
|
printf("Extracted %d keypoints from the second image\n", (int)keypoints2.size());
|
|
|
|
|
|
|
|
printf("Finding nearest neighbors... \n");
|
|
|
|
// find NN for each of keypoints2 in keypoints1
|
2010-10-29 16:44:42 +08:00
|
|
|
vector<DMatch> matches2to1;
|
|
|
|
descriptorMatcher->match( img2, keypoints2, img1, keypoints1, matches2to1 );
|
2010-06-10 15:59:18 +08:00
|
|
|
printf("Done\n");
|
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
Mat img_corr = DrawCorrespondences(img1, keypoints1, img2, keypoints2, matches2to1);
|
2010-06-10 15:59:18 +08:00
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
imshow("correspondences", img_corr);
|
|
|
|
waitKey(0);
|
2010-06-10 15:59:18 +08:00
|
|
|
}
|
|
|
|
|
2012-03-15 22:36:01 +08:00
|
|
|
Mat DrawCorrespondences(const Mat& img1, const vector<KeyPoint>& features1, const Mat& img2,
|
|
|
|
const vector<KeyPoint>& features2, const vector<DMatch>& desc_idx)
|
2010-06-10 15:59:18 +08:00
|
|
|
{
|
2012-03-15 22:36:01 +08:00
|
|
|
Mat part, img_corr(Size(img1.cols + img2.cols, MAX(img1.rows, img2.rows)), CV_8UC3);
|
|
|
|
img_corr = Scalar::all(0);
|
|
|
|
part = img_corr(Rect(0, 0, img1.cols, img1.rows));
|
|
|
|
cvtColor(img1, part, COLOR_GRAY2RGB);
|
|
|
|
part = img_corr(Rect(img1.cols, 0, img2.cols, img2.rows));
|
|
|
|
cvtColor(img1, part, COLOR_GRAY2RGB);
|
2010-06-10 15:59:18 +08:00
|
|
|
|
|
|
|
for (size_t i = 0; i < features1.size(); i++)
|
|
|
|
{
|
2013-04-11 23:27:54 +08:00
|
|
|
circle(img_corr, features1[i].pt, 3, Scalar(255, 0, 0));
|
2010-06-10 15:59:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < features2.size(); i++)
|
|
|
|
{
|
2012-03-15 22:36:01 +08:00
|
|
|
Point pt(cvRound(features2[i].pt.x + img1.cols), cvRound(features2[i].pt.y));
|
|
|
|
circle(img_corr, pt, 3, Scalar(0, 0, 255));
|
|
|
|
line(img_corr, features1[desc_idx[i].trainIdx].pt, pt, Scalar(0, 255, 0));
|
2010-06-10 15:59:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return img_corr;
|
|
|
|
}
|