-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatch.cpp
More file actions
103 lines (63 loc) · 2.28 KB
/
match.cpp
File metadata and controls
103 lines (63 loc) · 2.28 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <opencv2/opencv.hpp>
#include <iostream>
#include <cstdlib>
#include <cstdlib>
#include <random>
#include <vector>
#define OPENCV_FEATURES_2D_HPP
#include "opencv2/opencv_modules.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/xfeatures2d.hpp"
#include <opencv2/xfeatures2d/nonfree.hpp>
#include <opencv2/xfeatures2d.hpp>
int main(int argc, char** argv){
cv::Mat img1, img2, matchImage, result1, result2;;
img1 = cv::imread("t1.jpg",0);
img2 = cv::imread("t2.jpg",0);
std::vector<cv::KeyPoint> keypoints1, keypoints2;
cv::Ptr<cv::FeatureDetector> ptrDetector;
ptrDetector = cv::FastFeatureDetector::create(80);
ptrDetector->detect(img1, keypoints1);
ptrDetector->detect(img2, keypoints2);
const int nsize(11);
cv::Rect neighborhood(0,0, nsize, nsize);
cv::Mat patch1;
cv::Mat patch2;
cv::Mat result;
std::vector<cv::DMatch> matches;
for(int i=0; i<keypoints1.size(); i++){
neighborhood.x = keypoints1[i].pt.x-nsize/2;
neighborhood.y = keypoints1[i].pt.y-nsize/2;
if(neighborhood.x<0 || neighborhood.y<0 || neighborhood.x+nsize >= img1.cols || neighborhood.y+nsize >= img1.rows){
continue;
}
patch1 = img1(neighborhood);
cv::DMatch bestMatch;
for(int j=0; j<keypoints2.size(); j++){
neighborhood.x = keypoints2[j].pt.x-nsize/2;
neighborhood.y = keypoints2[j].pt.y-nsize/2;
if(neighborhood.x<0 || neighborhood.y<0 || neighborhood.x + nsize >= img2.cols || neighborhood.y + nsize >= img2.rows){
continue;
}
patch2 = img2(neighborhood);
cv::matchTemplate(patch1, patch2, result, cv::TM_SQDIFF);
if(result.at<float>(0,0) < bestMatch.distance){
bestMatch.distance = result.at<float>(0,0);
bestMatch.queryIdx = i;
bestMatch.trainIdx = j;
}
}
matches.push_back(bestMatch);
std::nth_element(matches.begin(), matches.begin()+25, matches.end());
matches.erase(matches.begin() + 25, matches.end());
}
// cv::drawKeypoints(img1, keypoints1, result1);// drawing flag
// cv::drawKeypoints(img2, keypoints2, result2);
cv::drawMatches(img1, keypoints1, img2, keypoints2, matches, matchImage,cv::Scalar(255,255,255), cv::Scalar(255,255,255));
// cv::namedWindow("img1");
// cv::namedWindow("img2");
// cv::imshow("img1",result1);
// cv::imshow("img2",result2);
cv::waitKey(0);
// cv::drawMatches(img1, )
}