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
| Mat objectImg = imread("forSearch.png", CV_LOAD_IMAGE_GRAYSCALE );
Mat sceneImg = imread("test.png", CV_LOAD_IMAGE_GRAYSCALE );
if(!objectImg.empty() && !sceneImg.empty())
{
std::vector<cv::KeyPoint> objectKeypoints;
std::vector<cv::KeyPoint> sceneKeypoints;
cv::Mat objectDescriptors;
cv::Mat sceneDescriptors;
////////////////////////////
// EXTRACT KEYPOINTS
////////////////////////////
// The detector can be any of (see OpenCV features2d.hpp):
// cv::FeatureDetector * detector = new cv::DenseFeatureDetector();
// cv::FeatureDetector * detector = new cv::FastFeatureDetector();
// cv::FeatureDetector * detector = new cv::GFTTDetector();
// cv::FeatureDetector * detector = new cv::MSER();
// cv::FeatureDetector * detector = new cv::ORB();
cv::FeatureDetector * detector = new cv::SIFT();
// cv::FeatureDetector * detector = new cv::StarFeatureDetector();
// cv::FeatureDetector * detector = new cv::SURF(600.0);
// cv::FeatureDetector * detector = new cv::BRISK();
detector->detect(objectImg, objectKeypoints);
printf("Object: %d keypoints detected in ms\n", (int)objectKeypoints.size());
detector->detect(sceneImg, sceneKeypoints);
printf("Scene: %d keypoints detected in ms\n", (int)sceneKeypoints.size());
////////////////////////////
// EXTRACT DESCRIPTORS
////////////////////////////
// The extractor can be any of (see OpenCV features2d.hpp):
// cv::DescriptorExtractor * extractor = new cv::BriefDescriptorExtractor();
// cv::DescriptorExtractor * extractor = new cv::ORB();
cv::DescriptorExtractor * extractor = new cv::SIFT();
// cv::DescriptorExtractor * extractor = new cv::SURF(600.0);
// cv::DescriptorExtractor * extractor = new cv::BRISK();
// cv::DescriptorExtractor * extractor = new cv::FREAK();
extractor->compute(objectImg, objectKeypoints, objectDescriptors);
printf("Object: %d descriptors extracted in ms\n", objectDescriptors.rows);
extractor->compute(sceneImg, sceneKeypoints, sceneDescriptors);
printf("Scene: %d descriptors extracted in ms\n", sceneDescriptors.rows);
////////////////////////////
// NEAREST NEIGHBOR MATCHING USING FLANN LIBRARY (included in OpenCV)
////////////////////////////
cv::Mat results;
cv::Mat dists;
std::vector<std::vector<cv::DMatch> > matches;
bool isBinaryDescriptors;
int k=2; // find the 2 nearest neighbors
bool useBFMatcher = false; // SET TO TRUE TO USE BRUTE FORCE MATCHER (may give better results with binary descriptors)
if(objectDescriptors.type()==CV_8U)
{
// Binary descriptors detected (from ORB, Brief, BRISK, FREAK)
printf("Binary descriptors detected...\n");
isBinaryDescriptors = true;
if(useBFMatcher)
{
cv::BFMatcher matcher(cv::NORM_HAMMING);
matcher.knnMatch(objectDescriptors, sceneDescriptors, matches, k);
}
else
{
// Create Flann LSH index
cv::flann::Index flannIndex(sceneDescriptors, cv::flann::LshIndexParams(12, 20, 2), cvflann::FLANN_DIST_HAMMING);
printf("Time creating FLANN LSH index = ms\n");
results = cv::Mat(objectDescriptors.rows, k, CV_32SC1); // Results index
dists = cv::Mat(objectDescriptors.rows, k, CV_32FC1); // Distance results are CV_32FC1 ?!?!? NOTE OpenCV doc is not clear about that...
// search (nearest neighbor)
flannIndex.knnSearch(objectDescriptors, results, dists, k, cv::flann::SearchParams() );
}
}
else
{
// assume it is CV_32F
printf("Float descriptors detected...\n");
isBinaryDescriptors = false;
if(useBFMatcher)
{
cv::BFMatcher matcher(cv::NORM_L2);
matcher.knnMatch(objectDescriptors, sceneDescriptors, matches, k);
}
else
{
// Create Flann KDTree index
cv::flann::Index flannIndex(sceneDescriptors, cv::flann::KDTreeIndexParams(), cvflann::FLANN_DIST_EUCLIDEAN);
printf("Time creating FLANN KDTree index = ms\n");
results = cv::Mat(objectDescriptors.rows, k, CV_32SC1); // Results index
dists = cv::Mat(objectDescriptors.rows, k, CV_32FC1); // Distance results are CV_32FC1
// search (nearest neighbor)
flannIndex.knnSearch(objectDescriptors, results, dists, k, cv::flann::SearchParams() );
}
}
printf("Time nearest neighbor search = ms\n");
////////////////////////////
// PROCESS NEAREST NEIGHBOR RESULTS
////////////////////////////
// Set gui data
/* obWidget.setData(objectKeypoints, objectDescriptors, objectImg, "", "");
sceneWidget.setData(sceneKeypoints, sceneDescriptors, sceneImg, "", "");
*/
// Find correspondences by NNDR (Nearest Neighbor Distance Ratio)
float nndrRatio = 0.6;
std::vector<cv::Point2f> mpts_1, mpts_2; // Used for homography
std::vector<int> indexes_1, indexes_2; // Used for homography
std::vector<uchar> outlier_mask; // Used for homography
// Check if this descriptor matches with those of the objects
if(!useBFMatcher)
{
for(int i=0; i<objectDescriptors.rows; ++i)
{
// Apply NNDR
//printf("q=%d dist1=%f dist2=%f\n", i, dists.at<float>(i,0), dists.at<float>(i,1));
if(isBinaryDescriptors || //Binary, just take the nearest
dists.at<float>(i,0) <= nndrRatio * dists.at<float>(i,1))
{
mpts_1.push_back(objectKeypoints.at(i).pt);
indexes_1.push_back(i);
mpts_2.push_back(sceneKeypoints.at(results.at<int>(i,0)).pt);
indexes_2.push_back(results.at<int>(i,0));
}
}
}
else
{
for(unsigned int i=0; i<matches.size(); ++i)
{
// Apply NNDR
//printf("q=%d dist1=%f dist2=%f\n", matches.at(i).at(0).queryIdx, matches.at(i).at(0).distance, matches.at(i).at(1).distance);
if(isBinaryDescriptors || //Binary, just take the nearest
matches.at(i).at(0).distance <= nndrRatio * matches.at(i).at(1).distance)
{
mpts_1.push_back(objectKeypoints.at(matches.at(i).at(0).queryIdx).pt);
indexes_1.push_back(matches.at(i).at(0).queryIdx);
mpts_2.push_back(sceneKeypoints.at(matches.at(i).at(0).trainIdx).pt);
indexes_2.push_back(matches.at(i).at(0).trainIdx);
}
}
}
// FIND HOMOGRAPHY
unsigned int minInliers = 8;
if(mpts_1.size() >= minInliers)
{
cv::Mat H = findHomography(mpts_1,
mpts_2,
cv::RANSAC,
1.0,
outlier_mask);
printf("Time finding homography = ms\n");
int inliers=0, outliers=0;
for(unsigned int k=0; k<mpts_1.size();++k)
{
if(outlier_mask.at(k))
{
++inliers;
}
else
{
++outliers;
}
}
QTransform hTransform(
H.at<double>(0,0), H.at<double>(1,0), H.at<double>(2,0),
H.at<double>(0,1), H.at<double>(1,1), H.at<double>(2,1),
H.at<double>(0,2), H.at<double>(1,2), H.at<double>(2,2));
// GUI : Change color and add homography rectangle
QColor color(Qt::green);
int alpha = 130;
color.setAlpha(alpha);
for(unsigned int k=0; k<mpts_1.size();++k)
{
/* if(outlier_mask.at(k))
{
obWidget.setKptColor(indexes_1.at(k), color);
sceneWidget.setKptColor(indexes_2.at(k), color);
}
else
{
obWidget.setKptColor(indexes_1.at(k), QColor(255,0,0,alpha));
sceneWidget.setKptColor(indexes_2.at(k), QColor(255,0,0,alpha));
}*/
}
QPen rectPen(color);
rectPen.setWidth(4);
/*QGraphicsRectItem * rectItem = new QGraphicsRectItem(obWidget.pixmap().rect());
rectItem->setPen(rectPen);
rectItem->setTransform(hTransform);
sceneWidget.addRect(rectItem);*/
printf("Inliers=%d Outliers=%d\n", inliers, outliers);
}
else
{
printf("Not enough matches (%d) for homography...\n", (int)mpts_1.size());
}
/* // Wait for gui
obWidget.setGraphicsViewMode(false);
obWidget.setWindowTitle("Object");
if(obWidget.pixmap().width() <= 800)
{
obWidget.setMinimumSize(obWidget.pixmap().width(), obWidget.pixmap().height());
}
else
{
obWidget.setMinimumSize(800, 600);
obWidget.setAutoScale(false);
}
sceneWidget.setGraphicsViewMode(false);
sceneWidget.setWindowTitle("Scene");
if(sceneWidget.pixmap().width() <= 800)
{
sceneWidget.setMinimumSize(sceneWidget.pixmap().width(), sceneWidget.pixmap().height());
}
else
{
sceneWidget.setMinimumSize(800, 600);
sceneWidget.setAutoScale(false);
}*/
/* sceneWidget.show();
obWidget.show();*/
printf("Closing...\n");
////////////////////////////
//Cleanup
////////////////////////////
delete detector;
delete extractor; |