-
Notifications
You must be signed in to change notification settings - Fork 2
/
alignmentmatrixcalc.cpp
640 lines (531 loc) · 18.3 KB
/
alignmentmatrixcalc.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/*
* Target Detection
*
* Copyright (C) Volkan Salma volkansalma@gmail.com
* Birol Kuyumcu bluekid70@gmail.com
* GPL v3 - https://github.com/birolkuyumcu/TargetDetection
*/
#include "alignmentmatrixcalc.h"
/* constructor sets default settings
*/
AlignmentMatrixCalc::AlignmentMatrixCalc()
{
exc.setModuleName("AlignmentMatrixCalc");
hMethod = featureBased;
keyRetainFactor = 0.75;
homographyCalcMethod = CV_RANSAC;
ransacReprojThreshold = 3;
matchType = normalMatch;
maxRatio = 0.50;
maxRadius = 100;
setDetectorSimple("SURF");
setDescriptorSimple("SURF");
setMatcherSimple("BruteForce-L1");
setMatcherSimple("BruteForce-L1");
isHomographyCalc=false;
stage = firstPass;
numOfPointsMin = 50;
errorCount=0;
flowErrorThreshold=3;
if(!cv::initModule_nonfree())
{
exc.showException("Compiled without Non-free Option!" );
}
}
/* inputImage ; always same size ,single channel and same depth CV_8U - 8-bit unsigned integers ( 0..255 )
* main calculation method
* gets frame and run with respect to stage of process
* if firstPass ; run init()
* elseif secondPass ; if error count above the threshold value bact to firstPass
* else - secondPass or onGoing stage - run();
* if run() returns true calculate homography with respect to HomograpyMethod
* else isHomographyCalc set to false;
*
**/
void AlignmentMatrixCalc::process(cv::Mat &inputImage)
{
if(inputImage.empty())
{
exc.showException("Empty Frame..." );
return;
}
if(stage == secondPass)
{
errorCount++;
if(errorCount >= 4)
{
stage=firstPass;
errorCount=0;
}
}
if(stage == firstPass)
{
init(inputImage);
}
else // secondPass or onGoing stage
{
if(stage == onGoing) // Third and so on passes
{
if(hMethod == featureBased)
{
prevFrame = currentFrame;
keypointsPrev = keypointsCurrent;
descriptorsPrev = descriptorsCurrent;
}
else if(hMethod == flowBased)
{
prevFrame = currentFrame;
// pointsPrev = pointsCurrent;
}
}
inputImage.copyTo(currentFrame);
if(run())
{
if(hMethod == featureBased)
{
featureBasedHomography();
}
else
{
flowBasedHomography();
}
}
else // false
{
//wayBack();
isHomographyCalc = false;
// return;
}
}
}
/*delete buffers
*set AlignmentMatrixCalc to initial state
* Not used, can be removed...
*/
void AlignmentMatrixCalc::reset()
{
prevFrame.release();
}
/* Internally used
* firstPass stage of calculation
*/
void AlignmentMatrixCalc::init(cv::Mat &frame)
{
qDebug()<<"Init\n\n";
frame.copyTo(prevFrame);
if(hMethod == featureBased)
{
detector->detect(prevFrame, keypointsPrev);
cv::KeyPointsFilter::retainBest(keypointsPrev, keyRetainFactor*keypointsPrev.size() );
// if enough points retained
if(keypointsPrev.size() >= numOfPointsMin)
{
stage = secondPass;
descriptor->compute(prevFrame, keypointsPrev, descriptorsPrev);
}
}
else if(hMethod == flowBased)
{
detector->detect(prevFrame, keypointsPrev);
cv::KeyPointsFilter::retainBest(keypointsPrev, keyRetainFactor*keypointsPrev.size() );
// if enough points retained
if(keypointsPrev.size() >= numOfPointsMin)
{
stage = secondPass;
pointsPrev.clear();
for(unsigned int i = 0; i<keypointsPrev.size(); i++)
{
pointsPrev.push_back(keypointsPrev[i].pt);
}
cv::cornerSubPix(prevFrame, pointsPrev, cv::Size(5,5), cv::Size(-1,-1),
cv::TermCriteria(cv::TermCriteria::MAX_ITER+cv::TermCriteria::EPS, 30, 0.1));
}
}
}
/* Internally used
* if current frame points ready returns true
* if not retruns false
**/
bool AlignmentMatrixCalc::run()
{
bool returnValue=true;
if(hMethod == featureBased)
{
// detect keyoints for current frame
detector->detect(currentFrame, keypointsCurrent);
// select best of them with respect to keyRetainFactor
cv::KeyPointsFilter::retainBest(keypointsCurrent, keyRetainFactor*keypointsCurrent.size() );
// if enough points retained
if(keypointsCurrent.size() >= numOfPointsMin)
{
stage = onGoing;
descriptor->compute(currentFrame, keypointsCurrent, descriptorsCurrent);
}
else
{
stage = secondPass;
returnValue=false;
}
}
else if(hMethod == flowBased)
{
detector->detect(prevFrame, keypointsPrev);
// qDebug()<<"Before retainBest :"<<keypointsPrev.size();
cv::KeyPointsFilter::retainBest(keypointsPrev, keyRetainFactor*keypointsPrev.size() );
// cv::KeyPointsFilter::retainBest(keypointsPrev, 80 );
// qDebug()<<"After retainBest :"<<keypointsPrev.size();
if(keypointsPrev.size() >= numOfPointsMin)
{
stage = onGoing;
pointsPrev.clear();
for(unsigned int i=0; i < keypointsPrev.size(); i++)
{
pointsPrev.push_back(keypointsPrev[i].pt);
}
cv::cornerSubPix(prevFrame, pointsPrev, cv::Size(5, 5), cv::Size(-1,-1),
cv::TermCriteria(cv::TermCriteria::MAX_ITER + cv::TermCriteria::EPS,30,0.1));
}
else
{
stage = secondPass;
returnValue=false;
}
}
return returnValue;
}
/* Calculation of Homography for feature based
*
**/
void AlignmentMatrixCalc::featureBasedHomography()
{
std::vector<cv::DMatch> matchesPrevToCurrent;
std::vector<cv::DMatch> matchesCurrentToPrev;
std::vector<std::vector<cv::DMatch> > kmatchesPrevToCurrent;
std::vector<std::vector<cv::DMatch> > kmatchesCurrentToPrev;
std::vector<cv::DMatch> matchesPassed;
// Matching Section begin
if( matchType == normalMatch )
{
matcher->match( descriptorsPrev, descriptorsCurrent, matchesPrevToCurrent );
matcher->match( descriptorsCurrent, descriptorsPrev, matchesCurrentToPrev );
// Symmetry Test start
symmetryTest(matchesPrevToCurrent, matchesCurrentToPrev, matchesPassed);
}
else if( matchType == knnMatch)
{
// qDebug()<<"Match : "<<keypointsCurrent.size()<<" "<<keypointsPrev.size()<<"\n";
matcher->knnMatch(descriptorsPrev, descriptorsCurrent, kmatchesPrevToCurrent,2);
// qDebug()<<"Ratio Test 1 :"<<kmatchesPrevToCurrent.size()<<"\n";
ratioTest(kmatchesPrevToCurrent);
// qDebug()<<"Ratio Test 1 End :"<<kmatchesPrevToCurrent.size()<<"\n";
matcher->knnMatch(descriptorsCurrent,descriptorsPrev, kmatchesCurrentToPrev, 2);
// qDebug()<<"Ratio Test 2 :"<<kmatchesCurrentToPrev.size()<<"\n";
ratioTest(kmatchesCurrentToPrev);
// qDebug()<<"Ratio Test 2 End :"<<kmatchesCurrentToPrev.size()<<"\n";
// Symmetry Test not working for knn
//matchesPassed=matchesPrevToCurrent;
symmetryTest(kmatchesPrevToCurrent,kmatchesCurrentToPrev,matchesPassed);
// qDebug()<<"Sym Test :"<<matchesPassed.size()<<"\n";
}
else if( matchType == radiusMatch)
{
// there is no documentation
// matcher->radiusMatch(descriptorsPrev, descriptorsCurrent, kmatchesPrevToCurrent,maxRadius );
// work but there is no matching back for any maxRadius
//convertRMatches(kmatchesCurrentToPrev,matchesPassed);
exc.showException("radiusMatch not working Dont use it!" );
}
// Matching Section end
isHomographyCalc = false;
pointsPrev.clear();
pointsCurrent.clear();
// Conversition of matched Keypoints to points
for (int p = 0; p < (int)matchesPassed.size(); ++p)
{
pointsPrev.push_back(keypointsPrev[matchesPassed[p].queryIdx].pt);
pointsCurrent.push_back(keypointsCurrent[matchesPassed[p].trainIdx].pt);
}
// if enough matched points exist
if(pointsPrev.size() >= 4 && pointsCurrent.size() >= 4)
{
// Sub-pixsel Accuracy
cv::cornerSubPix(prevFrame, pointsPrev, cv::Size(5,5), cv::Size(-1,-1),
cv::TermCriteria(cv::TermCriteria::MAX_ITER+cv::TermCriteria::EPS,30,0.1));
cv::cornerSubPix(currentFrame, pointsCurrent, cv::Size(5,5), cv::Size(-1,-1),
cv::TermCriteria(cv::TermCriteria::MAX_ITER+cv::TermCriteria::EPS,30,0.1));
homography = cv::findHomography(pointsPrev, pointsCurrent, homographyCalcMethod,
ransacReprojThreshold);
/*
cv::findHomography can return empty matrix in some cases.
This seems happen only when cv::RANSAC flag is passed.
check the computed homography before using it
*/
if(!homography.empty())
{
if(isHomographyValid()) //
{
isHomographyCalc = true;
}
}
}
if(isHomographyCalc == false)
{
// if valid homography not calculated returns to a second stage....
stage=secondPass;
}
}
/* Calculation of Homography for flow based
*
**/
void AlignmentMatrixCalc::flowBasedHomography()
{
std::vector<uchar>status;
std::vector<float>err;
std::vector<cv::Point2f>tempPrev;
std::vector<cv::Point2f>tempCurrent;
isHomographyCalc=false;
pointsCurrent.clear();
// flow calculation
calcOpticalFlowPyrLK(prevFrame, currentFrame, pointsPrev, pointsCurrent, status, err);
qDebug()<<"\n\n Prev Frame Features: "<<pointsPrev.size();
// filtering flow points by threshold
for (unsigned int i=0; i < pointsPrev.size(); i++)
{
if(status[i] && err[i] <= flowErrorThreshold)
{
tempPrev.push_back(pointsPrev[i]);
tempCurrent.push_back(pointsCurrent[i]);
}
qDebug()<<status[i]<<" "<<err[i]<<"\n";
}
qDebug()<<"After Flow Filtered Features : "<<tempCurrent.size();
// if enough flow points exist
if(tempPrev.size() >= 4 && tempCurrent.size() >= 4)
{
homography = cv::findHomography(tempPrev, tempCurrent, homographyCalcMethod,
ransacReprojThreshold);
/*
cv::findHomography can return empty matrix in some cases.
This seems happen only when cv::RANSAC flag is passed.
check the computed homography before using it
*/
if(!homography.empty())
{
if(isHomographyValid()) //
{
isHomographyCalc = true;
}
}
}
if(isHomographyCalc == false)
{
// if valid homography not calculated returns to a second stage....
stage=secondPass;
}
}
// Setters
/* Feature Detector setter used for both of them flowbased featurbased
* setting by cv::Ptr
**/
void AlignmentMatrixCalc::setDetector(cv::Ptr<cv::FeatureDetector> idetector)
{
detector = idetector;
}
/* Feature Detector setter used for both of them flowbased featurbased
* setting by Name
**/
void AlignmentMatrixCalc::setDetectorSimple(QString detectorName)
{
setDetector(cv::FeatureDetector::create(detectorName.toStdString()));
}
/* Feature Detector Discriptor setter used for featurbased
* setting by cv::Ptr
**/
void AlignmentMatrixCalc::setDescriptor(cv::Ptr<cv::DescriptorExtractor> idescriptor)
{
descriptor = idescriptor;
}
/* Feature Detector Discriptor setter used for featurbased
* setting by Name
**/
void AlignmentMatrixCalc::setDescriptorSimple(QString descriptorName)
{
setDescriptor(cv::DescriptorExtractor::create(descriptorName.toStdString()));
}
/* Discriptor Matcher setter used for featurbased
* setting by cv::Ptr
**/
void AlignmentMatrixCalc::setMatcher(cv::Ptr<cv::DescriptorMatcher> imatcher)
{
matcher = imatcher;
}
/* Discriptor Matcher setter used for featurbased
* setting by Name
**/
void AlignmentMatrixCalc::setMatcherSimple(QString matcherName)
{
setMatcher(cv::DescriptorMatcher::create(matcherName.toStdString()));
}
/* HomograpyMethod means that calculation of homography based on
* for featureBased ; find feature of each frame and calculete homography by using matching of them
* for flowBased ; find feature of first Frame then find flow at second frame and calculete homography by using these pairs
**/
void AlignmentMatrixCalc::setHomographyMethod(HomograpyMethod ihMethod)
{
hMethod = ihMethod;
}
/* Homography Calculation Method see findHomography documentation
– 0 - a regular method using all the points
– CV_RANSAC - RANSAC-based robust method
– CV_LMEDS - Least-Median robust method
*/
void AlignmentMatrixCalc::setHomographyCalcMethod(int ihomographyCalcMethod)
{
homographyCalcMethod = ihomographyCalcMethod;
}
/* Retruns true if homography calculated and set to gHomography
* else retruns false
**/
bool AlignmentMatrixCalc::getHomography(cv::Mat &gHomography)
{
if(isHomographyCalc)
{
gHomography = homography;
}
return isHomographyCalc;
}
/* MatchingType for featurebased Homography calculation
* for detail refer to OpenCv Documentation
* radiusMatch not working - OpenCv issue -
**/
void AlignmentMatrixCalc::setMatchingType(MatchingType iType)
{
matchType = iType;
}
/* Symmetry Test ; matching accepted if reverse matching reverse of it
* symmetrical matching scheme imposing that, for a match pair to be accepted, both points must be the best matching feature of the other:
*
**/
void AlignmentMatrixCalc::symmetryTest(std::vector<cv::DMatch> &matchesPrevToCurrent, std::vector<cv::DMatch> &matchesCurrentToPrev, std::vector<cv::DMatch> &matchesPassed)
{
for( size_t i = 0; i < matchesPrevToCurrent.size(); i++ )
{
cv::DMatch forward = matchesPrevToCurrent[i];
cv::DMatch backward = matchesCurrentToPrev[forward.trainIdx];
if( backward.trainIdx == forward.queryIdx && forward.trainIdx==backward.queryIdx)
{
matchesPassed.push_back( forward );
}
}
}
/* Symmetry Test ; matching's best match accepted if reverse matching's best match reverse of it
* symmetrical matching scheme imposing that, for a match pair to be accepted, both points must be the other:
**/
void AlignmentMatrixCalc::symmetryTest(std::vector<std::vector<cv::DMatch> >&kmatchesPrevToCurrent,std::vector<std::vector<cv::DMatch> >&kmatchesCurrentToPrev,std::vector< cv::DMatch >& matchesPassed)
{
for(std::vector<std::vector<cv::DMatch> >::iterator mPi= kmatchesPrevToCurrent.begin(); mPi != kmatchesPrevToCurrent.end(); ++mPi)
{
if(mPi->size() < 2 )
{
continue;
}
for(std::vector<std::vector<cv::DMatch> >::iterator mCi= kmatchesCurrentToPrev.begin();
mCi != kmatchesCurrentToPrev.end();
++mCi)
{
if(mCi->size() < 2 )
{
continue;
}
cv::DMatch forward = (*mPi)[0]; // [0] best
cv::DMatch backward = (*mCi)[0];
if((forward.queryIdx == backward.trainIdx) && (backward.queryIdx == forward.trainIdx) )
{
matchesPassed.push_back(forward);
break;
}
}
}
}
// for knnMatch
/* for each feature point, we have two candidate matches in the other view. These are
* the two best ones based on the distance between their descriptors. If this measured distance
is very low for the best match, and much larger for the second best match, we can safely
accept the first match as a good one since it is unambiguously the best choice. Reciprocally,
if the two best matches are relatively close in distance, then there exists a possibility that we
make an error if we select one or the other. In this case, we should reject both matches.
*
**/
void AlignmentMatrixCalc::ratioTest(std::vector<std::vector<cv::DMatch> > &kmatches)
{
for(std::vector<std::vector<cv::DMatch> >::iterator mi=kmatches.begin();
mi != kmatches.end();
++mi)
{
if(mi->size() > 1)
{
const cv::DMatch& best = (*mi)[0];
const cv::DMatch& good = (*mi)[1];
assert(best.distance <= good.distance);
float ratio = (best.distance / good.distance);
if (ratio > maxRatio)
{
mi->clear();
}
}
else
{
mi->clear();
}
}
}
/* check calculated homography
* calculate a aligned corners by using homography
* calculate aligned rows and cols
* if dfference over the 0.1 set to false
**/
bool AlignmentMatrixCalc::isHomographyValid()
{
std::vector<cv::Point2f> inputCorners(4);
inputCorners[0] = cvPoint(0,0);
inputCorners[1] = cvPoint( prevFrame.cols, 0 );
inputCorners[2] = cvPoint( prevFrame.cols, prevFrame.rows );
inputCorners[3] = cvPoint( 0, prevFrame.rows );
std::vector<cv::Point2f> alignedCorners(4);
perspectiveTransform( inputCorners, alignedCorners, homography);
float upDeltaX = fabs(alignedCorners[0].x-alignedCorners[1].x);
float downDeltaX = fabs(alignedCorners[2].x-alignedCorners[3].x);
float upDeltaY = fabs(alignedCorners[0].y-alignedCorners[3].y);
float downDeltaY = fabs(alignedCorners[1].y-alignedCorners[2].y);
float alignedCols=(upDeltaX+downDeltaX)/2;
float alignedRows=(upDeltaY+downDeltaY)/2;
float colsDifference=fabs(alignedCols - prevFrame.cols) / prevFrame.cols;
float rowsDifference=fabs(alignedRows - prevFrame.rows) / prevFrame.rows;
if( colsDifference < 0.1 && rowsDifference < 0.1 )
{
isHomographyCalc = true;
}
else
{
isHomographyCalc = false;
qDebug()<<"Homography Matrix is Invalid : "<<colsDifference<<" "<<rowsDifference ;
}
return isHomographyCalc;
}
/* Not used - crash the code
**/
void AlignmentMatrixCalc::wayBack()
{
if(hMethod == featureBased)
{
// return;
currentFrame = prevFrame;
keypointsCurrent = keypointsPrev;
descriptorsCurrent = descriptorsPrev;
}
else if(hMethod == flowBased)
{
currentFrame = prevFrame;
pointsCurrent = pointsPrev;
}
}