-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathmatcher.cpp
77 lines (64 loc) · 2.15 KB
/
matcher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
Reads 2 images, detects keypoints, compute ORB descriptors at these keypoints,
matches the descriptors by nearest neighbour (brute force)
Author : Manohar Kuse <[email protected]>
Released in Public Domain
*/
#include <iostream>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
using namespace std;
int main()
{
//
// Load Images
cv::Mat im1 = cv::imread( "../image/church1.jpg");
cv::Mat im2 = cv::imread( "../image/church2.jpg");
cv::imshow( "im1", im1 );
cv::imshow( "im2", im2 );
cv::waitKey(0);
//
// Feature Detector
cv::Ptr<cv::Feature2D> fdetector = cv::ORB::create();
std::vector<cv::KeyPoint> keypoints1, keypoints2;
cv::Mat descriptors1, descriptors2;
fdetector->detectAndCompute(im1, cv::Mat(), keypoints1, descriptors1);
fdetector->detectAndCompute(im2, cv::Mat(), keypoints2, descriptors2);
cout << "# of keypoints : "<< keypoints1.size() << endl;
cout << "# of keypoints : "<< keypoints2.size() << endl;
cout << "descriptors shape : "<< descriptors1.rows << "x" << descriptors1.cols << endl;
cout << "descriptors shape : "<< descriptors2.rows << "x" << descriptors2.cols << endl;
//
// Draw Keypoints
cv::Mat outImage1, outImage2;
cv::drawKeypoints(im1, keypoints1, outImage1 );
cv::drawKeypoints(im2, keypoints2, outImage2 );
cv::imshow( "win-keys1", outImage1 );
cv::imshow( "win-keys2", outImage2 );
cv::waitKey(0);
//
// Matcher - Brute Force
cv::BFMatcher matcher(cv::NORM_L2);
std::vector< cv::DMatch > matches;
matcher.match(descriptors1, descriptors2, matches);
/* - Consider using an FLANN based matches for speed
// Matcher - FLANN (Approx NN)
if(descriptors1.type()!=CV_32F)
{
descriptors1.convertTo(descriptors1, CV_32F);
descriptors2.convertTo(descriptors2, CV_32F);
}
cv::FlannBasedMatcher matcher;
std::vector< cv::DMatch > matches;
matcher.match( descriptors1, descriptors2, matches );
*/
//
// Draw Matches
cv::Mat outImg;
cv::drawMatches(im1, keypoints1, im2, keypoints2, matches, outImg );
cv::imshow( "matches", outImg );
cv::waitKey(0);
return 0;
}