-
Notifications
You must be signed in to change notification settings - Fork 4
/
cvx_kvld.cpp
204 lines (172 loc) · 7.62 KB
/
cvx_kvld.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
// cvx_kvld.cpp
// MAVGoogleImageMatching
//
// Created by jimmy on 2015-11-28.
// Copyright © 2015 jimmy. All rights reserved.
//
#include "cvx_kvld.hpp"
#include "vxlOpenCV.h"
#include "kvld.h"
#include <bapl/bapl_keypoint_sptr.h>
#include <bapl/bapl_lowe_keypoint.h>
/*
static int Convert_image(const cv::Mat& In, cv::Mat& imag)//convert only gray scale image of opencv
{
imag = cv::Mat(In.rows, In.cols, CV_32FC1);
int cn = In.channels();
if (cn == 1)//gray scale
{
for (int i = 0; i < In.rows; ++i)
{
for (int j = 0; j < In.cols; ++j)
{
imag.at<float>(i, j) = In.at<unsigned char>(i, j);
}
}
}
else
{
for (int i = 0; i < In.rows; ++i)
{
for (int j = 0; j < In.cols; ++j)
{
//imag.at<float>(i, j) = (float(pixelPtr[(i*In.cols + j)*cn + 0]) * 29 + float(pixelPtr[(i*In.cols + j)*cn + 1]) * 150 + float(pixelPtr[(i*In.cols + j)*cn + 2]) * 77) / 255;
//why not using uniform weights of the channels?
}
}
}
return 0;
}
*/
static int convert_image(const cv::Mat & in, cv::Mat & out)
{
out = cv::Mat(in.rows, in.cols, CV_32FC1);
cv::Mat gray;
if (in.channels() == 3) {
cv::cvtColor(in, gray, CV_BGR2GRAY);
}
else if(in.channels() == 1)
{
gray = in;
}
else
{
assert(0);
}
assert(gray.channels() == 1);
for (int i = 0; i < gray.rows; ++i)
{
for (int j = 0; j < gray.cols; ++j)
{
out.at<float>(i, j) = gray.at<unsigned char>(i, j);
}
}
return 0;
}
bool cvx_kvld::kvld_matching(const vil_image_view<vxl_byte> & image1,
const vil_image_view<vxl_byte> & image2,
vcl_vector<bapl_key_match> & final_matches,
vcl_vector<bool> & is_valid,
const cvx_kvld_parameter & param)
{
// feature point set 1
std::vector<VLDKeyPoint> F1;
for (int i = 0; i<param.keypoint_1.size(); i++) {
VLDKeyPoint vld_pt;
bapl_lowe_keypoint_sptr sift = dynamic_cast<bapl_lowe_keypoint *>(param.keypoint_1[i].as_pointer());
assert(sift);
vld_pt.cvKeyPoint.size = sift->scale();
vld_pt.cvKeyPoint.angle = sift->orientation();
vld_pt.cvKeyPoint.pt = cv::Point2f(sift->location_i(), sift->location_j());
F1.push_back(vld_pt);
}
// feature point set 2
std::vector<VLDKeyPoint> F2;
for (int i = 0; i<param.keypoint_2.size(); i++) {
VLDKeyPoint vld_pt;
bapl_lowe_keypoint_sptr sift = dynamic_cast<bapl_lowe_keypoint *>(param.keypoint_2[i].as_pointer());
assert(sift);
vld_pt.cvKeyPoint.size = sift->scale();
vld_pt.cvKeyPoint.angle = sift->orientation();
vld_pt.cvKeyPoint.pt = cv::Point2f(sift->location_i(), sift->location_j());
F2.push_back(vld_pt);
}
std::cout << "sift:: 1st image: " << F1.size() << " keypoints" << std::endl;
std::cout << "sift:: 2nd image: " << F2.size() << " keypoints" << std::endl;
std::vector<cv::DMatch> matches;
std::vector<cv::DMatch> matchesFiltered;
std::vector<double> vec_score;
for (int i = 0; i<param.matches.size(); i++) {
matches.push_back(cv::DMatch(param.matches[i].first, param.matches[i].second, 0.0));
}
std::cout << "K-VLD starts with " << matches.size() << " matches" << std::endl;
//In order to illustrate the gvld(or vld)-consistant neighbors, the following two parameters has been externalized as inputs of the function KVLD.
Matrixf E = Matrixf::ones(matches.size(), matches.size(), CV_32FC1)*(-1);
// gvld-consistency matrix, intitialized to -1, >0 consistency value, -1=unknow, -2=false
is_valid = std::vector<bool>(matches.size(), true);// indices of match in the initial matches, if true at the end of KVLD, a match is kept.
size_t it_num = 0;
KvldParameters kvldparameters;//initial parameters of KVLD
cv::Mat cv_image1 = VxlOpenCVImage::cv_image(image1);
cv::Mat cv_image2 = VxlOpenCVImage::cv_image(image2);
// int convert_image(const cv::Mat & in, cv::Mat & out)
cv::Mat cv_float_image1;
cv::Mat cv_float_image2;
convert_image(cv_image1, cv_float_image1);
convert_image(cv_image2, cv_float_image2);
while (it_num < 5 && kvldparameters.inlierRate>KVLD(cv_float_image1, cv_float_image2, F1, F2,
matches, matchesFiltered, vec_score, E, is_valid, kvldparameters))
{
kvldparameters.inlierRate /= 2;
kvldparameters.rang_ratio = sqrt(2.0f);
std::cout << "low inlier rate, re-select matches with new rate=" << kvldparameters.inlierRate << std::endl;
if (matchesFiltered.size() == 0) kvldparameters.K = 2;
it_num++;
}
std::cout << "K-VLD filter ends with " << matchesFiltered.size() << " selected matches" << std::endl;
final_matches.clear();
for (int i = 0; i<is_valid.size(); i++) {
if (is_valid[i]) {
int idx1 = param.matches[i].first;
int idx2 = param.matches[i].second;
bapl_key_match one_match(param.keypoint_1[idx1], param.keypoint_2[idx2]);
final_matches.push_back(one_match);
}
}
/*
cv::Mat image1color, image2color, concat;//for visualization
image1color = cv::imread(mavImgFilePath, CV_LOAD_IMAGE_COLOR);
image2color = cv::imread(streetImgFilePath, CV_LOAD_IMAGE_COLOR);
//=============== Read SIFT points =================//
std::cout << "Loading SIFT features" << std::endl;
read_detectors(datasetPath + "mav_keypoint/keypoint_" + mavImgNumStr + ".txt", F1);
read_detectors(datasetPath + "street_keypoint/keypoint_" + streetImgNumStr + ".txt", F2);
std::cout << "sift:: 1st image: " << F1.size() << " keypoints" << std::endl;
std::cout << "sift:: 2nd image: " << F2.size() << " keypoints" << std::endl;
//=============== load initial matching ====================//
std::vector<cv::DMatch> matches;
read_matches(datasetPath + "initial_matchings/" + fileName, matches);
//=============================== KVLD method ==================================//
std::cout << "K-VLD starts with " << matches.size() << " matches" << std::endl;
std::vector<cv::DMatch> matchesFiltered;
std::vector<double> vec_score;
//In order to illustrate the gvld(or vld)-consistant neighbors, the following two parameters has been externalized as inputs of the function KVLD.
Matrixf E = Matrixf::ones(matches.size(), matches.size(), CV_32FC1)*(-1);
// gvld-consistency matrix, intitialized to -1, >0 consistency value, -1=unknow, -2=false
std::vector<bool> valide(matches.size(), true);// indices of match in the initial matches, if true at the end of KVLD, a match is kept.
size_t it_num = 0;
KvldParameters kvldparameters;//initial parameters of KVLD
while (it_num < 5 && kvldparameters.inlierRate>KVLD(image1, image2, F1, F2,
matches, matchesFiltered, vec_score, E, valide, kvldparameters))
{
kvldparameters.inlierRate /= 2;
kvldparameters.rang_ratio = sqrt(2.0f);
std::cout << "low inlier rate, re-select matches with new rate=" << kvldparameters.inlierRate << std::endl;
if (matchesFiltered.size() == 0) kvldparameters.K = 2;
it_num++;
}
std::cout << "K-VLD filter ends with " << matchesFiltered.size() << " selected matches" << std::endl;
return true;
*/
return true;
}