-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessor.cpp
392 lines (295 loc) · 10.6 KB
/
preprocessor.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* preprocessor.cpp
*
* Created on: Jan 2, 2013
* Author: michael
*/
#include "preprocessor.h"
#include <dirent.h>
#include "opencv2/core/core.hpp"
//#include "opencv2/features2d/features2d.hpp"
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/nonfree/nonfree.hpp>
#include <opencv2/legacy/legacy.hpp>
#include "opencv2/calib3d/calib3d.hpp"
#include <opencv2/imgproc/imgproc.hpp>
#include <stdlib.h>
#include <stdio.h>
#define HISTMATCH_EPSILON 0.000001
using namespace cv;
using namespace std;
Preprocessor::Preprocessor(std::string refdir, int numberOfImagesToUse) {
refdir_ = refdir;
this->numberOfImagesToUse = numberOfImagesToUse;
}
Preprocessor::~Preprocessor() {
// TODO Auto-generated destructor stub
}
bool Preprocessor::loadImageSet(std::vector<cv::Mat> &image_set)
{
// load images
int count = 0;
DIR* dirp = opendir(refdir_.c_str());
struct dirent* dp;
while ((dp = readdir(dirp)) != NULL)
{
std::string fileName = std::string(dp->d_name);
if(fileName != ".." && fileName != ".")
{
std::string imgFile = refdir_ + fileName;
cv::Mat img = cv::imread(imgFile, CV_LOAD_IMAGE_COLOR);
if(! img.data ) // Check for invalid input
{
std::cout << "Could not open image: " << image_ << std::endl ;
return false;
}
image_set.push_back(img);
count++;
if(count == numberOfImagesToUse)
break;
}
}
std::cout << image_set.size( )<<" input images loaded." << std::endl;
closedir(dirp);
//match all images to image 0
for(unsigned i = 1; i < image_set.size();i++)
{
// histMatchRGB(image_set.at(i),cv::Mat_<uchar>::ones(image_set.at(1).size()), image_set.at(0),cv::Mat_<uchar>::ones(image_set.at(1).size()));
//cv::imwrite("out/after.jpg", image_set.at(1));
}
return true;
}
void Preprocessor::matchImages(std::vector<cv::Mat> &image_set, std::vector<cv::Mat> &transformed, std::vector<cv::Mat> &transformed_gray_set)
{
bool eq_hist = false;
Mat temp;
Mat first_img = image_set.at(0);
Mat next_img;
const int maxNumKeypoints = 10000;
const unsigned int minNumGoodMatches = 1000;
transformed.push_back(first_img);
cv::cvtColor(image_set.at(0), temp, CV_BGR2GRAY);
if(eq_hist)
cv::equalizeHist(temp, temp);
transformed_gray_set.push_back(temp);
first_img = image_set.at(0);
// resize(image_set.at(0), first_img, Size(image_set.at(0).cols*scale, image_set.at(0).rows*scale));
// int minHessian = 500;
// cv::SurfFeatureDetector detector( minHessian, 2, 2, false, false);
cv::FastFeatureDetector detector(40, true);
// cv::GFTTDetector detector( int maxCorners=1000, double qualityLevel=0.01, double minDistance=1,
// int blockSize=3, bool useHarrisDetector=false, double k=0.04 );
cv::SurfDescriptorExtractor extractor;
cv::FlannBasedMatcher matcher;
// cv::BFMatcher matcher = cv::BFMatcher(cv::NORM_HAMMING, true);
std::vector< DMatch > matches;
std::vector<KeyPoint> keypts_next, keypts_first;
std::vector<KeyPoint> keypts_next_extracted, keypts_first_extracted;
Mat descriptors_next, descriptors_first;
detector.detect( first_img, keypts_first_extracted );
int randm = rand() % keypts_first_extracted.size();
keypts_first.push_back(keypts_first_extracted.at(randm));
while(keypts_first.size() < (int)maxNumKeypoints && maxNumKeypoints > 0 )
{
//select random keypoints for homography estimation
randm = rand() % keypts_first_extracted.size();
keypts_first.push_back(keypts_first_extracted.at(randm));
}
extractor.compute( first_img, keypts_first, descriptors_first );
for(unsigned int frames = 1; frames < image_set.size(); frames++)
{
cout << "Transforming frame #" << frames << endl;
next_img = image_set.at(frames);
// resize(image_set.at(0), next_img, Size(image_set.at(0).cols*scale, image_set.at(0).rows*scale));
//-- Step 1: Detect the keypoints using SURF Detector
detector.detect( next_img, keypts_next_extracted );
keypts_next.clear();
randm = rand() % keypts_next_extracted.size();
keypts_next.push_back(keypts_next_extracted.at(randm));
while(keypts_next.size() < (int)maxNumKeypoints && maxNumKeypoints > 0)
{
//select random keypoints for homography estimation
randm = rand() % keypts_next_extracted.size();
keypts_next.push_back(keypts_next_extracted.at(randm));
}
// Draw position of keypoints (for debugging):
// for(unsigned i = 0; i < keypts_next.size(); i++)
// {
// int x,y;
//
// x = keypts_next.at(i).pt.x;
// y = keypts_next.at(i).pt.y;
//
// if(x > 0 && x < next_img.cols && y > 0 && y < next_img.rows)
// {
// Vec3b &v = next_img.at<Vec3b>(y,x);
//
// v[0] = 0;
// v[1] = 0;
// v[2] = 255;
// }
// }
//
// cv::namedWindow( "Fore", CV_WINDOW_NORMAL);
// cv::imshow("Fore", next_img);
// cv::waitKey(0);
//-- Step 2: Calculate descriptors (feature vectors)
extractor.compute( next_img, keypts_next, descriptors_next );
//-- Step 3: Matching descriptor vectors using FLANN matcher
matcher.match( descriptors_next, descriptors_first, matches );
double max_dist = 0; double min_dist = 100;
cout << "using " << descriptors_next.rows << " of " << keypts_next_extracted.size() << " Keypoints"<< endl;
//-- Quick calculation of max and min distances between keypoints
for( int i = 0; i < descriptors_next.rows; i++ )
{
double dist = matches[i].distance;
if( dist < min_dist ) min_dist = dist;
if( dist > max_dist ) max_dist = dist;
}
cout << "max distance: " << max_dist << endl;
cout << "min distance: " << min_dist << endl;
//-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
std::vector< DMatch > good_matches;
if(min_dist == 0)
min_dist = 0.05;
for( int i = 0; i < descriptors_next.rows; i++ )
{
if( matches[i].distance < 2*min_dist )
{
good_matches.push_back( matches[i]);
}
}
int prev_factor = 2;
int cur_factor;
cout << "found " << good_matches.size() << " good matches!"<< endl;
while(good_matches.size() < minNumGoodMatches)
{
//we need at least 4 matches for a homography
//but use some more, since the moving objects in the image may produce some outliers
//so add some other matches, which have not been added yet:
cur_factor = prev_factor+1;
for( int i = 0; i < descriptors_next.rows; i++ )
{
if( matches[i].distance > prev_factor*min_dist && matches[i].distance < cur_factor*min_dist)
{
good_matches.push_back( matches[i]);
if(good_matches.size() >= minNumGoodMatches)
break;
}
}
prev_factor = cur_factor;
}
//-- Localize the object
std::vector<Point2f> next;
std::vector<Point2f> first;
for(unsigned int i = 0; i < good_matches.size(); i++ )
{
//-- Get the keypoints from the good matches
next.push_back( keypts_next[ good_matches[i].queryIdx ].pt );
first.push_back( keypts_first[ good_matches[i].trainIdx ].pt );
}
Mat H = findHomography( next, first, CV_RANSAC, 3);
// cout << "H: "<<H << endl;
//// create matching image:
//-- Get the corners from the image_1 ( the object to be "detected" )
std::vector<Point2f> next_corners(4);
next_corners[0] = cvPoint(0,0); next_corners[1] = cvPoint( next_img.cols, 0 );
next_corners[2] = cvPoint( next_img.cols, next_img.rows ); next_corners[3] = cvPoint( 0, next_img.rows );
std::vector<Point2f> first_corners(4);
perspectiveTransform( next_corners, first_corners, H);
// COLORED
warpPerspective(next_img, temp, H, Size(image_set.at(0).cols, image_set.at(0).rows),INTER_LINEAR, BORDER_CONSTANT);
transformed.push_back(temp);
// GRAYSCALE
cv::cvtColor(image_set.at(frames), next_img, CV_BGR2GRAY);
if(eq_hist)
cv::equalizeHist(next_img, next_img);
warpPerspective(next_img, temp, H, Size(image_set.at(0).cols, image_set.at(0).rows),INTER_LINEAR, BORDER_CONSTANT, 255);
transformed_gray_set.push_back(temp);
}
}
// Compute histogram and CDF for an image with mask
void Preprocessor::do1ChnHist(const cv::Mat &_i, const cv::Mat &mask, double* h, double* cdf)
{
cv::Mat _t = _i.reshape(1,1);
cv::Mat _tm;
mask.copyTo(_tm);
_tm = _tm.reshape(1,1);
for(int p=0;p<_t.cols;p++)
{
if(_tm.at<uchar>(0,p) > 0)
{
uchar c = _t.at<uchar>(0,p);
h[c] += 1.0;
}
}
//normalize hist
Mat _tmp(1,256,CV_64FC1,h);
double minVal,maxVal;
cv::minMaxLoc(_tmp,&minVal,&maxVal);
_tmp = _tmp / maxVal;
cdf[0] = h[0];
for(int j=1;j<256;j++)
{
cdf[j] = cdf[j-1]+h[j];
}
//normalize CDF
_tmp.data = (uchar*)cdf;
cv::minMaxLoc(_tmp,&minVal,&maxVal);
_tmp = _tmp / maxVal;
}
//#define BTM_DEBUG
// match histograms of 'src' to that of 'dst', according to both masks
void Preprocessor::histMatchRGB(cv::Mat& src, const cv::Mat& src_mask, const cv::Mat& dst, const cv::Mat& dst_mask)
{
#ifdef BTM_DEBUG
namedWindow("original source",CV_WINDOW_AUTOSIZE);
imshow("original source",src);
namedWindow("original query",CV_WINDOW_AUTOSIZE);
imshow("original query",dst);
#endif
vector<Mat> chns;
split(src,chns);
vector<Mat> chns1;
split(dst,chns1);
Mat src_hist = Mat::zeros(1,256,CV_64FC1);
Mat dst_hist = Mat::zeros(1,256,CV_64FC1);
Mat src_cdf = Mat::zeros(1,256,CV_64FC1);
Mat dst_cdf = Mat::zeros(1,256,CV_64FC1);
Mat Mv(1,256,CV_8UC1);
uchar* M = Mv.ptr<uchar>();
for(int i=0;i<3;i++) {
src_hist.setTo(0);
dst_hist.setTo(0);
src_cdf.setTo(0);
src_cdf.setTo(0);
double* _src_cdf = src_cdf.ptr<double>();
double* _dst_cdf = dst_cdf.ptr<double>();
double* _src_hist = src_hist.ptr<double>();
double* _dst_hist = dst_hist.ptr<double>();
do1ChnHist(chns[i],src_mask,_src_hist,_src_cdf);
do1ChnHist(chns1[i],dst_mask,_dst_hist,_dst_cdf);
uchar last = 0;
for(int j=0;j<src_cdf.cols;j++) {
double F1j = _src_cdf[j];
for(uchar k = last; k<dst_cdf.cols; k++) {
double F2k = _dst_cdf[k];
if(abs(F2k - F1j) < HISTMATCH_EPSILON || F2k > F1j) {
M[j] = k;
last = k;
break;
}
}
}
Mat lut(1,256,CV_8UC1,M);
LUT(chns[i],lut,chns[i]);
}
Mat res;
merge(chns,res);
#ifdef BTM_DEBUG
namedWindow("matched",CV_WINDOW_AUTOSIZE);
imshow("matched",res);
waitKey(0);
#endif
res.copyTo(src);
}