Attachment 'desc_match.cpp'
Download 1 #include <highgui.h>
2 #include "opencv2/core/core.hpp"
3 #include "opencv2/calib3d/calib3d.hpp"
4 #include "opencv2/imgproc/imgproc.hpp"
5 #include "features_2d/features_2d.h"
6 #include <iostream>
7
8 using namespace cv;
9 using namespace std;
10 namespace f2d = features_2d;
11
12 #define DRAW_RICH_KEYPOINTS_MODE 0
13 #define DRAW_OUTLIERS_MODE 0
14
15 const string winName = "correspondences";
16
17 void drawMatchesOnly(const cv::Mat& image1, const std::vector<cv::KeyPoint>& keypoints1,
18 const cv::Mat& image2, const std::vector<cv::KeyPoint>& keypoints2,
19 const std::vector<f2d::Match>& matches, cv::Mat& display)
20 {
21 // Set up composite image
22 display = cv::Mat::zeros(std::max(image1.rows, image2.rows), image1.cols + image2.cols, CV_8UC3);
23 cv::Mat sub_display1 = display( cv::Rect(0, 0, image1.cols, image1.rows) );
24 cv::Mat sub_display2 = display( cv::Rect(image1.cols, 0, image2.cols, image2.rows) );
25
26 cvtColor(image1, sub_display1, CV_GRAY2BGR);
27 cvtColor(image2, sub_display2, CV_GRAY2BGR);
28
29 // Draw lines between matches
30 int shift_bits = 4;
31 int multiplier = 1 << shift_bits;
32 for (std::vector<f2d::Match>::const_iterator i = matches.begin(), ie = matches.end(); i != ie; ++i) {
33 const cv::KeyPoint& keypt1 = keypoints1[i->index1];
34 const cv::KeyPoint& keypt2 = keypoints2[i->index2];
35 cv::Point center1(keypt1.pt.x * multiplier, keypt1.pt.y * multiplier);
36 cv::Point center2((keypt2.pt.x + image1.cols) * multiplier, keypt2.pt.y * multiplier);
37 cv::Scalar color(std::rand() % 256, std::rand() % 256, std::rand() % 256);
38 cv::line(display, center1, center2, color, 1, CV_AA, shift_bits);
39 }
40 }
41
42 int main(int argc, char** argv)
43 {
44 if( argc != 4 )
45 {
46 cout << "Format:" << endl;
47 cout << argv[0] << " [image1] [image2] [ransacReprojThreshold]" << endl;
48
49 return -1;
50 }
51 double ransacReprojThreshold = atof(argv[3]);
52
53 cout << "< Creating detector, descriptor extractor and descriptor matcher ..." << endl;
54 f2d::SurfFeatureDetector detector;
55 f2d::SurfDescriptorExtractor descriptorExtractor;
56 f2d::BruteForceMatcher<f2d::L2<float> > descriptorMatcher;
57
58 cout << "< Reading the images..." << endl;
59 Mat img1 = imread( argv[1], 0 );
60 Mat img2 = imread( argv[2], 0 );
61 cout << ">" << endl;
62 if( img1.empty() || img2.empty() )
63 {
64 cout << "Can not read images" << endl;
65 return -1;
66 }
67
68 cout << endl << "< Extracting keypoints from the first image..." << endl;
69 vector<KeyPoint> keypoints1;
70 detector.detect( img1, keypoints1 );
71 cout << keypoints1.size() << " points" << endl << ">" << endl;
72
73 cout << "< Computing descriptors for keypoints from the first image..." << endl;
74 Mat descriptors1;
75 descriptorExtractor.compute( img1, keypoints1, descriptors1 );
76 cout << ">" << endl;
77
78 cout << endl << "< Extracting keypoints from the second image..." << endl;
79 vector<KeyPoint> keypoints2;
80 detector.detect( img2, keypoints2 );
81 cout << keypoints2.size() << " points" << endl << ">" << endl;
82
83 cout << "< Computing descriptors for keypoints from the second image..." << endl;
84 Mat descriptors2;
85 descriptorExtractor.compute( img2, keypoints2, descriptors2 );
86 cout << ">" << endl;
87
88 vector<f2d::Match> matches;
89 descriptorMatcher.matchWindowless(descriptors1, descriptors2, matches);
90
91 vector<Point2f> points1, points2;
92 for(size_t i = 0; i < matches.size(); i++)
93 {
94 points1.push_back(keypoints1[matches[i].index1].pt);
95 points2.push_back(keypoints2[matches[i].index2].pt);
96 }
97 Mat H = findHomography(Mat(points1), Mat(points2), CV_RANSAC, ransacReprojThreshold);
98 Mat points1Projected;
99 perspectiveTransform(Mat(points1), points1Projected, H);
100
101 vector<f2d::Match> matchesFiltered;
102 for(size_t i = 0; i < points1.size(); i++)
103 {
104 Point2f point1 = points1Projected.at<Point2f>(i, 0);
105 double dist = norm(point1 - points2[i]);
106 if(dist < ransacReprojThreshold)
107 {
108 matchesFiltered.push_back(matches[i]);
109 }
110 }
111
112 namedWindow(winName, 1);
113 Mat drawImg;
114 drawMatchesOnly(img1, keypoints1, img2, keypoints2, matchesFiltered, drawImg);
115 imshow(winName, drawImg);
116 waitKey(0);
117
118 return 0;
119 }
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.