-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathdeformable.cpp
More file actions
172 lines (139 loc) · 6.5 KB
/
deformable.cpp
File metadata and controls
172 lines (139 loc) · 6.5 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
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
/** @Deformable object correspondence
** @author Zhe Liu
**/
/*
Copyright (C) 2011-12 Zhe Liu and Pierre Moulon.
All rights reserved.
This file is part of the KVLD library and is made available under
the terms of the BSD license (see the COPYING file).
*/
#include <algorithm>
#include <memory>
#include "kvld/kvld.h"
#include "convert.h"
#include <vector>
#include "opencv2/opencv.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/calib3d.hpp"
#include "opencv2/core.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/xfeatures2d.hpp"
#include "opencv2/xfeatures2d/nonfree.hpp"
using namespace cv::xfeatures2d;
const float sift_matching_criterion=0.98;
int main(int argc,char*argv[]) {
std::cout<<"Warming: K-VLD may suffer performance degradation under Linux OS!"<<std::endl
<<"Please first check existing result in the output folder as a reference!"<<std::endl;
//================= load images ======================//
cv::Mat image1, image2;
int imageID=4;// index of a pair of images you want to use in folder demo_images
std::string index;
std::stringstream f;
f<<imageID;
f>>index;
std::string input=std::string(SOURCE_DIR)+"/demo_image/IMG_";
image1= cv::imread(input+index+".jpg", cv::IMREAD_GRAYSCALE);
image2= cv::imread(input+index+"bis.jpg", cv::IMREAD_GRAYSCALE);
cv::Mat image1color, image2color, concat;//for visualization
image1color= cv::imread(input+index+".jpg", cv::IMREAD_COLOR);
image2color= cv::imread(input+index+"bis.jpg", cv::IMREAD_COLOR);
std::cout << image1color.rows << " " << image1color.cols << std::endl;
std::cout << image2color.rows << " " << image2color.cols << std::endl;
//=============== compute SIFT points =================//
std::cout<<"Extracting SIFT features"<<std::endl;
std::vector<cv::KeyPoint> feat1,feat2;
cv::Ptr<SURF> detector = SURF::create(15,1);//default setting is ok, 5 levels generate too much features
cv::Ptr<SURF> extractor= SURF::create();
cv::Mat descriptors1,descriptors2;
detector->detect(image1,feat1);
extractor->compute(image1,feat1,descriptors1);
std::cout<< "sift:: 1st image: " << feat1.size() << " keypoints"<<std::endl;
detector->detect(image2,feat2);
extractor->compute(image2,feat2,descriptors2);
std::cout<< "sift:: 2nd image: " << feat2.size() << " keypoints"<<std::endl;
//=============== compute matches using brute force matching ====================//
std::vector<cv::DMatch> matches;
bool bSymmetricMatches = false;//caution, activate this with knn matching will cause errors.
cv::BFMatcher matcher(cv::NORM_L2, bSymmetricMatches);
if (bSymmetricMatches){
matcher.match(descriptors1,descriptors2,matches);
}
else
{
std::vector<std::vector<cv::DMatch>> knnmatches;
matcher.knnMatch(descriptors1,descriptors2,knnmatches,2);
for (std::vector<std::vector<cv::DMatch>>::const_iterator it=knnmatches.begin();it!=knnmatches.end();it++){
if (it->at(0).distance<sift_matching_criterion*it->at(1).distance)
matches.push_back((*it)[0]);
}
}
//=============== convert openCV sturctures to KVLD recognized elements
Image<float> If1, If2;
Convert_image(image1, If1);
Convert_image(image2, If2);
std::vector<keypoint> F1, F2;
Convert_detectors(feat1,F1);//we only need detectors for KVLD
Convert_detectors(feat2,F2);//we only need detectors for KVLD
std::vector<Pair> matchesPair;
Convert_matches(matches,matchesPair);
//=============================== KVLD method ==================================//
std::cout<<"K-VLD starts with "<<matches.size()<<" matches"<<std::endl;
std::vector<Pair> 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.
std::vector<std::vector<float>> E(matches.size(), std::vector<float>( matches.size(), -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.
clock_t tic = clock();
size_t it_num=0;
KvldParameters kvldparameters;//initial parameters of KVLD
while (it_num < 5 && kvldparameters.inlierRate>KVLD(If1, If2,F1,F2, matchesPair, 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++;
}
float time_passed = ((float)clock() - tic) / CLOCKS_PER_SEC;
std::cout<<"K-VLD filter ends with "<<matchesFiltered.size()<<" selected matches, time = "<< time_passed<<std::endl;
//================= write files to output folder ==================//
std::cout<<"Writing results to the output folder..."<<std::endl;
std::string output=std::string(SOURCE_DIR)+"/demo_output/IMG_"+index+"_";
writeResult(output,F1, F2, matchesPair, matchesFiltered, vec_score);
//================= Visualize matching result ====================//
cv::vconcat(image1color, image2color,concat);
for( std::vector<cv::DMatch>::const_iterator ptr = matches.begin(); ptr != matches.end(); ptr++)
{
cv::KeyPoint start = feat1[ptr->queryIdx];
cv::KeyPoint end = feat2[ptr->trainIdx];
cv::line( concat,start.pt, end.pt+cv::Point2f(0,image1.rows),cv::Scalar(0, 255,0 ));
}
cv::imwrite(output+"initial.png",concat);
//========== KVLD result =============//
cv::vconcat(image1color, image2color,concat);
//draw gvld-consistant neighbors (not exhostive)
for (int it1=0; it1<matchesPair.size()-1;it1++){
for (int it2=it1+1; it2<matchesPair.size();it2++){
if (valide[it1] && valide[it2] && E[it1][it2]>=0){
cv::KeyPoint l1 = feat1[matchesPair[it1].first];
cv::KeyPoint l2 = feat1[matchesPair[it2].first];
cv::KeyPoint r1 = feat2[matchesPair[it1].second];
cv::KeyPoint r2 = feat2[matchesPair[it2].second];
cv::line(concat,l1.pt, l2.pt,cv::Scalar(255,0,255),2);
cv::line(concat,r1.pt+cv::Point2f(0,image1.rows), r2.pt+cv::Point2f(0,image1.rows),cv::Scalar(255,0,255),2);
}
}
}
for( std::vector<Pair >::const_iterator ptr = matchesFiltered.begin(); ptr != matchesFiltered.end(); ++ptr)
{
size_t i = ptr->first;
size_t j = ptr->second;
cv::KeyPoint start = feat1[i];
cv::KeyPoint end = feat2[j];
cv::line( concat,start.pt, end.pt+cv::Point2f(0,image1.rows),cv::Scalar(0, 255, 0 ));
}
cv::imwrite(output+"kvld_filtered.png",concat);
std::cout<<"Please check the output folder for results."<<std::endl;
return 0;
}