1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
42 #include "test_precomp.hpp"
43 #include "opencv2/calib3d/calib3d.hpp"
48 const string FEATURES2D_DIR = "features2d";
49 const string DETECTOR_DIR = FEATURES2D_DIR + "/feature_detectors";
50 const string DESCRIPTOR_DIR = FEATURES2D_DIR + "/descriptor_extractors";
51 const string IMAGE_FILENAME = "tsukuba.png";
53 /****************************************************************************************\
54 * Regression tests for feature detectors comparing keypoints. *
55 \****************************************************************************************/
57 class CV_FeatureDetectorTest : public cvtest::BaseTest
60 CV_FeatureDetectorTest( const string& _name, const Ptr<FeatureDetector>& _fdetector ) :
61 name(_name), fdetector(_fdetector) {}
64 bool isSimilarKeypoints( const KeyPoint& p1, const KeyPoint& p2 );
65 void compareKeypointSets( const vector<KeyPoint>& validKeypoints, const vector<KeyPoint>& calcKeypoints );
68 void regressionTest(); // TODO test of detect() with mask
70 virtual void run( int );
73 Ptr<FeatureDetector> fdetector;
76 void CV_FeatureDetectorTest::emptyDataTest()
80 vector<KeyPoint> keypoints;
83 fdetector->detect( image, keypoints );
87 ts->printf( cvtest::TS::LOG, "detect() on empty image must not generate exception (1).\n" );
88 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
91 if( !keypoints.empty() )
93 ts->printf( cvtest::TS::LOG, "detect() on empty image must return empty keypoints vector (1).\n" );
94 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
100 vector<vector<KeyPoint> > keypointCollection;
103 fdetector->detect( images, keypointCollection );
107 ts->printf( cvtest::TS::LOG, "detect() on empty image vector must not generate exception (2).\n" );
108 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
112 bool CV_FeatureDetectorTest::isSimilarKeypoints( const KeyPoint& p1, const KeyPoint& p2 )
114 const float maxPtDif = 1.f;
115 const float maxSizeDif = 1.f;
116 const float maxAngleDif = 2.f;
117 const float maxResponseDif = 0.1f;
119 float dist = (float)norm( p1.pt - p2.pt );
120 return (dist < maxPtDif &&
121 fabs(p1.size - p2.size) < maxSizeDif &&
122 abs(p1.angle - p2.angle) < maxAngleDif &&
123 abs(p1.response - p2.response) < maxResponseDif &&
124 p1.octave == p2.octave &&
125 p1.class_id == p2.class_id );
128 void CV_FeatureDetectorTest::compareKeypointSets( const vector<KeyPoint>& validKeypoints, const vector<KeyPoint>& calcKeypoints )
130 const float maxCountRatioDif = 0.01f;
132 // Compare counts of validation and calculated keypoints.
133 float countRatio = (float)validKeypoints.size() / (float)calcKeypoints.size();
134 if( countRatio < 1 - maxCountRatioDif || countRatio > 1.f + maxCountRatioDif )
136 ts->printf( cvtest::TS::LOG, "Bad keypoints count ratio (validCount = %d, calcCount = %d).\n",
137 validKeypoints.size(), calcKeypoints.size() );
138 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
142 int progress = 0, progressCount = (int)(validKeypoints.size() * calcKeypoints.size());
143 int badPointCount = 0, commonPointCount = max((int)validKeypoints.size(), (int)calcKeypoints.size());
144 for( size_t v = 0; v < validKeypoints.size(); v++ )
147 float minDist = std::numeric_limits<float>::max();
149 for( size_t c = 0; c < calcKeypoints.size(); c++ )
151 progress = update_progress( progress, (int)(v*calcKeypoints.size() + c), progressCount, 0 );
152 float curDist = (float)norm( calcKeypoints[c].pt - validKeypoints[v].pt );
153 if( curDist < minDist )
160 assert( minDist >= 0 );
161 if( !isSimilarKeypoints( validKeypoints[v], calcKeypoints[nearestIdx] ) )
164 ts->printf( cvtest::TS::LOG, "badPointCount = %d; validPointCount = %d; calcPointCount = %d\n",
165 badPointCount, validKeypoints.size(), calcKeypoints.size() );
166 if( badPointCount > 0.9 * commonPointCount )
168 ts->printf( cvtest::TS::LOG, " - Bad accuracy!\n" );
169 ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
172 ts->printf( cvtest::TS::LOG, " - OK\n" );
175 void CV_FeatureDetectorTest::regressionTest()
177 assert( !fdetector.empty() );
178 string imgFilename = string(ts->get_data_path()) + FEATURES2D_DIR + "/" + IMAGE_FILENAME;
179 string resFilename = string(ts->get_data_path()) + DETECTOR_DIR + "/" + string(name) + ".xml.gz";
181 // Read the test image.
182 Mat image = imread( imgFilename );
185 ts->printf( cvtest::TS::LOG, "Image %s can not be read.\n", imgFilename.c_str() );
186 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
190 FileStorage fs( resFilename, FileStorage::READ );
192 // Compute keypoints.
193 vector<KeyPoint> calcKeypoints;
194 fdetector->detect( image, calcKeypoints );
196 if( fs.isOpened() ) // Compare computed and valid keypoints.
198 // TODO compare saved feature detector params with current ones
200 // Read validation keypoints set.
201 vector<KeyPoint> validKeypoints;
202 read( fs["keypoints"], validKeypoints );
203 if( validKeypoints.empty() )
205 ts->printf( cvtest::TS::LOG, "Keypoints can not be read.\n" );
206 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
210 compareKeypointSets( validKeypoints, calcKeypoints );
212 else // Write detector parameters and computed keypoints as validation data.
214 fs.open( resFilename, FileStorage::WRITE );
217 ts->printf( cvtest::TS::LOG, "File %s can not be opened to write.\n", resFilename.c_str() );
218 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
223 fs << "detector_params" << "{";
224 fdetector->write( fs );
227 write( fs, "keypoints", calcKeypoints );
232 void CV_FeatureDetectorTest::run( int /*start_from*/ )
234 if( fdetector.empty() )
236 ts->printf( cvtest::TS::LOG, "Feature detector is empty.\n" );
237 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
244 ts->set_failed_test_info( cvtest::TS::OK );
247 /****************************************************************************************\
248 * Regression tests for descriptor extractors. *
249 \****************************************************************************************/
250 static void writeMatInBin( const Mat& mat, const string& filename )
252 FILE* f = fopen( filename.c_str(), "wb");
255 int type = mat.type();
256 fwrite( (void*)&mat.rows, sizeof(int), 1, f );
257 fwrite( (void*)&mat.cols, sizeof(int), 1, f );
258 fwrite( (void*)&type, sizeof(int), 1, f );
259 int dataSize = (int)(mat.step * mat.rows * mat.channels());
260 fwrite( (void*)&dataSize, sizeof(int), 1, f );
261 fwrite( (void*)mat.data, 1, dataSize, f );
266 static Mat readMatFromBin( const string& filename )
268 FILE* f = fopen( filename.c_str(), "rb" );
271 int rows, cols, type, dataSize;
272 size_t elements_read1 = fread( (void*)&rows, sizeof(int), 1, f );
273 size_t elements_read2 = fread( (void*)&cols, sizeof(int), 1, f );
274 size_t elements_read3 = fread( (void*)&type, sizeof(int), 1, f );
275 size_t elements_read4 = fread( (void*)&dataSize, sizeof(int), 1, f );
276 CV_Assert(elements_read1 == 1 && elements_read2 == 1 && elements_read3 == 1 && elements_read4 == 1);
278 uchar* data = (uchar*)cvAlloc(dataSize);
279 size_t elements_read = fread( (void*)data, 1, dataSize, f );
280 CV_Assert(elements_read == (size_t)(dataSize));
283 return Mat( rows, cols, type, data );
288 template<class Distance>
289 class CV_DescriptorExtractorTest : public cvtest::BaseTest
292 typedef typename Distance::ValueType ValueType;
293 typedef typename Distance::ResultType DistanceType;
295 CV_DescriptorExtractorTest( const string _name, DistanceType _maxDist, const Ptr<DescriptorExtractor>& _dextractor,
296 Distance d = Distance() ):
297 name(_name), maxDist(_maxDist), dextractor(_dextractor), distance(d) {}
299 virtual void createDescriptorExtractor() {}
301 void compareDescriptors( const Mat& validDescriptors, const Mat& calcDescriptors )
303 if( validDescriptors.size != calcDescriptors.size || validDescriptors.type() != calcDescriptors.type() )
305 ts->printf(cvtest::TS::LOG, "Valid and computed descriptors matrices must have the same size and type.\n");
306 ts->printf(cvtest::TS::LOG, "Valid size is (%d x %d) actual size is (%d x %d).\n", validDescriptors.rows, validDescriptors.cols, calcDescriptors.rows, calcDescriptors.cols);
307 ts->printf(cvtest::TS::LOG, "Valid type is %d actual type is %d.\n", validDescriptors.type(), calcDescriptors.type());
308 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
312 CV_Assert( DataType<ValueType>::type == validDescriptors.type() );
314 int dimension = validDescriptors.cols;
315 DistanceType curMaxDist = std::numeric_limits<DistanceType>::min();
316 for( int y = 0; y < validDescriptors.rows; y++ )
318 DistanceType dist = distance( validDescriptors.ptr<ValueType>(y), calcDescriptors.ptr<ValueType>(y), dimension );
319 if( dist > curMaxDist )
324 ss << "Max distance between valid and computed descriptors " << curMaxDist;
325 if( curMaxDist < maxDist )
329 ss << ">" << maxDist << " - bad accuracy!"<< endl;
330 ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
332 ts->printf(cvtest::TS::LOG, ss.str().c_str() );
337 assert( !dextractor.empty() );
341 vector<KeyPoint> keypoints;
346 dextractor->compute( image, keypoints, descriptors );
350 ts->printf( cvtest::TS::LOG, "compute() on empty image and empty keypoints must not generate exception (1).\n");
351 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
354 image.create( 50, 50, CV_8UC3 );
357 dextractor->compute( image, keypoints, descriptors );
361 ts->printf( cvtest::TS::LOG, "compute() on nonempty image and empty keypoints must not generate exception (1).\n");
362 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
367 vector<vector<KeyPoint> > keypointsCollection;
368 vector<Mat> descriptorsCollection;
371 dextractor->compute( images, keypointsCollection, descriptorsCollection );
375 ts->printf( cvtest::TS::LOG, "compute() on empty images and empty keypoints collection must not generate exception (2).\n");
376 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
380 void regressionTest()
382 assert( !dextractor.empty() );
384 // Read the test image.
385 string imgFilename = string(ts->get_data_path()) + FEATURES2D_DIR + "/" + IMAGE_FILENAME;
387 Mat img = imread( imgFilename );
390 ts->printf( cvtest::TS::LOG, "Image %s can not be read.\n", imgFilename.c_str() );
391 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
395 vector<KeyPoint> keypoints;
396 FileStorage fs( string(ts->get_data_path()) + FEATURES2D_DIR + "/keypoints.xml.gz", FileStorage::READ );
399 read( fs.getFirstTopLevelNode(), keypoints );
402 double t = (double)getTickCount();
403 dextractor->compute( img, keypoints, calcDescriptors );
404 t = getTickCount() - t;
405 ts->printf(cvtest::TS::LOG, "\nAverage time of computing one descriptor = %g ms.\n", t/((double)cvGetTickFrequency()*1000.)/calcDescriptors.rows );
407 if( calcDescriptors.rows != (int)keypoints.size() )
409 ts->printf( cvtest::TS::LOG, "Count of computed descriptors and keypoints count must be equal.\n" );
410 ts->printf( cvtest::TS::LOG, "Count of keypoints is %d.\n", (int)keypoints.size() );
411 ts->printf( cvtest::TS::LOG, "Count of computed descriptors is %d.\n", calcDescriptors.rows );
412 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
416 if( calcDescriptors.cols != dextractor->descriptorSize() || calcDescriptors.type() != dextractor->descriptorType() )
418 ts->printf( cvtest::TS::LOG, "Incorrect descriptor size or descriptor type.\n" );
419 ts->printf( cvtest::TS::LOG, "Expected size is %d.\n", dextractor->descriptorSize() );
420 ts->printf( cvtest::TS::LOG, "Calculated size is %d.\n", calcDescriptors.cols );
421 ts->printf( cvtest::TS::LOG, "Expected type is %d.\n", dextractor->descriptorType() );
422 ts->printf( cvtest::TS::LOG, "Calculated type is %d.\n", calcDescriptors.type() );
423 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
427 // TODO read and write descriptor extractor parameters and check them
428 Mat validDescriptors = readDescriptors();
429 if( !validDescriptors.empty() )
430 compareDescriptors( validDescriptors, calcDescriptors );
433 if( !writeDescriptors( calcDescriptors ) )
435 ts->printf( cvtest::TS::LOG, "Descriptors can not be written.\n" );
436 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
443 ts->printf( cvtest::TS::LOG, "Compute and write keypoints.\n" );
444 fs.open( string(ts->get_data_path()) + FEATURES2D_DIR + "/keypoints.xml.gz", FileStorage::WRITE );
447 SurfFeatureDetector fd;
448 fd.detect(img, keypoints);
449 write( fs, "keypoints", keypoints );
453 ts->printf(cvtest::TS::LOG, "File for writting keypoints can not be opened.\n");
454 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
462 createDescriptorExtractor();
463 if( dextractor.empty() )
465 ts->printf(cvtest::TS::LOG, "Descriptor extractor is empty.\n");
466 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
473 ts->set_failed_test_info( cvtest::TS::OK );
476 virtual Mat readDescriptors()
478 Mat res = readMatFromBin( string(ts->get_data_path()) + DESCRIPTOR_DIR + "/" + string(name) );
482 virtual bool writeDescriptors( Mat& descs )
484 writeMatInBin( descs, string(ts->get_data_path()) + DESCRIPTOR_DIR + "/" + string(name) );
489 const DistanceType maxDist;
490 Ptr<DescriptorExtractor> dextractor;
494 CV_DescriptorExtractorTest& operator=(const CV_DescriptorExtractorTest&) { return *this; }
497 /*template<typename T, typename Distance>
498 class CV_CalonderDescriptorExtractorTest : public CV_DescriptorExtractorTest<Distance>
501 CV_CalonderDescriptorExtractorTest( const char* testName, float _normDif, float _prevTime ) :
502 CV_DescriptorExtractorTest<Distance>( testName, _normDif, Ptr<DescriptorExtractor>(), _prevTime )
506 virtual void createDescriptorExtractor()
508 CV_DescriptorExtractorTest<Distance>::dextractor =
509 new CalonderDescriptorExtractor<T>( string(CV_DescriptorExtractorTest<Distance>::ts->get_data_path()) +
510 FEATURES2D_DIR + "/calonder_classifier.rtc");
514 /****************************************************************************************\
515 * Algorithmic tests for descriptor matchers *
516 \****************************************************************************************/
517 class CV_DescriptorMatcherTest : public cvtest::BaseTest
520 CV_DescriptorMatcherTest( const string& _name, const Ptr<DescriptorMatcher>& _dmatcher, float _badPart ) :
521 badPart(_badPart), name(_name), dmatcher(_dmatcher)
524 static const int dim = 500;
525 static const int queryDescCount = 300; // must be even number because we split train data in some cases in two
526 static const int countFactor = 4; // do not change it
529 virtual void run( int );
530 void generateData( Mat& query, Mat& train );
532 void emptyDataTest();
533 void matchTest( const Mat& query, const Mat& train );
534 void knnMatchTest( const Mat& query, const Mat& train );
535 void radiusMatchTest( const Mat& query, const Mat& train );
538 Ptr<DescriptorMatcher> dmatcher;
541 CV_DescriptorMatcherTest& operator=(const CV_DescriptorMatcherTest&) { return *this; }
544 void CV_DescriptorMatcherTest::emptyDataTest()
546 assert( !dmatcher.empty() );
547 Mat queryDescriptors, trainDescriptors, mask;
548 vector<Mat> trainDescriptorCollection, masks;
549 vector<DMatch> matches;
550 vector<vector<DMatch> > vmatches;
554 dmatcher->match( queryDescriptors, trainDescriptors, matches, mask );
558 ts->printf( cvtest::TS::LOG, "match() on empty descriptors must not generate exception (1).\n" );
559 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
564 dmatcher->knnMatch( queryDescriptors, trainDescriptors, vmatches, 2, mask );
568 ts->printf( cvtest::TS::LOG, "knnMatch() on empty descriptors must not generate exception (1).\n" );
569 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
574 dmatcher->radiusMatch( queryDescriptors, trainDescriptors, vmatches, 10.f, mask );
578 ts->printf( cvtest::TS::LOG, "radiusMatch() on empty descriptors must not generate exception (1).\n" );
579 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
584 dmatcher->add( trainDescriptorCollection );
588 ts->printf( cvtest::TS::LOG, "add() on empty descriptors must not generate exception.\n" );
589 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
594 dmatcher->match( queryDescriptors, matches, masks );
598 ts->printf( cvtest::TS::LOG, "match() on empty descriptors must not generate exception (2).\n" );
599 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
604 dmatcher->knnMatch( queryDescriptors, vmatches, 2, masks );
608 ts->printf( cvtest::TS::LOG, "knnMatch() on empty descriptors must not generate exception (2).\n" );
609 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
614 dmatcher->radiusMatch( queryDescriptors, vmatches, 10.f, masks );
618 ts->printf( cvtest::TS::LOG, "radiusMatch() on empty descriptors must not generate exception (2).\n" );
619 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
624 void CV_DescriptorMatcherTest::generateData( Mat& query, Mat& train )
628 // Generate query descriptors randomly.
629 // Descriptor vector elements are integer values.
630 Mat buf( queryDescCount, dim, CV_32SC1 );
631 rng.fill( buf, RNG::UNIFORM, Scalar::all(0), Scalar(3) );
632 buf.convertTo( query, CV_32FC1 );
634 // Generate train decriptors as follows:
635 // copy each query descriptor to train set countFactor times
636 // and perturb some one element of the copied descriptors in
637 // in ascending order. General boundaries of the perturbation
639 train.create( query.rows*countFactor, query.cols, CV_32FC1 );
640 float step = 1.f / countFactor;
641 for( int qIdx = 0; qIdx < query.rows; qIdx++ )
643 Mat queryDescriptor = query.row(qIdx);
644 for( int c = 0; c < countFactor; c++ )
646 int tIdx = qIdx * countFactor + c;
647 Mat trainDescriptor = train.row(tIdx);
648 queryDescriptor.copyTo( trainDescriptor );
650 float diff = rng.uniform( step*c, step*(c+1) );
651 trainDescriptor.at<float>(0, elem) += diff;
656 void CV_DescriptorMatcherTest::matchTest( const Mat& query, const Mat& train )
660 // test const version of match()
662 vector<DMatch> matches;
663 dmatcher->match( query, train, matches );
665 if( (int)matches.size() != queryDescCount )
667 ts->printf(cvtest::TS::LOG, "Incorrect matches count while test match() function (1).\n");
668 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
673 for( size_t i = 0; i < matches.size(); i++ )
675 DMatch match = matches[i];
676 if( (match.queryIdx != (int)i) || (match.trainIdx != (int)i*countFactor) || (match.imgIdx != 0) )
679 if( (float)badCount > (float)queryDescCount*badPart )
681 ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test match() function (1).\n",
682 (float)badCount/(float)queryDescCount );
683 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
688 // test version of match() with add()
690 vector<DMatch> matches;
691 // make add() twice to test such case
692 dmatcher->add( vector<Mat>(1,train.rowRange(0, train.rows/2)) );
693 dmatcher->add( vector<Mat>(1,train.rowRange(train.rows/2, train.rows)) );
694 // prepare masks (make first nearest match illegal)
695 vector<Mat> masks(2);
696 for(int mi = 0; mi < 2; mi++ )
698 masks[mi] = Mat(query.rows, train.rows/2, CV_8UC1, Scalar::all(1));
699 for( int di = 0; di < queryDescCount/2; di++ )
700 masks[mi].col(di*countFactor).setTo(Scalar::all(0));
703 dmatcher->match( query, matches, masks );
705 if( (int)matches.size() != queryDescCount )
707 ts->printf(cvtest::TS::LOG, "Incorrect matches count while test match() function (2).\n");
708 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
713 for( size_t i = 0; i < matches.size(); i++ )
715 DMatch match = matches[i];
716 int shift = dmatcher->isMaskSupported() ? 1 : 0;
718 if( i < queryDescCount/2 )
720 if( (match.queryIdx != (int)i) || (match.trainIdx != (int)i*countFactor + shift) || (match.imgIdx != 0) )
725 if( (match.queryIdx != (int)i) || (match.trainIdx != ((int)i-queryDescCount/2)*countFactor + shift) || (match.imgIdx != 1) )
730 if( (float)badCount > (float)queryDescCount*badPart )
732 ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test match() function (2).\n",
733 (float)badCount/(float)queryDescCount );
734 ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
740 void CV_DescriptorMatcherTest::knnMatchTest( const Mat& query, const Mat& train )
744 // test const version of knnMatch()
748 vector<vector<DMatch> > matches;
749 dmatcher->knnMatch( query, train, matches, knn );
751 if( (int)matches.size() != queryDescCount )
753 ts->printf(cvtest::TS::LOG, "Incorrect matches count while test knnMatch() function (1).\n");
754 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
759 for( size_t i = 0; i < matches.size(); i++ )
761 if( (int)matches[i].size() != knn )
765 int localBadCount = 0;
766 for( int k = 0; k < knn; k++ )
768 DMatch match = matches[i][k];
769 if( (match.queryIdx != (int)i) || (match.trainIdx != (int)i*countFactor+k) || (match.imgIdx != 0) )
772 badCount += localBadCount > 0 ? 1 : 0;
775 if( (float)badCount > (float)queryDescCount*badPart )
777 ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test knnMatch() function (1).\n",
778 (float)badCount/(float)queryDescCount );
779 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
784 // test version of knnMatch() with add()
787 vector<vector<DMatch> > matches;
788 // make add() twice to test such case
789 dmatcher->add( vector<Mat>(1,train.rowRange(0, train.rows/2)) );
790 dmatcher->add( vector<Mat>(1,train.rowRange(train.rows/2, train.rows)) );
791 // prepare masks (make first nearest match illegal)
792 vector<Mat> masks(2);
793 for(int mi = 0; mi < 2; mi++ )
795 masks[mi] = Mat(query.rows, train.rows/2, CV_8UC1, Scalar::all(1));
796 for( int di = 0; di < queryDescCount/2; di++ )
797 masks[mi].col(di*countFactor).setTo(Scalar::all(0));
800 dmatcher->knnMatch( query, matches, knn, masks );
802 if( (int)matches.size() != queryDescCount )
804 ts->printf(cvtest::TS::LOG, "Incorrect matches count while test knnMatch() function (2).\n");
805 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
810 int shift = dmatcher->isMaskSupported() ? 1 : 0;
811 for( size_t i = 0; i < matches.size(); i++ )
813 if( (int)matches[i].size() != knn )
817 int localBadCount = 0;
818 for( int k = 0; k < knn; k++ )
820 DMatch match = matches[i][k];
822 if( i < queryDescCount/2 )
824 if( (match.queryIdx != (int)i) || (match.trainIdx != (int)i*countFactor + k + shift) ||
825 (match.imgIdx != 0) )
830 if( (match.queryIdx != (int)i) || (match.trainIdx != ((int)i-queryDescCount/2)*countFactor + k + shift) ||
831 (match.imgIdx != 1) )
836 badCount += localBadCount > 0 ? 1 : 0;
839 if( (float)badCount > (float)queryDescCount*badPart )
841 ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test knnMatch() function (2).\n",
842 (float)badCount/(float)queryDescCount );
843 ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
849 void CV_DescriptorMatcherTest::radiusMatchTest( const Mat& query, const Mat& train )
852 // test const version of match()
854 const float radius = 1.f/countFactor;
855 vector<vector<DMatch> > matches;
856 dmatcher->radiusMatch( query, train, matches, radius );
858 if( (int)matches.size() != queryDescCount )
860 ts->printf(cvtest::TS::LOG, "Incorrect matches count while test radiusMatch() function (1).\n");
861 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
866 for( size_t i = 0; i < matches.size(); i++ )
868 if( (int)matches[i].size() != 1 )
872 DMatch match = matches[i][0];
873 if( (match.queryIdx != (int)i) || (match.trainIdx != (int)i*countFactor) || (match.imgIdx != 0) )
877 if( (float)badCount > (float)queryDescCount*badPart )
879 ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test radiusMatch() function (1).\n",
880 (float)badCount/(float)queryDescCount );
881 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
886 // test version of match() with add()
889 const float radius = 1.f/countFactor * n;
890 vector<vector<DMatch> > matches;
891 // make add() twice to test such case
892 dmatcher->add( vector<Mat>(1,train.rowRange(0, train.rows/2)) );
893 dmatcher->add( vector<Mat>(1,train.rowRange(train.rows/2, train.rows)) );
894 // prepare masks (make first nearest match illegal)
895 vector<Mat> masks(2);
896 for(int mi = 0; mi < 2; mi++ )
898 masks[mi] = Mat(query.rows, train.rows/2, CV_8UC1, Scalar::all(1));
899 for( int di = 0; di < queryDescCount/2; di++ )
900 masks[mi].col(di*countFactor).setTo(Scalar::all(0));
903 dmatcher->radiusMatch( query, matches, radius, masks );
905 //int curRes = cvtest::TS::OK;
906 if( (int)matches.size() != queryDescCount )
908 ts->printf(cvtest::TS::LOG, "Incorrect matches count while test radiusMatch() function (1).\n");
909 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
913 int shift = dmatcher->isMaskSupported() ? 1 : 0;
914 int needMatchCount = dmatcher->isMaskSupported() ? n-1 : n;
915 for( size_t i = 0; i < matches.size(); i++ )
917 if( (int)matches[i].size() != needMatchCount )
921 int localBadCount = 0;
922 for( int k = 0; k < needMatchCount; k++ )
924 DMatch match = matches[i][k];
926 if( i < queryDescCount/2 )
928 if( (match.queryIdx != (int)i) || (match.trainIdx != (int)i*countFactor + k + shift) ||
929 (match.imgIdx != 0) )
934 if( (match.queryIdx != (int)i) || (match.trainIdx != ((int)i-queryDescCount/2)*countFactor + k + shift) ||
935 (match.imgIdx != 1) )
940 badCount += localBadCount > 0 ? 1 : 0;
943 if( (float)badCount > (float)queryDescCount*badPart )
945 ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test radiusMatch() function (2).\n",
946 (float)badCount/(float)queryDescCount );
947 ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
952 void CV_DescriptorMatcherTest::run( int )
955 generateData( query, train );
957 matchTest( query, train );
959 knnMatchTest( query, train );
961 radiusMatchTest( query, train );
964 /****************************************************************************************\
965 * Tests registrations *
966 \****************************************************************************************/
973 TEST( Features2d_Detector_SIFT, regression )
975 CV_FeatureDetectorTest test( "detector-sift", FeatureDetector::create("SIFT") );
979 TEST( Features2d_Detector_SURF, regression )
981 CV_FeatureDetectorTest test( "detector-surf", FeatureDetector::create("SURF") );
988 TEST( Features2d_DescriptorExtractor_SIFT, regression )
990 CV_DescriptorExtractorTest<L2<float> > test( "descriptor-sift", 0.03f,
991 DescriptorExtractor::create("SIFT") );
995 TEST( Features2d_DescriptorExtractor_SURF, regression )
997 CV_DescriptorExtractorTest<L2<float> > test( "descriptor-surf", 0.05f,
998 DescriptorExtractor::create("SURF") );
1002 TEST( Features2d_DescriptorExtractor_OpponentSIFT, regression )
1004 CV_DescriptorExtractorTest<L2<float> > test( "descriptor-opponent-sift", 0.18f,
1005 DescriptorExtractor::create("OpponentSIFT") );
1009 TEST( Features2d_DescriptorExtractor_OpponentSURF, regression )
1011 CV_DescriptorExtractorTest<L2<float> > test( "descriptor-opponent-surf", 0.3f,
1012 DescriptorExtractor::create("OpponentSURF") );
1017 TEST( Features2d_DescriptorExtractor_Calonder_uchar, regression )
1019 CV_CalonderDescriptorExtractorTest<uchar, L2<uchar> > test( "descriptor-calonder-uchar",
1020 std::numeric_limits<float>::epsilon() + 1,
1025 TEST( Features2d_DescriptorExtractor_Calonder_float, regression )
1027 CV_CalonderDescriptorExtractorTest<float, L2<float> > test( "descriptor-calonder-float",
1028 std::numeric_limits<float>::epsilon(),
1034 TEST(Features2d_BruteForceDescriptorMatcher_knnMatch, regression)
1039 Ptr<DescriptorExtractor> ext = DescriptorExtractor::create("SURF");
1040 ASSERT_TRUE(ext != NULL);
1042 Ptr<FeatureDetector> det = FeatureDetector::create("SURF");
1043 //"%YAML:1.0\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n"
1044 ASSERT_TRUE(det != NULL);
1046 Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce");
1047 ASSERT_TRUE(matcher != NULL);
1049 Mat imgT(sz, sz, CV_8U, Scalar(255));
1050 line(imgT, Point(20, sz/2), Point(sz-21, sz/2), Scalar(100), 2);
1051 line(imgT, Point(sz/2, 20), Point(sz/2, sz-21), Scalar(100), 2);
1052 vector<KeyPoint> kpT;
1053 kpT.push_back( KeyPoint(50, 50, 16, 0, 20000, 1, -1) );
1054 kpT.push_back( KeyPoint(42, 42, 16, 160, 10000, 1, -1) );
1056 ext->compute(imgT, kpT, descT);
1058 Mat imgQ(sz, sz, CV_8U, Scalar(255));
1059 line(imgQ, Point(30, sz/2), Point(sz-31, sz/2), Scalar(100), 3);
1060 line(imgQ, Point(sz/2, 30), Point(sz/2, sz-31), Scalar(100), 3);
1061 vector<KeyPoint> kpQ;
1062 det->detect(imgQ, kpQ);
1064 ext->compute(imgQ, kpQ, descQ);
1066 vector<vector<DMatch> > matches;
1068 matcher->knnMatch(descQ, descT, matches, k);
1070 //cout << "\nBest " << k << " matches to " << descT.rows << " train desc-s." << endl;
1071 ASSERT_EQ(descQ.rows, static_cast<int>(matches.size()));
1072 for(size_t i = 0; i<matches.size(); i++)
1074 //cout << "\nmatches[" << i << "].size()==" << matches[i].size() << endl;
1075 ASSERT_GE(min(k, descT.rows), static_cast<int>(matches[i].size()));
1076 for(size_t j = 0; j<matches[i].size(); j++)
1078 //cout << "\t" << matches[i][j].queryIdx << " -> " << matches[i][j].trainIdx << endl;
1079 ASSERT_EQ(matches[i][j].queryIdx, static_cast<int>(i));
1084 /*TEST(Features2d_DescriptorExtractorParamTest, regression)
1086 Ptr<DescriptorExtractor> s = DescriptorExtractor::create("SURF");
1087 ASSERT_STREQ(s->paramHelp("extended").c_str(), "");
1091 class CV_DetectPlanarTest : public cvtest::BaseTest
1094 CV_DetectPlanarTest(const string& _fname, int _min_ninliers) : fname(_fname), min_ninliers(_min_ninliers) {}
1099 Ptr<Feature2D> f = Algorithm::create<Feature2D>("Feature2D." + fname);
1102 string path = string(ts->get_data_path()) + "detectors_descriptors_evaluation/planar/";
1103 string imgname1 = path + "box.png";
1104 string imgname2 = path + "box_in_scene.png";
1105 Mat img1 = imread(imgname1, 0);
1106 Mat img2 = imread(imgname2, 0);
1107 if( img1.empty() || img2.empty() )
1109 ts->printf( cvtest::TS::LOG, "missing %s and/or %s\n", imgname1.c_str(), imgname2.c_str());
1110 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
1113 vector<KeyPoint> kpt1, kpt2;
1115 f->operator()(img1, Mat(), kpt1, d1);
1116 f->operator()(img1, Mat(), kpt2, d2);
1117 for( size_t i = 0; i < kpt1.size(); i++ )
1118 CV_Assert(kpt1[i].response > 0 );
1119 for( size_t i = 0; i < kpt2.size(); i++ )
1120 CV_Assert(kpt2[i].response > 0 );
1122 vector<DMatch> matches;
1123 BFMatcher(NORM_L2, true).match(d1, d2, matches);
1125 vector<Point2f> pt1, pt2;
1126 for( size_t i = 0; i < matches.size(); i++ ) {
1127 pt1.push_back(kpt1[matches[i].queryIdx].pt);
1128 pt2.push_back(kpt2[matches[i].trainIdx].pt);
1131 Mat inliers, H = findHomography(pt1, pt2, RANSAC, 10, inliers);
1132 int ninliers = countNonZero(inliers);
1134 if( ninliers < min_ninliers )
1136 ts->printf( cvtest::TS::LOG, "too little inliers (%d) vs expected %d\n", ninliers, min_ninliers);
1137 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
1146 TEST(Features2d_SIFTHomographyTest, regression) { CV_DetectPlanarTest test("SIFT", 80); test.safe_run(); }
1147 TEST(Features2d_SURFHomographyTest, regression) { CV_DetectPlanarTest test("SURF", 80); test.safe_run(); }
1149 class FeatureDetectorUsingMaskTest : public cvtest::BaseTest
1152 FeatureDetectorUsingMaskTest(const Ptr<FeatureDetector>& featureDetector) :
1153 featureDetector_(featureDetector)
1155 CV_Assert(!featureDetector_.empty());
1162 const int nStepX = 2;
1163 const int nStepY = 2;
1165 const string imageFilename = string(ts->get_data_path()) + "/features2d/tsukuba.png";
1167 Mat image = imread(imageFilename);
1170 ts->printf(cvtest::TS::LOG, "Image %s can not be read.\n", imageFilename.c_str());
1171 ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
1175 Mat mask(image.size(), CV_8U);
1177 const int stepX = image.size().width / nStepX;
1178 const int stepY = image.size().height / nStepY;
1180 vector<KeyPoint> keyPoints;
1181 vector<Point2f> points;
1182 for(int i=0; i<nStepX; ++i)
1183 for(int j=0; j<nStepY; ++j)
1187 Rect whiteArea(i * stepX, j * stepY, stepX, stepY);
1188 mask(whiteArea).setTo(255);
1190 featureDetector_->detect(image, keyPoints, mask);
1191 KeyPoint::convert(keyPoints, points);
1193 for(size_t k=0; k<points.size(); ++k)
1195 if ( !whiteArea.contains(points[k]) )
1197 ts->printf(cvtest::TS::LOG, "The feature point is outside of the mask.");
1198 ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
1204 ts->set_failed_test_info( cvtest::TS::OK );
1207 Ptr<FeatureDetector> featureDetector_;
1210 TEST(Features2d_SIFT_using_mask, regression)
1212 FeatureDetectorUsingMaskTest test(Algorithm::create<FeatureDetector>("Feature2D.SIFT"));
1216 TEST(DISABLED_Features2d_SURF_using_mask, regression)
1218 FeatureDetectorUsingMaskTest test(Algorithm::create<FeatureDetector>("Feature2D.SURF"));