Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/features2d/test/test_matchers_algorithmic.cpp
16354 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// Intel License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000, Intel Corporation, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
18
//
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
21
//
22
// * Redistribution's in binary form must reproduce the above copyright notice,
23
// this list of conditions and the following disclaimer in the documentation
24
// and/or other materials provided with the distribution.
25
//
26
// * The name of Intel Corporation may not be used to endorse or promote products
27
// derived from this software without specific prior written permission.
28
//
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall the Intel Corporation or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
39
//
40
//M*/
41
42
#include "test_precomp.hpp"
43
44
namespace opencv_test { namespace {
45
46
const string FEATURES2D_DIR = "features2d";
47
const string IMAGE_FILENAME = "tsukuba.png";
48
49
/****************************************************************************************\
50
* Algorithmic tests for descriptor matchers *
51
\****************************************************************************************/
52
class CV_DescriptorMatcherTest : public cvtest::BaseTest
53
{
54
public:
55
CV_DescriptorMatcherTest( const string& _name, const Ptr<DescriptorMatcher>& _dmatcher, float _badPart ) :
56
badPart(_badPart), name(_name), dmatcher(_dmatcher)
57
{}
58
protected:
59
static const int dim = 500;
60
static const int queryDescCount = 300; // must be even number because we split train data in some cases in two
61
static const int countFactor = 4; // do not change it
62
const float badPart;
63
64
virtual void run( int );
65
void generateData( Mat& query, Mat& train );
66
67
#if 0
68
void emptyDataTest(); // FIXIT not used
69
#endif
70
void matchTest( const Mat& query, const Mat& train );
71
void knnMatchTest( const Mat& query, const Mat& train );
72
void radiusMatchTest( const Mat& query, const Mat& train );
73
74
string name;
75
Ptr<DescriptorMatcher> dmatcher;
76
77
private:
78
CV_DescriptorMatcherTest& operator=(const CV_DescriptorMatcherTest&) { return *this; }
79
};
80
81
#if 0
82
void CV_DescriptorMatcherTest::emptyDataTest()
83
{
84
assert( !dmatcher.empty() );
85
Mat queryDescriptors, trainDescriptors, mask;
86
vector<Mat> trainDescriptorCollection, masks;
87
vector<DMatch> matches;
88
vector<vector<DMatch> > vmatches;
89
90
try
91
{
92
dmatcher->match( queryDescriptors, trainDescriptors, matches, mask );
93
}
94
catch(...)
95
{
96
ts->printf( cvtest::TS::LOG, "match() on empty descriptors must not generate exception (1).\n" );
97
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
98
}
99
100
try
101
{
102
dmatcher->knnMatch( queryDescriptors, trainDescriptors, vmatches, 2, mask );
103
}
104
catch(...)
105
{
106
ts->printf( cvtest::TS::LOG, "knnMatch() on empty descriptors must not generate exception (1).\n" );
107
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
108
}
109
110
try
111
{
112
dmatcher->radiusMatch( queryDescriptors, trainDescriptors, vmatches, 10.f, mask );
113
}
114
catch(...)
115
{
116
ts->printf( cvtest::TS::LOG, "radiusMatch() on empty descriptors must not generate exception (1).\n" );
117
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
118
}
119
120
try
121
{
122
dmatcher->add( trainDescriptorCollection );
123
}
124
catch(...)
125
{
126
ts->printf( cvtest::TS::LOG, "add() on empty descriptors must not generate exception.\n" );
127
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
128
}
129
130
try
131
{
132
dmatcher->match( queryDescriptors, matches, masks );
133
}
134
catch(...)
135
{
136
ts->printf( cvtest::TS::LOG, "match() on empty descriptors must not generate exception (2).\n" );
137
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
138
}
139
140
try
141
{
142
dmatcher->knnMatch( queryDescriptors, vmatches, 2, masks );
143
}
144
catch(...)
145
{
146
ts->printf( cvtest::TS::LOG, "knnMatch() on empty descriptors must not generate exception (2).\n" );
147
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
148
}
149
150
try
151
{
152
dmatcher->radiusMatch( queryDescriptors, vmatches, 10.f, masks );
153
}
154
catch(...)
155
{
156
ts->printf( cvtest::TS::LOG, "radiusMatch() on empty descriptors must not generate exception (2).\n" );
157
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
158
}
159
160
}
161
#endif
162
163
void CV_DescriptorMatcherTest::generateData( Mat& query, Mat& train )
164
{
165
RNG& rng = theRNG();
166
167
// Generate query descriptors randomly.
168
// Descriptor vector elements are integer values.
169
Mat buf( queryDescCount, dim, CV_32SC1 );
170
rng.fill( buf, RNG::UNIFORM, Scalar::all(0), Scalar(3) );
171
buf.convertTo( query, CV_32FC1 );
172
173
// Generate train descriptors as follows:
174
// copy each query descriptor to train set countFactor times
175
// and perturb some one element of the copied descriptors in
176
// in ascending order. General boundaries of the perturbation
177
// are (0.f, 1.f).
178
train.create( query.rows*countFactor, query.cols, CV_32FC1 );
179
float step = 1.f / countFactor;
180
for( int qIdx = 0; qIdx < query.rows; qIdx++ )
181
{
182
Mat queryDescriptor = query.row(qIdx);
183
for( int c = 0; c < countFactor; c++ )
184
{
185
int tIdx = qIdx * countFactor + c;
186
Mat trainDescriptor = train.row(tIdx);
187
queryDescriptor.copyTo( trainDescriptor );
188
int elem = rng(dim);
189
float diff = rng.uniform( step*c, step*(c+1) );
190
trainDescriptor.at<float>(0, elem) += diff;
191
}
192
}
193
}
194
195
void CV_DescriptorMatcherTest::matchTest( const Mat& query, const Mat& train )
196
{
197
dmatcher->clear();
198
199
// test const version of match()
200
{
201
vector<DMatch> matches;
202
dmatcher->match( query, train, matches );
203
204
if( (int)matches.size() != queryDescCount )
205
{
206
ts->printf(cvtest::TS::LOG, "Incorrect matches count while test match() function (1).\n");
207
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
208
}
209
else
210
{
211
int badCount = 0;
212
for( size_t i = 0; i < matches.size(); i++ )
213
{
214
DMatch& match = matches[i];
215
if( (match.queryIdx != (int)i) || (match.trainIdx != (int)i*countFactor) || (match.imgIdx != 0) )
216
badCount++;
217
}
218
if( (float)badCount > (float)queryDescCount*badPart )
219
{
220
ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test match() function (1).\n",
221
(float)badCount/(float)queryDescCount );
222
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
223
}
224
}
225
}
226
227
// test const version of match() for the same query and test descriptors
228
{
229
vector<DMatch> matches;
230
dmatcher->match( query, query, matches );
231
232
if( (int)matches.size() != query.rows )
233
{
234
ts->printf(cvtest::TS::LOG, "Incorrect matches count while test match() function for the same query and test descriptors (1).\n");
235
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
236
}
237
else
238
{
239
for( size_t i = 0; i < matches.size(); i++ )
240
{
241
DMatch& match = matches[i];
242
//std::cout << match.distance << std::endl;
243
244
if( match.queryIdx != (int)i || match.trainIdx != (int)i || std::abs(match.distance) > FLT_EPSILON )
245
{
246
ts->printf( cvtest::TS::LOG, "Bad match (i=%d, queryIdx=%d, trainIdx=%d, distance=%f) while test match() function for the same query and test descriptors (1).\n",
247
i, match.queryIdx, match.trainIdx, match.distance );
248
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
249
}
250
}
251
}
252
}
253
254
// test version of match() with add()
255
{
256
vector<DMatch> matches;
257
// make add() twice to test such case
258
dmatcher->add( vector<Mat>(1,train.rowRange(0, train.rows/2)) );
259
dmatcher->add( vector<Mat>(1,train.rowRange(train.rows/2, train.rows)) );
260
// prepare masks (make first nearest match illegal)
261
vector<Mat> masks(2);
262
for(int mi = 0; mi < 2; mi++ )
263
{
264
masks[mi] = Mat(query.rows, train.rows/2, CV_8UC1, Scalar::all(1));
265
for( int di = 0; di < queryDescCount/2; di++ )
266
masks[mi].col(di*countFactor).setTo(Scalar::all(0));
267
}
268
269
dmatcher->match( query, matches, masks );
270
271
if( (int)matches.size() != queryDescCount )
272
{
273
ts->printf(cvtest::TS::LOG, "Incorrect matches count while test match() function (2).\n");
274
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
275
}
276
else
277
{
278
int badCount = 0;
279
for( size_t i = 0; i < matches.size(); i++ )
280
{
281
DMatch& match = matches[i];
282
int shift = dmatcher->isMaskSupported() ? 1 : 0;
283
{
284
if( i < queryDescCount/2 )
285
{
286
if( (match.queryIdx != (int)i) || (match.trainIdx != (int)i*countFactor + shift) || (match.imgIdx != 0) )
287
badCount++;
288
}
289
else
290
{
291
if( (match.queryIdx != (int)i) || (match.trainIdx != ((int)i-queryDescCount/2)*countFactor + shift) || (match.imgIdx != 1) )
292
badCount++;
293
}
294
}
295
}
296
if( (float)badCount > (float)queryDescCount*badPart )
297
{
298
ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test match() function (2).\n",
299
(float)badCount/(float)queryDescCount );
300
ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
301
}
302
}
303
}
304
}
305
306
void CV_DescriptorMatcherTest::knnMatchTest( const Mat& query, const Mat& train )
307
{
308
dmatcher->clear();
309
310
// test const version of knnMatch()
311
{
312
const int knn = 3;
313
314
vector<vector<DMatch> > matches;
315
dmatcher->knnMatch( query, train, matches, knn );
316
317
if( (int)matches.size() != queryDescCount )
318
{
319
ts->printf(cvtest::TS::LOG, "Incorrect matches count while test knnMatch() function (1).\n");
320
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
321
}
322
else
323
{
324
int badCount = 0;
325
for( size_t i = 0; i < matches.size(); i++ )
326
{
327
if( (int)matches[i].size() != knn )
328
badCount++;
329
else
330
{
331
int localBadCount = 0;
332
for( int k = 0; k < knn; k++ )
333
{
334
DMatch& match = matches[i][k];
335
if( (match.queryIdx != (int)i) || (match.trainIdx != (int)i*countFactor+k) || (match.imgIdx != 0) )
336
localBadCount++;
337
}
338
badCount += localBadCount > 0 ? 1 : 0;
339
}
340
}
341
if( (float)badCount > (float)queryDescCount*badPart )
342
{
343
ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test knnMatch() function (1).\n",
344
(float)badCount/(float)queryDescCount );
345
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
346
}
347
}
348
}
349
350
// test version of knnMatch() with add()
351
{
352
const int knn = 2;
353
vector<vector<DMatch> > matches;
354
// make add() twice to test such case
355
dmatcher->add( vector<Mat>(1,train.rowRange(0, train.rows/2)) );
356
dmatcher->add( vector<Mat>(1,train.rowRange(train.rows/2, train.rows)) );
357
// prepare masks (make first nearest match illegal)
358
vector<Mat> masks(2);
359
for(int mi = 0; mi < 2; mi++ )
360
{
361
masks[mi] = Mat(query.rows, train.rows/2, CV_8UC1, Scalar::all(1));
362
for( int di = 0; di < queryDescCount/2; di++ )
363
masks[mi].col(di*countFactor).setTo(Scalar::all(0));
364
}
365
366
dmatcher->knnMatch( query, matches, knn, masks );
367
368
if( (int)matches.size() != queryDescCount )
369
{
370
ts->printf(cvtest::TS::LOG, "Incorrect matches count while test knnMatch() function (2).\n");
371
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
372
}
373
else
374
{
375
int badCount = 0;
376
int shift = dmatcher->isMaskSupported() ? 1 : 0;
377
for( size_t i = 0; i < matches.size(); i++ )
378
{
379
if( (int)matches[i].size() != knn )
380
badCount++;
381
else
382
{
383
int localBadCount = 0;
384
for( int k = 0; k < knn; k++ )
385
{
386
DMatch& match = matches[i][k];
387
{
388
if( i < queryDescCount/2 )
389
{
390
if( (match.queryIdx != (int)i) || (match.trainIdx != (int)i*countFactor + k + shift) ||
391
(match.imgIdx != 0) )
392
localBadCount++;
393
}
394
else
395
{
396
if( (match.queryIdx != (int)i) || (match.trainIdx != ((int)i-queryDescCount/2)*countFactor + k + shift) ||
397
(match.imgIdx != 1) )
398
localBadCount++;
399
}
400
}
401
}
402
badCount += localBadCount > 0 ? 1 : 0;
403
}
404
}
405
if( (float)badCount > (float)queryDescCount*badPart )
406
{
407
ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test knnMatch() function (2).\n",
408
(float)badCount/(float)queryDescCount );
409
ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
410
}
411
}
412
}
413
}
414
415
void CV_DescriptorMatcherTest::radiusMatchTest( const Mat& query, const Mat& train )
416
{
417
dmatcher->clear();
418
// test const version of match()
419
{
420
const float radius = 1.f/countFactor;
421
vector<vector<DMatch> > matches;
422
dmatcher->radiusMatch( query, train, matches, radius );
423
424
if( (int)matches.size() != queryDescCount )
425
{
426
ts->printf(cvtest::TS::LOG, "Incorrect matches count while test radiusMatch() function (1).\n");
427
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
428
}
429
else
430
{
431
int badCount = 0;
432
for( size_t i = 0; i < matches.size(); i++ )
433
{
434
if( (int)matches[i].size() != 1 )
435
badCount++;
436
else
437
{
438
DMatch& match = matches[i][0];
439
if( (match.queryIdx != (int)i) || (match.trainIdx != (int)i*countFactor) || (match.imgIdx != 0) )
440
badCount++;
441
}
442
}
443
if( (float)badCount > (float)queryDescCount*badPart )
444
{
445
ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test radiusMatch() function (1).\n",
446
(float)badCount/(float)queryDescCount );
447
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
448
}
449
}
450
}
451
452
// test version of match() with add()
453
{
454
int n = 3;
455
const float radius = 1.f/countFactor * n;
456
vector<vector<DMatch> > matches;
457
// make add() twice to test such case
458
dmatcher->add( vector<Mat>(1,train.rowRange(0, train.rows/2)) );
459
dmatcher->add( vector<Mat>(1,train.rowRange(train.rows/2, train.rows)) );
460
// prepare masks (make first nearest match illegal)
461
vector<Mat> masks(2);
462
for(int mi = 0; mi < 2; mi++ )
463
{
464
masks[mi] = Mat(query.rows, train.rows/2, CV_8UC1, Scalar::all(1));
465
for( int di = 0; di < queryDescCount/2; di++ )
466
masks[mi].col(di*countFactor).setTo(Scalar::all(0));
467
}
468
469
dmatcher->radiusMatch( query, matches, radius, masks );
470
471
//int curRes = cvtest::TS::OK;
472
if( (int)matches.size() != queryDescCount )
473
{
474
ts->printf(cvtest::TS::LOG, "Incorrect matches count while test radiusMatch() function (1).\n");
475
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
476
}
477
478
int badCount = 0;
479
int shift = dmatcher->isMaskSupported() ? 1 : 0;
480
int needMatchCount = dmatcher->isMaskSupported() ? n-1 : n;
481
for( size_t i = 0; i < matches.size(); i++ )
482
{
483
if( (int)matches[i].size() != needMatchCount )
484
badCount++;
485
else
486
{
487
int localBadCount = 0;
488
for( int k = 0; k < needMatchCount; k++ )
489
{
490
DMatch& match = matches[i][k];
491
{
492
if( i < queryDescCount/2 )
493
{
494
if( (match.queryIdx != (int)i) || (match.trainIdx != (int)i*countFactor + k + shift) ||
495
(match.imgIdx != 0) )
496
localBadCount++;
497
}
498
else
499
{
500
if( (match.queryIdx != (int)i) || (match.trainIdx != ((int)i-queryDescCount/2)*countFactor + k + shift) ||
501
(match.imgIdx != 1) )
502
localBadCount++;
503
}
504
}
505
}
506
badCount += localBadCount > 0 ? 1 : 0;
507
}
508
}
509
if( (float)badCount > (float)queryDescCount*badPart )
510
{
511
//curRes = cvtest::TS::FAIL_INVALID_OUTPUT;
512
ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test radiusMatch() function (2).\n",
513
(float)badCount/(float)queryDescCount );
514
ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
515
}
516
}
517
}
518
519
void CV_DescriptorMatcherTest::run( int )
520
{
521
Mat query, train;
522
generateData( query, train );
523
524
matchTest( query, train );
525
526
knnMatchTest( query, train );
527
528
radiusMatchTest( query, train );
529
}
530
531
/****************************************************************************************\
532
* Tests registrations *
533
\****************************************************************************************/
534
535
TEST( Features2d_DescriptorMatcher_BruteForce, regression )
536
{
537
CV_DescriptorMatcherTest test( "descriptor-matcher-brute-force",
538
DescriptorMatcher::create("BruteForce"), 0.01f );
539
test.safe_run();
540
}
541
542
#ifdef HAVE_OPENCV_FLANN
543
TEST( Features2d_DescriptorMatcher_FlannBased, regression )
544
{
545
CV_DescriptorMatcherTest test( "descriptor-matcher-flann-based",
546
DescriptorMatcher::create("FlannBased"), 0.04f );
547
test.safe_run();
548
}
549
#endif
550
551
TEST( Features2d_DMatch, read_write )
552
{
553
FileStorage fs(".xml", FileStorage::WRITE + FileStorage::MEMORY);
554
vector<DMatch> matches;
555
matches.push_back(DMatch(1,2,3,4.5f));
556
fs << "Match" << matches;
557
String str = fs.releaseAndGetString();
558
ASSERT_NE( strstr(str.c_str(), "4.5"), (char*)0 );
559
}
560
561
}} // namespace
562
563