-
Notifications
You must be signed in to change notification settings - Fork 44
/
CMT.cpp
720 lines (630 loc) · 26.1 KB
/
CMT.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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
#include "CMT.h"
#include <stdio.h>
#define _USE_MATH_DEFINES
#include <cmath>
#if __cplusplus < 201103L //test if c++11
#include <limits>
#ifndef NAN
//may not be correct on all compilator, DON'T USE the flag FFAST-MATH
#define NAN std::numeric_limits<float>::quiet_NaN()
template <typename T>
bool isnan(T d)
{
return d != d;
}
#endif
#endif
void inout_rect(const std::vector<cv::KeyPoint>& keypoints, cv::Point2f topleft, cv::Point2f bottomright, std::vector<cv::KeyPoint>& in, std::vector<cv::KeyPoint>& out)
{
for(unsigned int i = 0; i < keypoints.size(); i++)
{
if(keypoints[i].pt.x > topleft.x && keypoints[i].pt.y > topleft.y && keypoints[i].pt.x < bottomright.x && keypoints[i].pt.y < bottomright.y)
in.push_back(keypoints[i]);
else out.push_back(keypoints[i]);
}
}
void track(cv::Mat im_prev, cv::Mat im_gray, const std::vector<std::pair<cv::KeyPoint, int> >& keypointsIN, std::vector<std::pair<cv::KeyPoint, int> >& keypointsTracked, std::vector<unsigned char>& status, int THR_FB)
{
//Status of tracked keypoint - True means successfully tracked
status = std::vector<unsigned char>();
//for(int i = 0; i < keypointsIN.size(); i++)
// status.push_back(false);
//If at least one keypoint is active
if(keypointsIN.size() > 0)
{
std::vector<cv::Point2f> pts;
std::vector<cv::Point2f> pts_back;
std::vector<cv::Point2f> nextPts;
std::vector<unsigned char> status_back;
std::vector<float> err;
std::vector<float> err_back;
std::vector<float> fb_err;
for(unsigned int i = 0; i < keypointsIN.size(); i++)
pts.push_back(cv::Point2f(keypointsIN[i].first.pt.x,keypointsIN[i].first.pt.y));
//Calculate forward optical flow for prev_location
cv::calcOpticalFlowPyrLK(im_prev, im_gray, pts, nextPts, status, err);
//Calculate backward optical flow for prev_location
cv::calcOpticalFlowPyrLK(im_gray, im_prev, nextPts, pts_back, status_back, err_back);
//Calculate forward-backward error
for(unsigned int i = 0; i < pts.size(); i++)
{
cv::Point2f v = pts_back[i]-pts[i];
fb_err.push_back(sqrt(v.dot(v)));
}
//Set status depending on fb_err and lk error
for(unsigned int i = 0; i < status.size(); i++)
status[i] = (fb_err[i] <= THR_FB) & status[i];
keypointsTracked = std::vector<std::pair<cv::KeyPoint, int> >();
for(unsigned int i = 0; i < pts.size(); i++)
{
std::pair<cv::KeyPoint, int> p = keypointsIN[i];
if(status[i])
{
p.first.pt = nextPts[i];
keypointsTracked.push_back(p);
}
}
}
else keypointsTracked = std::vector<std::pair<cv::KeyPoint, int> >();
}
cv::Point2f rotate(cv::Point2f p, float rad)
{
if(rad == 0)
return p;
float s = sin(rad);
float c = cos(rad);
return cv::Point2f(c*p.x-s*p.y,s*p.x+c*p.y);
}
CMT::CMT()
{
detectorType = "Feature2D.BRISK";
descriptorType = "Feature2D.BRISK";
matcherType = "BruteForce-Hamming";
thrOutlier = 20;
thrConf = 0.75;
thrRatio = 0.8;
descriptorLength = 512;
estimateScale = true;
estimateRotation = true;
nbInitialKeypoints = 0;
isInitialized = false;
}
void CMT::initialise(cv::Mat im_gray0, cv::Point2f topleft, cv::Point2f bottomright)
{
//Initialise detector, descriptor, matcher
#if OPENCV_VERSION_CODE<OPENCV_VERSION(3,1,0)
detector = cv::Algorithm::create<cv::FeatureDetector>(detectorType.c_str());
descriptorExtractor = cv::Algorithm::create<cv::DescriptorExtractor>(descriptorType.c_str());
descriptorMatcher = cv::DescriptorMatcher::create(matcherType.c_str());
std::vector<std::string> list;
cv::Algorithm::getList(list);
#else
detector = cv::BRISK::create();
descriptorMatcher = cv::DescriptorMatcher::create(matcherType.c_str());
#endif
//Get initial keypoints in whole image
std::vector<cv::KeyPoint> keypoints;
detector->detect(im_gray0, keypoints);
//Remember keypoints that are in the rectangle as selected keypoints
std::vector<cv::KeyPoint> selected_keypoints;
std::vector<cv::KeyPoint> background_keypoints;
inout_rect(keypoints, topleft, bottomright, selected_keypoints, background_keypoints);
#if OPENCV_VERSION_CODE<OPENCV_VERSION(3,1,0)
descriptorExtractor->compute(im_gray0, selected_keypoints, selectedFeatures);
#else
detector->compute(im_gray0, selected_keypoints, selectedFeatures);
#endif
if(selected_keypoints.size() == 0)
{
printf("No keypoints found in selection");
return;
}
//Remember keypoints that are not in the rectangle as background keypoints
cv::Mat background_features;
#if OPENCV_VERSION_CODE<OPENCV_VERSION(3,1,0)
descriptorExtractor->compute(im_gray0, background_keypoints, background_features);
#else
detector->compute(im_gray0, background_keypoints, background_features);
#endif
//Assign each keypoint a class starting from 1, background is 0
selectedClasses = std::vector<int>();
for(unsigned int i = 1; i <= selected_keypoints.size(); i++)
selectedClasses.push_back(i);
std::vector<int> backgroundClasses;
for(unsigned int i = 0; i < background_keypoints.size(); i++)
backgroundClasses.push_back(0);
//Stack background features and selected features into database
featuresDatabase = cv::Mat(background_features.rows+selectedFeatures.rows, std::max(background_features.cols,selectedFeatures.cols), background_features.type());
if(background_features.cols > 0)
background_features.copyTo(featuresDatabase(cv::Rect(0,0,background_features.cols, background_features.rows)));
if(selectedFeatures.cols > 0)
selectedFeatures.copyTo(featuresDatabase(cv::Rect(0,background_features.rows,selectedFeatures.cols, selectedFeatures.rows)));
//Same for classes
classesDatabase = std::vector<int>();
for(unsigned int i = 0; i < backgroundClasses.size(); i++)
classesDatabase.push_back(backgroundClasses[i]);
for(unsigned int i = 0; i < selectedClasses.size(); i++)
classesDatabase.push_back(selectedClasses[i]);
//Get all distances between selected keypoints in squareform and get all angles between selected keypoints
squareForm = std::vector<std::vector<float> >();
angles = std::vector<std::vector<float> >();
for(unsigned int i = 0; i < selected_keypoints.size(); i++)
{
std::vector<float> lineSquare;
std::vector<float> lineAngle;
for(unsigned int j = 0; j < selected_keypoints.size(); j++)
{
float dx = selected_keypoints[j].pt.x-selected_keypoints[i].pt.x;
float dy = selected_keypoints[j].pt.y-selected_keypoints[i].pt.y;
lineSquare.push_back(sqrt(dx*dx+dy*dy));
lineAngle.push_back(atan2(dy, dx));
}
squareForm.push_back(lineSquare);
angles.push_back(lineAngle);
}
//Find the center of selected keypoints
cv::Point2f center(0,0);
for(unsigned int i = 0; i < selected_keypoints.size(); i++)
center += selected_keypoints[i].pt;
center *= (1.0/selected_keypoints.size());
//Remember the rectangle coordinates relative to the center
centerToTopLeft = topleft - center;
centerToTopRight = cv::Point2f(bottomright.x, topleft.y) - center;
centerToBottomRight = bottomright - center;
centerToBottomLeft = cv::Point2f(topleft.x, bottomright.y) - center;
//Calculate springs of each keypoint
springs = std::vector<cv::Point2f>();
for(unsigned int i = 0; i < selected_keypoints.size(); i++)
springs.push_back(selected_keypoints[i].pt - center);
//Set start image for tracking
im_prev = im_gray0.clone();
//Make keypoints 'active' keypoints
activeKeypoints = std::vector<std::pair<cv::KeyPoint,int> >();
for(unsigned int i = 0; i < selected_keypoints.size(); i++)
activeKeypoints.push_back(std::make_pair(selected_keypoints[i], selectedClasses[i]));
//Remember number of initial keypoints
nbInitialKeypoints = selected_keypoints.size();
isInitialized = true;
}
typedef std::pair<int,int> PairInt;
typedef std::pair<float,int> PairFloat;
template<typename T>
bool comparatorPair ( const std::pair<T,int>& l, const std::pair<T,int>& r)
{
return l.first < r.first;
}
template<typename T>
bool comparatorPairDesc ( const std::pair<T,int>& l, const std::pair<T,int>& r)
{
return l.first > r.first;
}
template <typename T>
T sign(T t)
{
if( t == 0 )
return T(0);
else
return (t < 0) ? T(-1) : T(1);
}
template<typename T>
T median(std::vector<T> list)
{
T val;
std::nth_element(&list[0], &list[0]+list.size()/2, &list[0]+list.size());
val = list[list.size()/2];
if(list.size()%2==0)
{
std::nth_element(&list[0], &list[0]+list.size()/2-1, &list[0]+list.size());
val = (val+list[list.size()/2-1])/2;
}
return val;
}
float findMinSymetric(const std::vector<std::vector<float> >& dist, const std::vector<bool>& used, int limit, int &i, int &j)
{
float min = dist[0][0];
i = 0;
j = 0;
for(int x = 0; x < limit; x++)
{
if(!used[x])
{
for(int y = x+1; y < limit; y++)
if(!used[y] && dist[x][y] <= min)
{
min = dist[x][y];
i = x;
j = y;
}
}
}
return min;
}
std::vector<Cluster> linkage(const std::vector<cv::Point2f>& list)
{
float inf = 10000000.0;
std::vector<bool> used;
for(unsigned int i = 0; i < 2*list.size(); i++)
used.push_back(false);
std::vector<std::vector<float> > dist;
for(unsigned int i = 0; i < list.size(); i++)
{
std::vector<float> line;
for(unsigned int j = 0; j < list.size(); j++)
{
if(i == j)
line.push_back(inf);
else
{
cv::Point2f p = list[i]-list[j];
line.push_back(sqrt(p.dot(p)));
}
}
for(unsigned int j = 0; j < list.size(); j++)
line.push_back(inf);
dist.push_back(line);
}
for(unsigned int i = 0; i < list.size(); i++)
{
std::vector<float> line;
for(unsigned int j = 0; j < 2*list.size(); j++)
line.push_back(inf);
dist.push_back(line);
}
std::vector<Cluster> clusters;
while(clusters.size() < list.size()-1)
{
int x, y;
float min = findMinSymetric(dist, used, list.size()+clusters.size(), x, y);
Cluster cluster;
cluster.first = x;
cluster.second = y;
cluster.dist = min;
cluster.num = (x < (int)list.size() ? 1 : clusters[x-list.size()].num) + (y < (int)list.size() ? 1 : clusters[y-list.size()].num);
used[x] = true;
used[y] = true;
int limit = list.size()+clusters.size();
for(int i = 0; i < limit; i++)
{
if(!used[i])
dist[i][limit] = dist[limit][i] = std::min(dist[i][x], dist[i][y]);
}
clusters.push_back(cluster);
}
return clusters;
}
void fcluster_rec(std::vector<int>& data, const std::vector<Cluster>& clusters, float threshold, const Cluster& currentCluster, int& binId)
{
int startBin = binId;
if(currentCluster.first >= (int)data.size())
fcluster_rec(data, clusters, threshold, clusters[currentCluster.first - data.size()], binId);
else data[currentCluster.first] = binId;
if(startBin == binId && currentCluster.dist >= threshold)
binId++;
startBin = binId;
if(currentCluster.second >= (int)data.size())
fcluster_rec(data, clusters, threshold, clusters[currentCluster.second - data.size()], binId);
else data[currentCluster.second] = binId;
if(startBin == binId && currentCluster.dist >= threshold)
binId++;
}
std::vector<int> binCount(const std::vector<int>& T)
{
std::vector<int> result;
for(unsigned int i = 0; i < T.size(); i++)
{
while(T[i] >= (int)result.size())
result.push_back(0);
result[T[i]]++;
}
return result;
}
int argmax(const std::vector<int>& list)
{
int max = list[0];
int id = 0;
for(unsigned int i = 1; i < list.size(); i++)
if(list[i] > max)
{
max = list[i];
id = i;
}
return id;
}
std::vector<int> fcluster(const std::vector<Cluster>& clusters, float threshold)
{
std::vector<int> data;
for(unsigned int i = 0; i < clusters.size()+1; i++)
data.push_back(0);
int binId = 0;
fcluster_rec(data, clusters, threshold, clusters[clusters.size()-1], binId);
return data;
}
bool comparatorPairSecond( const std::pair<int, int>& l, const std::pair<int, int>& r)
{
return l.second < r.second;
}
std::vector<int> argSortInt(const std::vector<int>& list)
{
std::vector<int> result(list.size());
std::vector<std::pair<int, int> > pairList(list.size());
for(int i = 0; i < list.size(); i++)
pairList[i] = std::make_pair(i, list[i]);
std::sort(&pairList[0], &pairList[0]+pairList.size(), comparatorPairSecond);
for(int i = 0; i < list.size(); i++)
result[i] = pairList[i].first;
return result;
}
void CMT::estimate(const std::vector<std::pair<cv::KeyPoint, int> >& keypointsIN, cv::Point2f& center, float& scaleEstimate, float& medRot, std::vector<std::pair<cv::KeyPoint, int> >& keypoints)
{
center = cv::Point2f(NAN,NAN);
scaleEstimate = NAN;
medRot = NAN;
//At least 2 keypoints are needed for scale
if(keypointsIN.size() > 1)
{
//sort
std::vector<PairInt> list;
for(unsigned int i = 0; i < keypointsIN.size(); i++)
list.push_back(std::make_pair(keypointsIN[i].second, i));
std::sort(&list[0], &list[0]+list.size(), comparatorPair<int>);
for(unsigned int i = 0; i < list.size(); i++)
keypoints.push_back(keypointsIN[list[i].second]);
std::vector<int> ind1;
std::vector<int> ind2;
for(unsigned int i = 0; i < list.size(); i++)
for(unsigned int j = 0; j < list.size(); j++)
{
if(i != j && keypoints[i].second != keypoints[j].second)
{
ind1.push_back(i);
ind2.push_back(j);
}
}
if(ind1.size() > 0)
{
std::vector<int> class_ind1;
std::vector<int> class_ind2;
std::vector<cv::KeyPoint> pts_ind1;
std::vector<cv::KeyPoint> pts_ind2;
for(unsigned int i = 0; i < ind1.size(); i++)
{
class_ind1.push_back(keypoints[ind1[i]].second-1);
class_ind2.push_back(keypoints[ind2[i]].second-1);
pts_ind1.push_back(keypoints[ind1[i]].first);
pts_ind2.push_back(keypoints[ind2[i]].first);
}
std::vector<float> scaleChange;
std::vector<float> angleDiffs;
for(unsigned int i = 0; i < pts_ind1.size(); i++)
{
cv::Point2f p = pts_ind2[i].pt - pts_ind1[i].pt;
//This distance might be 0 for some combinations,
//as it can happen that there is more than one keypoint at a single location
float dist = sqrt(p.dot(p));
float origDist = squareForm[class_ind1[i]][class_ind2[i]];
scaleChange.push_back(dist/origDist);
//Compute angle
float angle = atan2(p.y, p.x);
float origAngle = angles[class_ind1[i]][class_ind2[i]];
float angleDiff = angle - origAngle;
//Fix long way angles
if(fabs(angleDiff) > CV_PI)
angleDiff -= sign(angleDiff) * 2 * CV_PI;
angleDiffs.push_back(angleDiff);
}
scaleEstimate = median(scaleChange);
if(!estimateScale)
scaleEstimate = 1;
medRot = median(angleDiffs);
if(!estimateRotation)
medRot = 0;
votes = std::vector<cv::Point2f>();
for(unsigned int i = 0; i < keypoints.size(); i++)
votes.push_back(keypoints[i].first.pt - scaleEstimate * rotate(springs[keypoints[i].second-1], medRot));
//Compute linkage between pairwise distances
std::vector<Cluster> linkageData = linkage(votes);
//Perform hierarchical distance-based clustering
std::vector<int> T = fcluster(linkageData, thrOutlier);
//Count votes for each cluster
std::vector<int> cnt = binCount(T);
//Get largest class
int Cmax = argmax(cnt);
//Remember outliers
outliers = std::vector<std::pair<cv::KeyPoint, int> >();
std::vector<std::pair<cv::KeyPoint, int> > newKeypoints;
std::vector<cv::Point2f> newVotes;
for(unsigned int i = 0; i < keypoints.size(); i++)
{
if(T[i] != Cmax)
outliers.push_back(keypoints[i]);
else
{
newKeypoints.push_back(keypoints[i]);
newVotes.push_back(votes[i]);
}
}
keypoints = newKeypoints;
center = cv::Point2f(0,0);
for(unsigned int i = 0; i < newVotes.size(); i++)
center += newVotes[i];
center *= (1.0/newVotes.size());
}
}
}
//todo : n*log(n) by sorting the second array and dichotomic search instead of n^2
std::vector<bool> in1d(const std::vector<int>& a, const std::vector<int>& b)
{
std::vector<bool> result;
for(unsigned int i = 0; i < a.size(); i++)
{
bool found = false;
for(unsigned int j = 0; j < b.size(); j++)
if(a[i] == b[j])
{
found = true;
break;
}
result.push_back(found);
}
return result;
}
void CMT::processFrame(cv::Mat im_gray)
{
if (!isInitialized)
{
std::cout << "ERROR: Tracking window has been not initialized yet. ";
std::cout << "Please call CMT::initialise first." << std::endl;
return;
}
trackedKeypoints = std::vector<std::pair<cv::KeyPoint, int> >();
std::vector<unsigned char> status;
track(im_prev, im_gray, activeKeypoints, trackedKeypoints, status);
cv::Point2f center;
float scaleEstimate;
float rotationEstimate;
std::vector<std::pair<cv::KeyPoint, int> > trackedKeypoints2;
estimate(trackedKeypoints, center, scaleEstimate, rotationEstimate, trackedKeypoints2);
trackedKeypoints = trackedKeypoints2;
//Detect keypoints, compute descriptors
std::vector<cv::KeyPoint> keypoints;
cv::Mat features;
detector->detect(im_gray, keypoints);
#if OPENCV_VERSION_CODE<OPENCV_VERSION(3,1,0)
descriptorExtractor->compute(im_gray, keypoints, features);
#else
detector->compute(im_gray, keypoints, features);
#endif
//Create list of active keypoints
activeKeypoints = std::vector<std::pair<cv::KeyPoint, int> >();
//Get the best two matches for each feature
std::vector<std::vector<cv::DMatch> > matchesAll, selectedMatchesAll;
descriptorMatcher->knnMatch(features, featuresDatabase, matchesAll, 2);
//Get all matches for selected features
if(!isnan(center.x) && !isnan(center.y))
descriptorMatcher->knnMatch(features, selectedFeatures, selectedMatchesAll, selectedFeatures.rows);
std::vector<cv::Point2f> transformedSprings(springs.size());
for(int i = 0; i < springs.size(); i++)
transformedSprings[i] = scaleEstimate * rotate(springs[i], -rotationEstimate);
//For each keypoint and its descriptor
for(unsigned int i = 0; i < keypoints.size(); i++)
{
cv::KeyPoint keypoint = keypoints[i];
//First: Match over whole image
//Compute distances to all descriptors
std::vector<cv::DMatch> matches = matchesAll[i];
//Convert distances to confidences, do not weight
std::vector<float> combined;
for(unsigned int j = 0; j < matches.size(); j++)
combined.push_back(1 - matches[j].distance / descriptorLength);
std::vector<int>& classes = classesDatabase;
//Get best and second best index
int bestInd = matches[0].trainIdx;
int secondBestInd = matches[1].trainIdx;
//Compute distance ratio according to Lowe
float ratio = (1-combined[0]) / (1-combined[1]);
//Extract class of best match
int keypoint_class = classes[bestInd];
//If distance ratio is ok and absolute distance is ok and keypoint class is not background
if(ratio < thrRatio && combined[0] > thrConf && keypoint_class != 0)
activeKeypoints.push_back(std::make_pair(keypoint, keypoint_class));
//In a second step, try to match difficult keypoints
//If structural constraints are applicable
if(!(isnan(center.x) | isnan(center.y)))
{
//Compute distances to initial descriptors
std::vector<cv::DMatch> matches = selectedMatchesAll[i];
std::vector<float> distances(matches.size()), distancesTmp(matches.size());
std::vector<int> trainIndex(matches.size());
for(int j = 0; j < matches.size(); j++)
{
distancesTmp[j] = matches[j].distance;
trainIndex[j] = matches[j].trainIdx;
}
//Re-order the distances based on indexing
std::vector<int> idxs = argSortInt(trainIndex);
for(int j = 0; j < idxs.size(); j++)
distances[j] = distancesTmp[idxs[j]];
//Convert distances to confidences
std::vector<float> confidences(matches.size());
for(unsigned int j = 0; j < matches.size(); j++)
confidences[j] = 1 - distances[j] / descriptorLength;
//Compute the keypoint location relative to the object center
cv::Point2f relative_location = keypoint.pt - center;
//Compute the distances to all springs
std::vector<float> displacements(springs.size());
for(unsigned int j = 0; j < springs.size(); j++)
{
cv::Point2f p = (transformedSprings[j] - relative_location);
displacements[j] = sqrt(p.dot(p));
}
//For each spring, calculate weight
std::vector<float> combined(confidences.size());
for(unsigned int j = 0; j < confidences.size(); j++)
combined[j] = (displacements[j] < thrOutlier)*confidences[j];
std::vector<int>& classes = selectedClasses;
//Sort in descending order
std::vector<PairFloat> sorted_conf(combined.size());
for(unsigned int j = 0; j < combined.size(); j++)
sorted_conf[j] = std::make_pair(combined[j], j);
std::sort(&sorted_conf[0], &sorted_conf[0]+sorted_conf.size(), comparatorPairDesc<float>);
//Get best and second best index
int bestInd = sorted_conf[0].second;
int secondBestInd = sorted_conf[1].second;
//Compute distance ratio according to Lowe
float ratio = (1-combined[bestInd]) / (1-combined[secondBestInd]);
//Extract class of best match
int keypoint_class = classes[bestInd];
//If distance ratio is ok and absolute distance is ok and keypoint class is not background
if(ratio < thrRatio && combined[bestInd] > thrConf && keypoint_class != 0)
{
for(int i = activeKeypoints.size()-1; i >= 0; i--)
if(activeKeypoints[i].second == keypoint_class)
activeKeypoints.erase(activeKeypoints.begin()+i);
activeKeypoints.push_back(std::make_pair(keypoint, keypoint_class));
}
}
}
//If some keypoints have been tracked
if(trackedKeypoints.size() > 0)
{
//Extract the keypoint classes
std::vector<int> tracked_classes(trackedKeypoints.size());
for(unsigned int i = 0; i < trackedKeypoints.size(); i++)
tracked_classes[i] = trackedKeypoints[i].second;
//If there already are some active keypoints
if(activeKeypoints.size() > 0)
{
//Add all tracked keypoints that have not been matched
std::vector<int> associated_classes(activeKeypoints.size());
for(unsigned int i = 0; i < activeKeypoints.size(); i++)
associated_classes[i] = activeKeypoints[i].second;
std::vector<bool> notmissing = in1d(tracked_classes, associated_classes);
for(unsigned int i = 0; i < trackedKeypoints.size(); i++)
if(!notmissing[i])
activeKeypoints.push_back(trackedKeypoints[i]);
}
else activeKeypoints = trackedKeypoints;
}
//Update object state estimate
std::vector<std::pair<cv::KeyPoint, int> > activeKeypointsBefore = activeKeypoints;
im_prev = im_gray;
topLeft = cv::Point2f(NAN,NAN);
topRight = cv::Point2f(NAN,NAN);
bottomLeft = cv::Point2f(NAN,NAN);
bottomRight = cv::Point2f(NAN,NAN);
boundingbox = cv::Rect_<float>(NAN,NAN,NAN,NAN);
hasResult = false;
if(!(isnan(center.x) | isnan(center.y)) && activeKeypoints.size() > nbInitialKeypoints / 10)
{
hasResult = true;
topLeft = center + scaleEstimate*rotate(centerToTopLeft, rotationEstimate);
topRight = center + scaleEstimate*rotate(centerToTopRight, rotationEstimate);
bottomLeft = center + scaleEstimate*rotate(centerToBottomLeft, rotationEstimate);
bottomRight = center + scaleEstimate*rotate(centerToBottomRight, rotationEstimate);
float minx = std::min(std::min(topLeft.x,topRight.x),std::min(bottomRight.x, bottomLeft.x));
float miny = std::min(std::min(topLeft.y,topRight.y),std::min(bottomRight.y, bottomLeft.y));
float maxx = std::max(std::max(topLeft.x,topRight.x),std::max(bottomRight.x, bottomLeft.x));
float maxy = std::max(std::max(topLeft.y,topRight.y),std::max(bottomRight.y, bottomLeft.y));
boundingbox = cv::Rect_<float>(minx, miny, maxx-minx, maxy-miny);
}
}