-
Notifications
You must be signed in to change notification settings - Fork 4
/
cvxImgMatch.cpp
139 lines (111 loc) · 5.57 KB
/
cvxImgMatch.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// cvxImgMatch.cpp
// RGBD_RF
//
// Created by jimmy on 2016-09-06.
// Copyright (c) 2016 Nowhere Planet. All rights reserved.
//
#include "cvxImgMatch.h"
#include "eigenVLFeatSIFT.h"
#include "eigenFlann.h"
#include <opencv2/core/eigen.hpp>
void CvxImgMatch::SIFTMatching(const cv::Mat & srcImg, const cv::Mat & dstImg,
const SIFTMatchingParameter & param,
vector<cv::Point2d> & srcPts, vector<cv::Point2d> & dstPts)
{
const double ratio_threshold = 0.7;
double feature_distance_threshold = 0.5;
vl_feat_sift_parameter sift_param;
sift_param.edge_thresh = 10;
sift_param.dim = 128;
sift_param.nlevels = 3;
vector<std::shared_ptr<sift_keypoint> > src_keypoints;
vector<std::shared_ptr<sift_keypoint> > dst_keypoints;
EigenVLFeatSIFT::extractSIFTKeypoint(srcImg, sift_param, src_keypoints, false);
EigenVLFeatSIFT::extractSIFTKeypoint(dstImg, sift_param, dst_keypoints, false);
Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> src_descriptors;
Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> dst_descriptors;
EigenVLFeatSIFT::descriptorToMatrix(src_keypoints, src_descriptors);
EigenVLFeatSIFT::descriptorToMatrix(dst_keypoints, dst_descriptors);
EigenFlann32F flann32;
flann32.setData(dst_descriptors, 1);
vector<vector<int> > indices; // index of src descriptors
vector<vector<float> > dists;
flann32.search(src_descriptors, indices, dists, 2);
for (int i = 0; i<src_keypoints.size(); i++) {
double dis1 = dists[i][0];
double dis2 = dists[i][1];
if (dis1 < feature_distance_threshold && dis1 < dis2 * ratio_threshold) {
int dst_index = indices[i][0];
cv::Point2d src_pt(src_keypoints[i]->location_x(), src_keypoints[i]->location_y());
cv::Point2d dst_pt(dst_keypoints[dst_index]->location_x(), dst_keypoints[dst_index]->location_y());
srcPts.push_back(src_pt);
dstPts.push_back(dst_pt);
}
}
assert(srcPts.size() == dstPts.size());
}
void CvxImgMatch::SIFTMatching(const vector<std::shared_ptr<sift_keypoint> >& src_keypoints,
const vector<std::shared_ptr<sift_keypoint> >& dst_keypoints,
const SIFTMatchingParameter & param,
vector<cv::Point2d> & srcPts, vector<cv::Point2d> & dstPts)
{
const double ratio_threshold = 0.7;
double feature_distance_threshold = 1.0;
Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> src_descriptors;
Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> dst_descriptors;
EigenVLFeatSIFT::descriptorToMatrix(src_keypoints, src_descriptors);
EigenVLFeatSIFT::descriptorToMatrix(dst_keypoints, dst_descriptors);
EigenFlann32F flann32;
flann32.setData(dst_descriptors, 1);
vector<vector<int> > indices; // index of src descriptors
vector<vector<float> > dists;
flann32.search(src_descriptors, indices, dists, 2);
for (int i = 0; i<src_keypoints.size(); i++) {
double dis1 = dists[i][0];
double dis2 = dists[i][1];
if (dis1 < feature_distance_threshold && dis1 < dis2 * ratio_threshold) {
int dst_index = indices[i][0];
cv::Point2d src_pt(src_keypoints[i]->location_x(), src_keypoints[i]->location_y());
cv::Point2d dst_pt(dst_keypoints[dst_index]->location_x(), dst_keypoints[dst_index]->location_y());
srcPts.push_back(src_pt);
dstPts.push_back(dst_pt);
}
}
assert(srcPts.size() == dstPts.size());
}
void CvxImgMatch::NNMatching(const cv::Mat & srcDescriptors, const cv::Mat & dstDescriptors,
const vector<cv::Point2d> & srcPts, const vector<cv::Point2d> & dstPts,
vector<cv::Point2d> & matchedSrcPts, vector<cv::Point2d> & matchedDstPts)
{
assert(srcDescriptors.type() == srcDescriptors.type());
assert(srcDescriptors.type() == CV_64FC1 || srcDescriptors.type() == CV_32FC1);
assert(dstDescriptors.type() == CV_64FC1 || dstDescriptors.type() == CV_32FC1);
assert(srcDescriptors.rows == srcPts.size());
assert(dstDescriptors.rows == dstPts.size());
const double ratio_threshold = 0.7;
using RowMajorFloat32 = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
RowMajorFloat32 src_descriptors = RowMajorFloat32::Zero(srcDescriptors.rows, srcDescriptors.cols);
RowMajorFloat32 dst_descriptors = RowMajorFloat32::Zero(dstDescriptors.rows, dstDescriptors.cols);
cv::cv2eigen(srcDescriptors, src_descriptors);
cv::cv2eigen(dstDescriptors, dst_descriptors);
EigenFlann32F flann32;
flann32.setData(dst_descriptors, 1);
vector<vector<int> > indices; // index of src descriptors
vector<vector<float> > dists;
flann32.search(src_descriptors, indices, dists, 2, 32);
for (int i = 0; i<srcDescriptors.rows; i++) {
double dis1 = dists[i][0];
double dis2 = dists[i][1];
if (dis1 < dis2 * ratio_threshold) {
int dst_index = indices[i][0];
matchedSrcPts.push_back(srcPts[i]);
matchedDstPts.push_back(dstPts[dst_index]);
}
}
assert(matchedSrcPts.size() == matchedDstPts.size());
}
void CvxImgMatch::ORBMatching(const cv::Mat & srcImg, const cv::Mat & dstImg,
vector<cv::Point2d> & srcPts, vector<cv::Point2d> & dstPts)
{
}