-
Notifications
You must be signed in to change notification settings - Fork 2
/
detect.cpp
133 lines (93 loc) · 3.16 KB
/
detect.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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "leap.h"
#include <opencv2/opencv.hpp>
#include <opencv2/nonfree/features2d.hpp>
using namespace cv;
Mat display_left(480,640, CV_8UC3, Scalar(0,0,255));
Mat display_right(480,640, CV_8UC3, Scalar(0,0,255));
Mat display_match(480,640, CV_8UC3, Scalar(0,0,255));
Mat map(1000,1000, CV_8UC3, Scalar(0,0,0));
void render_callback(leap_t *leap, int key)
{
//imshow("left", display_left);
//imshow("right", display_right);
imshow("match", display_match);
//imshow("map", map);
}
void callback(leap_t *leap)
{
//FeatureDetector *feat = FeatureDetector::create("SURF");
//FeatureDetector *detector = FeatureDetector::create("FAST");
FastFeatureDetector detector(6);
//OrbFeatureDetector detector(5);
//SurfFeatureDetector detector(20);
SurfDescriptorExtractor ext;
FlannBasedMatcher matcher;
//BFMatcher matcher;
Mat left(leap->left);
Mat right(leap->right);
vector <DMatch> matches;
vector <DMatch> good_matches;
vector <KeyPoint> keypoints_left;
vector <KeyPoint> keypoints_right;
Mat descriptors_left, descriptors_right;
map -= Scalar(20,20,20);
//FAST(left, keypoints_left, 8, true);
//FAST(right, keypoints_right, 8, true);
detector.detect(left, keypoints_left);
detector.detect(right, keypoints_right);
if(keypoints_left.size() == 0)
return;
if(keypoints_right.size() == 0)
return;
ext.compute(left, keypoints_left, descriptors_left);
ext.compute(right, keypoints_right, descriptors_right);
matcher.match(descriptors_left, descriptors_right, matches);
if(matches.size() == 0)
return;
//drawKeypoints(left, keypoints_left, display_left);
//drawKeypoints(right, keypoints_right, display_right);
double max_dist = 0; double min_dist = 1000;
//-- Quick calculation of max and min distances between keypoints
for( int i = 0; i < descriptors_left.rows; i++ )
{ double dist = matches[i].distance;
if( dist < min_dist ) min_dist = dist;
if( dist > max_dist ) max_dist = dist;
}
for( int i = 0; i < (int)matches.size(); i++ )
{
int kp1 = matches[i].queryIdx;
int kp2 = matches[i].trainIdx;
float dx,dy;
KeyPoint p1,p2;
//printf( "Good Match [%d] Keypoint 1: %d -- Keypoint 2: %d \n", i, good_matches[i].queryIdx, good_matches[i].trainIdx );
p1 = keypoints_left[kp1];
p2 = keypoints_right[kp2];
//printf("%f,%f -> %f,%f\n", p1.pt.x, p1.pt.y, p2.pt.x, p2.pt.y);
dx = p1.pt.x - p2.pt.x;
dy = p1.pt.y - p2.pt.y;
if(fabs(dy) <= 10 && fabs(dx) > 20)
{
float distance = (fabs(dx)/200.0)*100.0;
//printf("%f\n", fabs(dx));
good_matches.push_back(matches[i]);
//printf("%f\n", distance);
// Draw on the map
circle(map, Point(500-p1.pt.x,1000-distance*10), 3, Scalar(0,255,0));
}
//if( matches[i].distance <= max(2*min_dist, 0.001) )
//good_matches.push_back( matches[i]);
}
if(good_matches.size() > 0)
drawMatches(left, keypoints_left, right, keypoints_right, good_matches, display_match);
}
int main()
{
leap_t *leap;
leap_open(&leap, &callback);
leap_diag(leap);
leap->render_callback = render_callback;
leap_run(leap);
}