forked from blacksoil/HomographyAnalyzerDesktop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
282 lines (216 loc) · 9.16 KB
/
main.cpp
File metadata and controls
282 lines (216 loc) · 9.16 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//
// main.cpp
// SIFT
//
// Created by Antonius Harijanto on 5/17/12.
// Copyright (c) 2012 University Of Washington. All rights reserved.
//
#include <iostream>
#include <string>
#include <cassert>
#include "CVUtilities.h"
#include "HomAdapter.h"
#include <sys/time.h>
#include <sstream>
//command line argument
#define ARG_LENGTH 4
#define INPUT_PATH 1
#define OUTPUT_PATH 2
#define BASE_IMG_PATH 3
//Whether to display to screen or save or both
#define SHOW_TO_SCREEN 0
#define SAVE 1
//Whether inliers matches would be drawn or not
#define DRAW_FEATURE 1
#define RESULT_WINDOW "Input to Base"
#define INPUT_WINDOW "Input"
#define BASE_WINDOW "Base"
using namespace std;
/*
Block until an ESC is hit
*/
void EscapeToQuit(){
while (1) {
if(cvWaitKey(100) == 27)
break;
}
}
/* Given two timeval, prints out the difference in milliseconds */
void PrintTiming(struct timeval &before, struct timeval &after){
float timing = ((after.tv_sec*1000000 + after.tv_usec) - (before.tv_sec*1000000 + before.tv_usec))/1000.0;
cout << timing << " milliseconds." << endl << endl;
}
void DoTransformation(string base_path, string input_path, string save_path, int image_index){
// Prints processing input file
cout << "PROCESSING: " << input_path << endl;
cout << "---------------------------------------------" << endl << endl;
// To record the timing of the processing
struct timeval tp_before, tp_after;
//TODO: Compute the treshold using precentage
//const float RANSAC_TRESHOLD_PERCENTAGE = 0.02;
Mat image_base; //Reference image
Mat image_other; //Image to be transformed to look like reference image
Mat result; //result of homography transformation
Mat result_matches; //result with matches drawn
Mat result_matches_feature; // result of feature detection with keypoints on it
//Result with matches drawn
Mat result_inlier_matches, result_all_matches;
//Result with feature drawn
Mat result_feature_base, result_feature_input;
//Rescale the two images to be of the same size
ScaleImage(base_path, input_path, &image_base, &image_other);
float ransac_treshold = 3;//ComputeDiagonal(image_base) * RANSAC_TRESHOLD_PERCENTAGE;
if(image_base.data == NULL){
cout << "Invalid base image path: " << base_path << endl;
return;
}
if(image_other.data == NULL){
cout << "Invalid target image path: " << input_path << endl;
return;
}
//Holds the correspondences points
Correspondences corr;
//Holds the correspondences in terms of DMatch
vector<DMatch> matches;
//Feature keypoints for base and output image
vector<KeyPoint> kp_base;
vector<KeyPoint> kp_out;
cout << "Computing correspondences using SIFT" << endl;
gettimeofday(&tp_before, NULL);
gettimeofday(&tp_before, NULL);
//Getting the correspondences points using SIFT
matches = FindCorrespondences(image_base, image_other, &corr, kp_base, kp_out);
gettimeofday(&tp_after, NULL);
PrintTiming(tp_before, tp_after);
cout << "Computing homography" << endl;
gettimeofday(&tp_before, NULL);
gettimeofday(&tp_before, NULL);
//Getting the homography matrix
HomAdapter ha(corr.points_A, corr.points_B, ransac_treshold);
gettimeofday(&tp_after, NULL);
PrintTiming(tp_before, tp_after);
cout << "Producing transformed image" << endl;
gettimeofday(&tp_before, NULL);
gettimeofday(&tp_before, NULL);
//Project the input image to the base image
const int INPUT_TO_BASE = 2;
result = ha.GetProjectedWarpImage(image_base, image_other, INPUT_TO_BASE);
gettimeofday(&tp_after, NULL);
PrintTiming(tp_before, tp_after);
int numInliers = 0;
//If an inlier matches wanted to be drawn
if(DRAW_FEATURE){
cout << "Producing feature points image" << endl;
gettimeofday(&tp_before, NULL);
gettimeofday(&tp_before, NULL);
//To mask the outlier
vector<char> matchesMask(matches.size(),0);
//Transformation matrix
Mat H = ha.GetHomography();
// We compute the inlier by transforming the image then look at the
// keypoints we found before the transformation
// if the keypoints were of within ransac treshold, it is an inlier
Mat outputPtTransformed;
//Transform the base keypoints
perspectiveTransform(Mat(corr.points_A), outputPtTransformed, H);
//Iterate through the points and figure out the inliers
for(int i=0 ; i < corr.points_B.size() ; i++){
//inliers is the one that's within RansacTreshold
//norm is just a fancy way for doing absolute
if( norm(corr.points_B[i] - outputPtTransformed.at<Point2f>((int)i,0)) <= ransac_treshold) // inlier
{
matchesMask[i] = 1; // We include this keypoint
numInliers++;
}
}
//Draw the inlier matches
drawMatches(image_base, kp_base, image_other, kp_out, matches, result_inlier_matches, CV_RGB(0, 0, 0), CV_RGB(255, 255, 255)/*Scalar::all(-1)*/, matchesMask, /*DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS*/DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
//Draw all matches
drawMatches(image_base, kp_base, image_other, kp_out, matches, result_all_matches, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
//Draw feature points
//of the reference image
drawKeypoints(image_base, kp_base, result_feature_base, Scalar::all(-1), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
//of the input image
drawKeypoints(image_other, kp_out, result_feature_input, Scalar::all(-1), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
gettimeofday(&tp_after, NULL);
PrintTiming(tp_before, tp_after);
}
if(SHOW_TO_SCREEN){
namedWindow(RESULT_WINDOW);
namedWindow(BASE_WINDOW);
namedWindow(INPUT_WINDOW);
imshow(BASE_WINDOW, image_base);
imshow(INPUT_WINDOW, image_other);
imshow(RESULT_WINDOW, result);
cout << "Press Esc to process next image" << endl;
EscapeToQuit();
}
if(SAVE){
stringstream ss;
ss << numInliers << "__" << image_index;
save_path += ss.str();
imwrite(save_path + "_homog_result.jpg", result);
cout << "Image produced: " << save_path + "_homog_result.jpg" << endl;
if(DRAW_FEATURE){
/*
//Result with matches drawn
Mat result_inlier_matches, result_all_matches;
//Result with feature drawn
Mat result_feature_base, result_feature_input;
*/
string name = "_matches_inlier_matches.jpg";
imwrite(save_path + name, result_inlier_matches);
cout << "Image produced: " << save_path + name << endl;
name = "_matches_all_matches.jpg";
imwrite(save_path + name, result_all_matches);
cout << "Image produced: " << save_path + name << endl;
name = "_result_feature_base.jpg";
imwrite(save_path + name, result_feature_base);
cout << "Image produced: " << save_path + name << endl;
name = "_result_feature_input.jpg";
imwrite(save_path + name, result_feature_input);
cout << "Image produced: " << save_path + name << endl;
}
}
cout << "---------------------------------------------" << endl << endl;
}
void FUsage(){
cout << "./ProgramName [input-dir-path] [output-dir-path] [base-file-path]" << endl;
cout << ".jpg or .png files are expected on the input-path" << endl << endl;
// cout << "Be sure to have two folders on the output-dir-path: \"homography\" and \"Feature\"" << endl;
exit(EXIT_FAILURE);
}
int main(int argc, char **argv){
if(argc != ARG_LENGTH){
cout << "Arg length: " << argc << endl;
FUsage();
}
/* Grab all input file in the input folder */
vector<string> inputFile = listImageFile(argv[INPUT_PATH]);
if(inputFile.size() == 0){
cout << "Invalid input path? No file found!" << endl;
exit(EXIT_FAILURE);
}
/* grab the input path and append / if necessary */
string inputpath = argv[INPUT_PATH];
if(inputpath[inputpath.length()-1] != '/')
inputpath += '/';
string outputdirpath = argv[OUTPUT_PATH];
if(outputdirpath[outputdirpath.length()-1] != '/')
outputdirpath += '/';
/* base image path */
string base_img_path = argv[BASE_IMG_PATH];
/* input output path*/
string ifpath;
stringstream ofpath;
string ofpath_homography;
for(int i=0;i<inputFile.size();i++){
ofpath.str("");
ifpath = inputpath + inputFile[i];
ofpath << outputdirpath;
/* be sure to append .extension to the file */
ofpath_homography = ofpath.str();// + "_homog_result.JPG";
DoTransformation(base_img_path, ifpath,ofpath_homography, i+1);
}
return 0;
}