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"
47 const string FEATURES2D_DIR = "features2d";
48 const string DETECTOR_DIR = FEATURES2D_DIR + "/feature_detectors";
49 const string DESCRIPTOR_DIR = FEATURES2D_DIR + "/descriptor_extractors";
50 const string IMAGE_FILENAME = "tsukuba.png";
52 /****************************************************************************************\
53 * Regression tests for feature detectors comparing keypoints. *
54 \****************************************************************************************/
56 class CV_FeatureDetectorTest : public cvtest::BaseTest
59 CV_FeatureDetectorTest( const string& _name, const Ptr<FeatureDetector>& _fdetector ) :
60 name(_name), fdetector(_fdetector) {}
63 bool isSimilarKeypoints( const KeyPoint& p1, const KeyPoint& p2 );
64 void compareKeypointSets( const vector<KeyPoint>& validKeypoints, const vector<KeyPoint>& calcKeypoints );
67 void regressionTest(); // TODO test of detect() with mask
69 virtual void run( int );
72 Ptr<FeatureDetector> fdetector;
75 void CV_FeatureDetectorTest::emptyDataTest()
79 vector<KeyPoint> keypoints;
82 fdetector->detect( image, keypoints );
86 ts->printf( cvtest::TS::LOG, "detect() on empty image must not generate exception (1).\n" );
87 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
90 if( !keypoints.empty() )
92 ts->printf( cvtest::TS::LOG, "detect() on empty image must return empty keypoints vector (1).\n" );
93 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
99 vector<vector<KeyPoint> > keypointCollection;
102 fdetector->detect( images, keypointCollection );
106 ts->printf( cvtest::TS::LOG, "detect() on empty image vector must not generate exception (2).\n" );
107 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
111 bool CV_FeatureDetectorTest::isSimilarKeypoints( const KeyPoint& p1, const KeyPoint& p2 )
113 const float maxPtDif = 1.f;
114 const float maxSizeDif = 1.f;
115 const float maxAngleDif = 2.f;
116 const float maxResponseDif = 0.1f;
118 float dist = (float)norm( p1.pt - p2.pt );
119 return (dist < maxPtDif &&
120 fabs(p1.size - p2.size) < maxSizeDif &&
121 abs(p1.angle - p2.angle) < maxAngleDif &&
122 abs(p1.response - p2.response) < maxResponseDif &&
123 p1.octave == p2.octave &&
124 p1.class_id == p2.class_id );
127 void CV_FeatureDetectorTest::compareKeypointSets( const vector<KeyPoint>& validKeypoints, const vector<KeyPoint>& calcKeypoints )
129 const float maxCountRatioDif = 0.01f;
131 // Compare counts of validation and calculated keypoints.
132 float countRatio = (float)validKeypoints.size() / (float)calcKeypoints.size();
133 if( countRatio < 1 - maxCountRatioDif || countRatio > 1.f + maxCountRatioDif )
135 ts->printf( cvtest::TS::LOG, "Bad keypoints count ratio (validCount = %d, calcCount = %d).\n",
136 validKeypoints.size(), calcKeypoints.size() );
137 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
141 int progress = 0, progressCount = (int)(validKeypoints.size() * calcKeypoints.size());
142 int badPointCount = 0, commonPointCount = max((int)validKeypoints.size(), (int)calcKeypoints.size());
143 for( size_t v = 0; v < validKeypoints.size(); v++ )
146 float minDist = std::numeric_limits<float>::max();
148 for( size_t c = 0; c < calcKeypoints.size(); c++ )
150 progress = update_progress( progress, (int)(v*calcKeypoints.size() + c), progressCount, 0 );
151 float curDist = (float)norm( calcKeypoints[c].pt - validKeypoints[v].pt );
152 if( curDist < minDist )
159 assert( minDist >= 0 );
160 if( !isSimilarKeypoints( validKeypoints[v], calcKeypoints[nearestIdx] ) )
163 ts->printf( cvtest::TS::LOG, "badPointCount = %d; validPointCount = %d; calcPointCount = %d\n",
164 badPointCount, validKeypoints.size(), calcKeypoints.size() );
165 if( badPointCount > 0.9 * commonPointCount )
167 ts->printf( cvtest::TS::LOG, " - Bad accuracy!\n" );
168 ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
171 ts->printf( cvtest::TS::LOG, " - OK\n" );
174 void CV_FeatureDetectorTest::regressionTest()
176 assert( !fdetector.empty() );
177 string imgFilename = string(ts->get_data_path()) + FEATURES2D_DIR + "/" + IMAGE_FILENAME;
178 string resFilename = string(ts->get_data_path()) + DETECTOR_DIR + "/" + string(name) + ".xml.gz";
180 // Read the test image.
181 Mat image = imread( imgFilename );
184 ts->printf( cvtest::TS::LOG, "Image %s can not be read.\n", imgFilename.c_str() );
185 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
189 FileStorage fs( resFilename, FileStorage::READ );
191 // Compute keypoints.
192 vector<KeyPoint> calcKeypoints;
193 fdetector->detect( image, calcKeypoints );
195 if( fs.isOpened() ) // Compare computed and valid keypoints.
197 // TODO compare saved feature detector params with current ones
199 // Read validation keypoints set.
200 vector<KeyPoint> validKeypoints;
201 read( fs["keypoints"], validKeypoints );
202 if( validKeypoints.empty() )
204 ts->printf( cvtest::TS::LOG, "Keypoints can not be read.\n" );
205 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
209 compareKeypointSets( validKeypoints, calcKeypoints );
211 else // Write detector parameters and computed keypoints as validation data.
213 fs.open( resFilename, FileStorage::WRITE );
216 ts->printf( cvtest::TS::LOG, "File %s can not be opened to write.\n", resFilename.c_str() );
217 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
222 fs << "detector_params" << "{";
223 fdetector->write( fs );
226 write( fs, "keypoints", calcKeypoints );
231 void CV_FeatureDetectorTest::run( int /*start_from*/ )
233 if( fdetector.empty() )
235 ts->printf( cvtest::TS::LOG, "Feature detector is empty.\n" );
236 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
243 ts->set_failed_test_info( cvtest::TS::OK );
246 /****************************************************************************************\
247 * Regression tests for descriptor extractors. *
248 \****************************************************************************************/
249 static void writeMatInBin( const Mat& mat, const string& filename )
251 FILE* f = fopen( filename.c_str(), "wb");
254 int type = mat.type();
255 fwrite( (void*)&mat.rows, sizeof(int), 1, f );
256 fwrite( (void*)&mat.cols, sizeof(int), 1, f );
257 fwrite( (void*)&type, sizeof(int), 1, f );
258 int dataSize = (int)(mat.step * mat.rows * mat.channels());
259 fwrite( (void*)&dataSize, sizeof(int), 1, f );
260 fwrite( (void*)mat.data, 1, dataSize, f );
265 static Mat readMatFromBin( const string& filename )
267 FILE* f = fopen( filename.c_str(), "rb" );
270 int rows, cols, type, dataSize;
271 fread( (void*)&rows, sizeof(int), 1, f );
272 fread( (void*)&cols, sizeof(int), 1, f );
273 fread( (void*)&type, sizeof(int), 1, f );
274 fread( (void*)&dataSize, sizeof(int), 1, f );
276 uchar* data = (uchar*)cvAlloc(dataSize);
277 fread( (void*)data, 1, dataSize, f );
280 return Mat( rows, cols, type, data );
285 template<class Distance>
286 class CV_DescriptorExtractorTest : public cvtest::BaseTest
289 typedef typename Distance::ValueType ValueType;
290 typedef typename Distance::ResultType DistanceType;
292 CV_DescriptorExtractorTest( const string _name, DistanceType _maxDist, const Ptr<DescriptorExtractor>& _dextractor, float _prevTime,
293 Distance d = Distance() ):
294 name(_name), maxDist(_maxDist), prevTime(_prevTime), dextractor(_dextractor), distance(d) {}
296 virtual void createDescriptorExtractor() {}
298 void compareDescriptors( const Mat& validDescriptors, const Mat& calcDescriptors )
300 if( validDescriptors.size != calcDescriptors.size || validDescriptors.type() != calcDescriptors.type() )
302 ts->printf(cvtest::TS::LOG, "Valid and computed descriptors matrices must have the same size and type.\n");
303 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
307 CV_Assert( DataType<ValueType>::type == validDescriptors.type() );
309 int dimension = validDescriptors.cols;
310 DistanceType curMaxDist = std::numeric_limits<DistanceType>::min();
311 for( int y = 0; y < validDescriptors.rows; y++ )
313 DistanceType dist = distance( validDescriptors.ptr<ValueType>(y), calcDescriptors.ptr<ValueType>(y), dimension );
314 if( dist > curMaxDist )
319 ss << "Max distance between valid and computed descriptors " << curMaxDist;
320 if( curMaxDist < maxDist )
324 ss << ">" << maxDist << " - bad accuracy!"<< endl;
325 ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
327 ts->printf(cvtest::TS::LOG, ss.str().c_str() );
332 assert( !dextractor.empty() );
336 vector<KeyPoint> keypoints;
341 dextractor->compute( image, keypoints, descriptors );
345 ts->printf( cvtest::TS::LOG, "compute() on empty image and empty keypoints must not generate exception (1).\n");
346 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
349 image.create( 50, 50, CV_8UC3 );
352 dextractor->compute( image, keypoints, descriptors );
356 ts->printf( cvtest::TS::LOG, "compute() on nonempty image and empty keypoints must not generate exception (1).\n");
357 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
362 vector<vector<KeyPoint> > keypointsCollection;
363 vector<Mat> descriptorsCollection;
366 dextractor->compute( images, keypointsCollection, descriptorsCollection );
370 ts->printf( cvtest::TS::LOG, "compute() on empty images and empty keypoints collection must not generate exception (2).\n");
371 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
375 void regressionTest()
377 assert( !dextractor.empty() );
379 // Read the test image.
380 string imgFilename = string(ts->get_data_path()) + FEATURES2D_DIR + "/" + IMAGE_FILENAME;
382 Mat img = imread( imgFilename );
385 ts->printf( cvtest::TS::LOG, "Image %s can not be read.\n", imgFilename.c_str() );
386 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
390 vector<KeyPoint> keypoints;
391 FileStorage fs( string(ts->get_data_path()) + FEATURES2D_DIR + "/keypoints.xml.gz", FileStorage::READ );
394 read( fs.getFirstTopLevelNode(), keypoints );
397 double t = (double)getTickCount();
398 dextractor->compute( img, keypoints, calcDescriptors );
399 t = getTickCount() - t;
400 ts->printf(cvtest::TS::LOG, "\nAverage time of computing one descriptor = %g ms (previous time = %g ms).\n", t/((double)cvGetTickFrequency()*1000.)/calcDescriptors.rows, prevTime );
402 if( calcDescriptors.rows != (int)keypoints.size() )
404 ts->printf( cvtest::TS::LOG, "Count of computed descriptors and keypoints count must be equal.\n" );
405 ts->printf( cvtest::TS::LOG, "Count of keypoints is %d.\n", (int)keypoints.size() );
406 ts->printf( cvtest::TS::LOG, "Count of computed descriptors is %d.\n", calcDescriptors.rows );
407 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
411 if( calcDescriptors.cols != dextractor->descriptorSize() || calcDescriptors.type() != dextractor->descriptorType() )
413 ts->printf( cvtest::TS::LOG, "Incorrect descriptor size or descriptor type.\n" );
414 ts->printf( cvtest::TS::LOG, "Expected size is %d.\n", dextractor->descriptorSize() );
415 ts->printf( cvtest::TS::LOG, "Calculated size is %d.\n", calcDescriptors.cols );
416 ts->printf( cvtest::TS::LOG, "Expected type is %d.\n", dextractor->descriptorType() );
417 ts->printf( cvtest::TS::LOG, "Calculated type is %d.\n", calcDescriptors.type() );
418 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
422 // TODO read and write descriptor extractor parameters and check them
423 Mat validDescriptors = readDescriptors();
424 if( !validDescriptors.empty() )
425 compareDescriptors( validDescriptors, calcDescriptors );
428 if( !writeDescriptors( calcDescriptors ) )
430 ts->printf( cvtest::TS::LOG, "Descriptors can not be written.\n" );
431 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
438 ts->printf( cvtest::TS::LOG, "Compute and write keypoints.\n" );
439 fs.open( string(ts->get_data_path()) + FEATURES2D_DIR + "/keypoints.xml.gz", FileStorage::WRITE );
442 SurfFeatureDetector fd;
443 fd.detect(img, keypoints);
444 write( fs, "keypoints", keypoints );
448 ts->printf(cvtest::TS::LOG, "File for writting keypoints can not be opened.\n");
449 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
457 createDescriptorExtractor();
458 if( dextractor.empty() )
460 ts->printf(cvtest::TS::LOG, "Descriptor extractor is empty.\n");
461 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
468 ts->set_failed_test_info( cvtest::TS::OK );
471 virtual Mat readDescriptors()
473 Mat res = readMatFromBin( string(ts->get_data_path()) + DESCRIPTOR_DIR + "/" + string(name) );
477 virtual bool writeDescriptors( Mat& descs )
479 writeMatInBin( descs, string(ts->get_data_path()) + DESCRIPTOR_DIR + "/" + string(name) );
484 const DistanceType maxDist;
485 const float prevTime;
486 Ptr<DescriptorExtractor> dextractor;
490 CV_DescriptorExtractorTest& operator=(const CV_DescriptorExtractorTest&) { return *this; }
493 /*template<typename T, typename Distance>
494 class CV_CalonderDescriptorExtractorTest : public CV_DescriptorExtractorTest<Distance>
497 CV_CalonderDescriptorExtractorTest( const char* testName, float _normDif, float _prevTime ) :
498 CV_DescriptorExtractorTest<Distance>( testName, _normDif, Ptr<DescriptorExtractor>(), _prevTime )
502 virtual void createDescriptorExtractor()
504 CV_DescriptorExtractorTest<Distance>::dextractor =
505 new CalonderDescriptorExtractor<T>( string(CV_DescriptorExtractorTest<Distance>::ts->get_data_path()) +
506 FEATURES2D_DIR + "/calonder_classifier.rtc");
510 /****************************************************************************************\
511 * Algorithmic tests for descriptor matchers *
512 \****************************************************************************************/
513 class CV_DescriptorMatcherTest : public cvtest::BaseTest
516 CV_DescriptorMatcherTest( const string& _name, const Ptr<DescriptorMatcher>& _dmatcher, float _badPart ) :
517 badPart(_badPart), name(_name), dmatcher(_dmatcher)
520 static const int dim = 500;
521 static const int queryDescCount = 300; // must be even number because we split train data in some cases in two
522 static const int countFactor = 4; // do not change it
525 virtual void run( int );
526 void generateData( Mat& query, Mat& train );
528 void emptyDataTest();
529 void matchTest( const Mat& query, const Mat& train );
530 void knnMatchTest( const Mat& query, const Mat& train );
531 void radiusMatchTest( const Mat& query, const Mat& train );
534 Ptr<DescriptorMatcher> dmatcher;
537 CV_DescriptorMatcherTest& operator=(const CV_DescriptorMatcherTest&) { return *this; }
540 void CV_DescriptorMatcherTest::emptyDataTest()
542 assert( !dmatcher.empty() );
543 Mat queryDescriptors, trainDescriptors, mask;
544 vector<Mat> trainDescriptorCollection, masks;
545 vector<DMatch> matches;
546 vector<vector<DMatch> > vmatches;
550 dmatcher->match( queryDescriptors, trainDescriptors, matches, mask );
554 ts->printf( cvtest::TS::LOG, "match() on empty descriptors must not generate exception (1).\n" );
555 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
560 dmatcher->knnMatch( queryDescriptors, trainDescriptors, vmatches, 2, mask );
564 ts->printf( cvtest::TS::LOG, "knnMatch() on empty descriptors must not generate exception (1).\n" );
565 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
570 dmatcher->radiusMatch( queryDescriptors, trainDescriptors, vmatches, 10.f, mask );
574 ts->printf( cvtest::TS::LOG, "radiusMatch() on empty descriptors must not generate exception (1).\n" );
575 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
580 dmatcher->add( trainDescriptorCollection );
584 ts->printf( cvtest::TS::LOG, "add() on empty descriptors must not generate exception.\n" );
585 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
590 dmatcher->match( queryDescriptors, matches, masks );
594 ts->printf( cvtest::TS::LOG, "match() on empty descriptors must not generate exception (2).\n" );
595 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
600 dmatcher->knnMatch( queryDescriptors, vmatches, 2, masks );
604 ts->printf( cvtest::TS::LOG, "knnMatch() on empty descriptors must not generate exception (2).\n" );
605 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
610 dmatcher->radiusMatch( queryDescriptors, vmatches, 10.f, masks );
614 ts->printf( cvtest::TS::LOG, "radiusMatch() on empty descriptors must not generate exception (2).\n" );
615 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
620 void CV_DescriptorMatcherTest::generateData( Mat& query, Mat& train )
624 // Generate query descriptors randomly.
625 // Descriptor vector elements are integer values.
626 Mat buf( queryDescCount, dim, CV_32SC1 );
627 rng.fill( buf, RNG::UNIFORM, Scalar::all(0), Scalar(3) );
628 buf.convertTo( query, CV_32FC1 );
630 // Generate train decriptors as follows:
631 // copy each query descriptor to train set countFactor times
632 // and perturb some one element of the copied descriptors in
633 // in ascending order. General boundaries of the perturbation
635 train.create( query.rows*countFactor, query.cols, CV_32FC1 );
636 float step = 1.f / countFactor;
637 for( int qIdx = 0; qIdx < query.rows; qIdx++ )
639 Mat queryDescriptor = query.row(qIdx);
640 for( int c = 0; c < countFactor; c++ )
642 int tIdx = qIdx * countFactor + c;
643 Mat trainDescriptor = train.row(tIdx);
644 queryDescriptor.copyTo( trainDescriptor );
646 float diff = rng.uniform( step*c, step*(c+1) );
647 trainDescriptor.at<float>(0, elem) += diff;
652 void CV_DescriptorMatcherTest::matchTest( const Mat& query, const Mat& train )
656 // test const version of match()
658 vector<DMatch> matches;
659 dmatcher->match( query, train, matches );
661 if( (int)matches.size() != queryDescCount )
663 ts->printf(cvtest::TS::LOG, "Incorrect matches count while test match() function (1).\n");
664 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
669 for( size_t i = 0; i < matches.size(); i++ )
671 DMatch match = matches[i];
672 if( (match.queryIdx != (int)i) || (match.trainIdx != (int)i*countFactor) || (match.imgIdx != 0) )
675 if( (float)badCount > (float)queryDescCount*badPart )
677 ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test match() function (1).\n",
678 (float)badCount/(float)queryDescCount );
679 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
684 // test version of match() with add()
686 vector<DMatch> matches;
687 // make add() twice to test such case
688 dmatcher->add( vector<Mat>(1,train.rowRange(0, train.rows/2)) );
689 dmatcher->add( vector<Mat>(1,train.rowRange(train.rows/2, train.rows)) );
690 // prepare masks (make first nearest match illegal)
691 vector<Mat> masks(2);
692 for(int mi = 0; mi < 2; mi++ )
694 masks[mi] = Mat(query.rows, train.rows/2, CV_8UC1, Scalar::all(1));
695 for( int di = 0; di < queryDescCount/2; di++ )
696 masks[mi].col(di*countFactor).setTo(Scalar::all(0));
699 dmatcher->match( query, matches, masks );
701 if( (int)matches.size() != queryDescCount )
703 ts->printf(cvtest::TS::LOG, "Incorrect matches count while test match() function (2).\n");
704 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
709 for( size_t i = 0; i < matches.size(); i++ )
711 DMatch match = matches[i];
712 int shift = dmatcher->isMaskSupported() ? 1 : 0;
714 if( i < queryDescCount/2 )
716 if( (match.queryIdx != (int)i) || (match.trainIdx != (int)i*countFactor + shift) || (match.imgIdx != 0) )
721 if( (match.queryIdx != (int)i) || (match.trainIdx != ((int)i-queryDescCount/2)*countFactor + shift) || (match.imgIdx != 1) )
726 if( (float)badCount > (float)queryDescCount*badPart )
728 ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test match() function (2).\n",
729 (float)badCount/(float)queryDescCount );
730 ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
736 void CV_DescriptorMatcherTest::knnMatchTest( const Mat& query, const Mat& train )
740 // test const version of knnMatch()
744 vector<vector<DMatch> > matches;
745 dmatcher->knnMatch( query, train, matches, knn );
747 if( (int)matches.size() != queryDescCount )
749 ts->printf(cvtest::TS::LOG, "Incorrect matches count while test knnMatch() function (1).\n");
750 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
755 for( size_t i = 0; i < matches.size(); i++ )
757 if( (int)matches[i].size() != knn )
761 int localBadCount = 0;
762 for( int k = 0; k < knn; k++ )
764 DMatch match = matches[i][k];
765 if( (match.queryIdx != (int)i) || (match.trainIdx != (int)i*countFactor+k) || (match.imgIdx != 0) )
768 badCount += localBadCount > 0 ? 1 : 0;
771 if( (float)badCount > (float)queryDescCount*badPart )
773 ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test knnMatch() function (1).\n",
774 (float)badCount/(float)queryDescCount );
775 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
780 // test version of knnMatch() with add()
783 vector<vector<DMatch> > matches;
784 // make add() twice to test such case
785 dmatcher->add( vector<Mat>(1,train.rowRange(0, train.rows/2)) );
786 dmatcher->add( vector<Mat>(1,train.rowRange(train.rows/2, train.rows)) );
787 // prepare masks (make first nearest match illegal)
788 vector<Mat> masks(2);
789 for(int mi = 0; mi < 2; mi++ )
791 masks[mi] = Mat(query.rows, train.rows/2, CV_8UC1, Scalar::all(1));
792 for( int di = 0; di < queryDescCount/2; di++ )
793 masks[mi].col(di*countFactor).setTo(Scalar::all(0));
796 dmatcher->knnMatch( query, matches, knn, masks );
798 if( (int)matches.size() != queryDescCount )
800 ts->printf(cvtest::TS::LOG, "Incorrect matches count while test knnMatch() function (2).\n");
801 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
806 int shift = dmatcher->isMaskSupported() ? 1 : 0;
807 for( size_t i = 0; i < matches.size(); i++ )
809 if( (int)matches[i].size() != knn )
813 int localBadCount = 0;
814 for( int k = 0; k < knn; k++ )
816 DMatch match = matches[i][k];
818 if( i < queryDescCount/2 )
820 if( (match.queryIdx != (int)i) || (match.trainIdx != (int)i*countFactor + k + shift) ||
821 (match.imgIdx != 0) )
826 if( (match.queryIdx != (int)i) || (match.trainIdx != ((int)i-queryDescCount/2)*countFactor + k + shift) ||
827 (match.imgIdx != 1) )
832 badCount += localBadCount > 0 ? 1 : 0;
835 if( (float)badCount > (float)queryDescCount*badPart )
837 ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test knnMatch() function (2).\n",
838 (float)badCount/(float)queryDescCount );
839 ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
845 void CV_DescriptorMatcherTest::radiusMatchTest( const Mat& query, const Mat& train )
848 // test const version of match()
850 const float radius = 1.f/countFactor;
851 vector<vector<DMatch> > matches;
852 dmatcher->radiusMatch( query, train, matches, radius );
854 if( (int)matches.size() != queryDescCount )
856 ts->printf(cvtest::TS::LOG, "Incorrect matches count while test radiusMatch() function (1).\n");
857 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
862 for( size_t i = 0; i < matches.size(); i++ )
864 if( (int)matches[i].size() != 1 )
868 DMatch match = matches[i][0];
869 if( (match.queryIdx != (int)i) || (match.trainIdx != (int)i*countFactor) || (match.imgIdx != 0) )
873 if( (float)badCount > (float)queryDescCount*badPart )
875 ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test radiusMatch() function (1).\n",
876 (float)badCount/(float)queryDescCount );
877 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
882 // test version of match() with add()
885 const float radius = 1.f/countFactor * n;
886 vector<vector<DMatch> > matches;
887 // make add() twice to test such case
888 dmatcher->add( vector<Mat>(1,train.rowRange(0, train.rows/2)) );
889 dmatcher->add( vector<Mat>(1,train.rowRange(train.rows/2, train.rows)) );
890 // prepare masks (make first nearest match illegal)
891 vector<Mat> masks(2);
892 for(int mi = 0; mi < 2; mi++ )
894 masks[mi] = Mat(query.rows, train.rows/2, CV_8UC1, Scalar::all(1));
895 for( int di = 0; di < queryDescCount/2; di++ )
896 masks[mi].col(di*countFactor).setTo(Scalar::all(0));
899 dmatcher->radiusMatch( query, matches, radius, masks );
901 //int curRes = cvtest::TS::OK;
902 if( (int)matches.size() != queryDescCount )
904 ts->printf(cvtest::TS::LOG, "Incorrect matches count while test radiusMatch() function (1).\n");
905 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
909 int shift = dmatcher->isMaskSupported() ? 1 : 0;
910 int needMatchCount = dmatcher->isMaskSupported() ? n-1 : n;
911 for( size_t i = 0; i < matches.size(); i++ )
913 if( (int)matches[i].size() != needMatchCount )
917 int localBadCount = 0;
918 for( int k = 0; k < needMatchCount; k++ )
920 DMatch match = matches[i][k];
922 if( i < queryDescCount/2 )
924 if( (match.queryIdx != (int)i) || (match.trainIdx != (int)i*countFactor + k + shift) ||
925 (match.imgIdx != 0) )
930 if( (match.queryIdx != (int)i) || (match.trainIdx != ((int)i-queryDescCount/2)*countFactor + k + shift) ||
931 (match.imgIdx != 1) )
936 badCount += localBadCount > 0 ? 1 : 0;
939 if( (float)badCount > (float)queryDescCount*badPart )
941 ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test radiusMatch() function (2).\n",
942 (float)badCount/(float)queryDescCount );
943 ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
948 void CV_DescriptorMatcherTest::run( int )
951 generateData( query, train );
953 matchTest( query, train );
955 knnMatchTest( query, train );
957 radiusMatchTest( query, train );
960 /****************************************************************************************\
961 * Tests registrations *
962 \****************************************************************************************/
969 TEST( Features2d_Detector_SIFT, regression )
971 CV_FeatureDetectorTest test( "detector-sift", FeatureDetector::create("SIFT") );
975 TEST( Features2d_Detector_SURF, regression )
977 CV_FeatureDetectorTest test( "detector-surf", FeatureDetector::create("SURF") );
984 TEST( Features2d_DescriptorExtractor_SIFT, regression )
986 CV_DescriptorExtractorTest<L2<float> > test( "descriptor-sift", 0.03f,
987 DescriptorExtractor::create("SIFT"), 8.06652f );
991 TEST( Features2d_DescriptorExtractor_SURF, regression )
993 CV_DescriptorExtractorTest<L2<float> > test( "descriptor-surf", 0.035f,
994 DescriptorExtractor::create("SURF"), 0.147372f );
998 /*TEST( Features2d_DescriptorExtractor_OpponentSIFT, regression )
1000 CV_DescriptorExtractorTest<L2<float> > test( "descriptor-opponent-sift", 0.18f,
1001 DescriptorExtractor::create("OpponentSIFT"), 8.06652f );
1005 TEST( Features2d_DescriptorExtractor_OpponentSURF, regression )
1007 CV_DescriptorExtractorTest<L2<float> > test( "descriptor-opponent-surf", 0.18f,
1008 DescriptorExtractor::create("OpponentSURF"), 0.147372f );
1013 TEST( Features2d_DescriptorExtractor_Calonder_uchar, regression )
1015 CV_CalonderDescriptorExtractorTest<uchar, L2<uchar> > test( "descriptor-calonder-uchar",
1016 std::numeric_limits<float>::epsilon() + 1,
1021 TEST( Features2d_DescriptorExtractor_Calonder_float, regression )
1023 CV_CalonderDescriptorExtractorTest<float, L2<float> > test( "descriptor-calonder-float",
1024 std::numeric_limits<float>::epsilon(),