Path: blob/master/modules/features2d/test/test_detectors_regression.impl.hpp
16354 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html34namespace opencv_test { namespace {56/****************************************************************************************\7* Regression tests for feature detectors comparing keypoints. *8\****************************************************************************************/910class CV_FeatureDetectorTest : public cvtest::BaseTest11{12public:13CV_FeatureDetectorTest( const string& _name, const Ptr<FeatureDetector>& _fdetector ) :14name(_name), fdetector(_fdetector) {}1516protected:17bool isSimilarKeypoints( const KeyPoint& p1, const KeyPoint& p2 );18void compareKeypointSets( const vector<KeyPoint>& validKeypoints, const vector<KeyPoint>& calcKeypoints );1920void emptyDataTest();21void regressionTest(); // TODO test of detect() with mask2223virtual void run( int );2425string name;26Ptr<FeatureDetector> fdetector;27};2829void CV_FeatureDetectorTest::emptyDataTest()30{31// One image.32Mat image;33vector<KeyPoint> keypoints;34try35{36fdetector->detect( image, keypoints );37}38catch(...)39{40ts->printf( cvtest::TS::LOG, "detect() on empty image must not generate exception (1).\n" );41ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );42}4344if( !keypoints.empty() )45{46ts->printf( cvtest::TS::LOG, "detect() on empty image must return empty keypoints vector (1).\n" );47ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );48return;49}5051// Several images.52vector<Mat> images;53vector<vector<KeyPoint> > keypointCollection;54try55{56fdetector->detect( images, keypointCollection );57}58catch(...)59{60ts->printf( cvtest::TS::LOG, "detect() on empty image vector must not generate exception (2).\n" );61ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );62}63}6465bool CV_FeatureDetectorTest::isSimilarKeypoints( const KeyPoint& p1, const KeyPoint& p2 )66{67const float maxPtDif = 1.f;68const float maxSizeDif = 1.f;69const float maxAngleDif = 2.f;70const float maxResponseDif = 0.1f;7172float dist = (float)cv::norm( p1.pt - p2.pt );73return (dist < maxPtDif &&74fabs(p1.size - p2.size) < maxSizeDif &&75abs(p1.angle - p2.angle) < maxAngleDif &&76abs(p1.response - p2.response) < maxResponseDif &&77p1.octave == p2.octave &&78p1.class_id == p2.class_id );79}8081void CV_FeatureDetectorTest::compareKeypointSets( const vector<KeyPoint>& validKeypoints, const vector<KeyPoint>& calcKeypoints )82{83const float maxCountRatioDif = 0.01f;8485// Compare counts of validation and calculated keypoints.86float countRatio = (float)validKeypoints.size() / (float)calcKeypoints.size();87if( countRatio < 1 - maxCountRatioDif || countRatio > 1.f + maxCountRatioDif )88{89ts->printf( cvtest::TS::LOG, "Bad keypoints count ratio (validCount = %d, calcCount = %d).\n",90validKeypoints.size(), calcKeypoints.size() );91ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );92return;93}9495int progress = 0, progressCount = (int)(validKeypoints.size() * calcKeypoints.size());96int badPointCount = 0, commonPointCount = max((int)validKeypoints.size(), (int)calcKeypoints.size());97for( size_t v = 0; v < validKeypoints.size(); v++ )98{99int nearestIdx = -1;100float minDist = std::numeric_limits<float>::max();101102for( size_t c = 0; c < calcKeypoints.size(); c++ )103{104progress = update_progress( progress, (int)(v*calcKeypoints.size() + c), progressCount, 0 );105float curDist = (float)cv::norm( calcKeypoints[c].pt - validKeypoints[v].pt );106if( curDist < minDist )107{108minDist = curDist;109nearestIdx = (int)c;110}111}112113assert( minDist >= 0 );114if( !isSimilarKeypoints( validKeypoints[v], calcKeypoints[nearestIdx] ) )115badPointCount++;116}117ts->printf( cvtest::TS::LOG, "badPointCount = %d; validPointCount = %d; calcPointCount = %d\n",118badPointCount, validKeypoints.size(), calcKeypoints.size() );119if( badPointCount > 0.9 * commonPointCount )120{121ts->printf( cvtest::TS::LOG, " - Bad accuracy!\n" );122ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );123return;124}125ts->printf( cvtest::TS::LOG, " - OK\n" );126}127128void CV_FeatureDetectorTest::regressionTest()129{130assert( !fdetector.empty() );131string imgFilename = string(ts->get_data_path()) + FEATURES2D_DIR + "/" + IMAGE_FILENAME;132string resFilename = string(ts->get_data_path()) + DETECTOR_DIR + "/" + string(name) + ".xml.gz";133134// Read the test image.135Mat image = imread( imgFilename );136if( image.empty() )137{138ts->printf( cvtest::TS::LOG, "Image %s can not be read.\n", imgFilename.c_str() );139ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );140return;141}142143FileStorage fs( resFilename, FileStorage::READ );144145// Compute keypoints.146vector<KeyPoint> calcKeypoints;147fdetector->detect( image, calcKeypoints );148149if( fs.isOpened() ) // Compare computed and valid keypoints.150{151// TODO compare saved feature detector params with current ones152153// Read validation keypoints set.154vector<KeyPoint> validKeypoints;155read( fs["keypoints"], validKeypoints );156if( validKeypoints.empty() )157{158ts->printf( cvtest::TS::LOG, "Keypoints can not be read.\n" );159ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );160return;161}162163compareKeypointSets( validKeypoints, calcKeypoints );164}165else // Write detector parameters and computed keypoints as validation data.166{167fs.open( resFilename, FileStorage::WRITE );168if( !fs.isOpened() )169{170ts->printf( cvtest::TS::LOG, "File %s can not be opened to write.\n", resFilename.c_str() );171ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );172return;173}174else175{176fs << "detector_params" << "{";177fdetector->write( fs );178fs << "}";179180write( fs, "keypoints", calcKeypoints );181}182}183}184185void CV_FeatureDetectorTest::run( int /*start_from*/ )186{187if( !fdetector )188{189ts->printf( cvtest::TS::LOG, "Feature detector is empty.\n" );190ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );191return;192}193194emptyDataTest();195regressionTest();196197ts->set_failed_test_info( cvtest::TS::OK );198}199200}} // namespace201202203