added to FeatureDetector test the check of detect() on empty data

pull/13383/head
Maria Dimashova 15 years ago
parent 27690e3b6e
commit 43716f31b9
  1. 234
      tests/cv/src/afeatures2d.cpp

@ -60,111 +60,174 @@ public:
CvTest( testName, "cv::FeatureDetector::detect"), fdetector(_fdetector) {} CvTest( testName, "cv::FeatureDetector::detect"), fdetector(_fdetector) {}
protected: protected:
virtual void run( int /*start_from*/ ) bool isSimilarKeypoints( const KeyPoint& p1, const KeyPoint& p2 );
void compareKeypointSets( const vector<KeyPoint>& validKeypoints, const vector<KeyPoint>& calcKeypoints );
void emptyDataTest();
void regressionTest(); // TODO test of detect() with mask
virtual void run( int );
Ptr<FeatureDetector> fdetector;
};
void CV_FeatureDetectorTest::emptyDataTest()
{
Mat image;
vector<KeyPoint> keypoints;
try
{ {
const float maxPtDif = 1.f; fdetector->detect( image, keypoints );
const float maxSizeDif = 1.f; }
const float maxAngleDif = 2.f; catch(...)
const float maxResponseDif = 0.1f; {
ts->printf( CvTS::LOG, "emptyDataTest: Detect() on empty image must not generate exeption\n" );
ts->set_failed_test_info( CvTS::FAIL_INVALID_OUTPUT );
return;
}
string imgFilename = string(ts->get_data_path()) + FEATURES2D_DIR + "/" + IMAGE_FILENAME; if( !keypoints.empty() )
string resFilename = string(ts->get_data_path()) + DETECTOR_DIR + "/" + string(name) + ".xml.gz"; {
ts->printf( CvTS::LOG, "emptyDataTest: Detect() on empty image must return empty keypoints vector\n" );
ts->set_failed_test_info( CvTS::FAIL_INVALID_OUTPUT );
return;
}
}
if( fdetector.empty() ) bool CV_FeatureDetectorTest::isSimilarKeypoints( const KeyPoint& p1, const KeyPoint& p2 )
{ {
ts->printf( CvTS::LOG, "Feature detector is empty" ); const float maxPtDif = 1.f;
ts->set_failed_test_info( CvTS::FAIL_INVALID_TEST_DATA ); const float maxSizeDif = 1.f;
return; const float maxAngleDif = 2.f;
} const float maxResponseDif = 0.1f;
float dist = (float)norm( p1.pt - p2.pt );
return (dist < maxPtDif &&
fabs(p1.size - p2.size) < maxSizeDif &&
abs(p1.angle - p2.angle) < maxAngleDif &&
abs(p1.response - p2.response) < maxResponseDif &&
p1.octave == p2.octave &&
p1.class_id == p2.class_id );
}
Mat image = imread( imgFilename, 0 ); void CV_FeatureDetectorTest::compareKeypointSets( const vector<KeyPoint>& validKeypoints, const vector<KeyPoint>& calcKeypoints )
if( image.empty() ) {
{ const float maxCountRatioDif = 0.01f;
ts->printf( CvTS::LOG, "image %s can not be read \n", imgFilename.c_str() );
ts->set_failed_test_info( CvTS::FAIL_INVALID_TEST_DATA );
return;
}
FileStorage fs( resFilename, FileStorage::READ ); // Compare counts of validation and calculated keypoints.
float countRatio = (float)validKeypoints.size() / (float)calcKeypoints.size();
if( countRatio < 1 - maxCountRatioDif || countRatio > 1.f + maxCountRatioDif )
{
ts->printf( CvTS::LOG, "Bad keypoints count ratio (validCount = %d, calcCount = %d)!\n",
validKeypoints.size(), calcKeypoints.size() );
ts->set_failed_test_info( CvTS::FAIL_BAD_ACCURACY );
return;
}
vector<KeyPoint> calcKeypoints; int progress = 0, progressCount = validKeypoints.size() * calcKeypoints.size();
fdetector->detect( image, calcKeypoints ); int badPointCount = 0, commonPointCount = max(validKeypoints.size(), calcKeypoints.size());
for( size_t v = 0; v < validKeypoints.size(); v++ )
{
int nearestIdx = -1;
float minDist = std::numeric_limits<float>::max();
if( fs.isOpened() ) // compare computed and valid keypoints for( size_t c = 0; c < calcKeypoints.size(); c++ )
{ {
// TODO compare saved feature detector params with current ones progress = update_progress( progress, v*calcKeypoints.size() + c, progressCount, 0 );
vector<KeyPoint> validKeypoints; float curDist = (float)norm( calcKeypoints[c].pt - validKeypoints[v].pt );
read( fs["keypoints"], validKeypoints ); if( curDist < minDist )
if( validKeypoints.empty() )
{ {
ts->printf( CvTS::LOG, "Keypoints can nod be read\n" ); minDist = curDist;
ts->set_failed_test_info( CvTS::FAIL_INVALID_TEST_DATA ); nearestIdx = c;
return;
} }
}
int progress = 0, progressCount = validKeypoints.size() * calcKeypoints.size(); assert( minDist >= 0 );
int badPointCount = 0, commonPointCount = max(validKeypoints.size(), calcKeypoints.size()); if( !isSimilarKeypoints( validKeypoints[v], calcKeypoints[nearestIdx] ) )
for( size_t v = 0; v < validKeypoints.size(); v++ ) badPointCount++;
{ }
int nearestIdx = -1; ts->printf( CvTS::LOG, "regressionTest: badPointCount = %d; validPointCount = %d; calcPointCount = %d\n",
float minDist = std::numeric_limits<float>::max(); badPointCount, validKeypoints.size(), calcKeypoints.size() );
if( badPointCount > 0.9 * commonPointCount )
{
ts->printf( CvTS::LOG, " - Bad accuracy!\n" );
ts->set_failed_test_info( CvTS::FAIL_BAD_ACCURACY );
return;
}
ts->printf( CvTS::LOG, " - OK\n" );
}
for( size_t c = 0; c < calcKeypoints.size(); c++ ) void CV_FeatureDetectorTest::regressionTest()
{ {
progress = update_progress( progress, v*calcKeypoints.size() + c, progressCount, 0 ); assert( !fdetector.empty() );
float curDist = (float)norm( calcKeypoints[c].pt - validKeypoints[v].pt ); string imgFilename = string(ts->get_data_path()) + FEATURES2D_DIR + "/" + IMAGE_FILENAME;
if( curDist < minDist ) string resFilename = string(ts->get_data_path()) + DETECTOR_DIR + "/" + string(name) + ".xml.gz";
{
minDist = curDist;
nearestIdx = c;
}
}
if( minDist > maxPtDif || // Read the test image.
fabs(calcKeypoints[nearestIdx].size - validKeypoints[v].size) > maxSizeDif || Mat image = imread( imgFilename, 0 );
abs(calcKeypoints[nearestIdx].angle - validKeypoints[v].angle) > maxAngleDif || if( image.empty() )
abs(calcKeypoints[nearestIdx].response - validKeypoints[v].response) > maxResponseDif || {
calcKeypoints[nearestIdx].octave != validKeypoints[v].octave ts->printf( CvTS::LOG, "image %s can not be read \n", imgFilename.c_str() );
ts->set_failed_test_info( CvTS::FAIL_INVALID_TEST_DATA );
return;
}
// TODO !!!!!!! FileStorage fs( resFilename, FileStorage::READ );
/*||
calcKeypoints[nearestIdx].class_id != validKeypoints[v].class_id*/ ) // Compute keypoints.
{ vector<KeyPoint> calcKeypoints;
badPointCount++; fdetector->detect( image, calcKeypoints );
}
} if( fs.isOpened() ) // Compare computed and valid keypoints.
ts->printf( CvTS::LOG, "badPointCount = %d; validPointCount = %d; calcPointCount = %d\n", {
badPointCount, validKeypoints.size(), calcKeypoints.size() ); // TODO compare saved feature detector params with current ones
if( badPointCount > 0.9 * commonPointCount )
{ // Read validation keypoints set.
ts->printf( CvTS::LOG, "Bad accuracy!\n" ); vector<KeyPoint> validKeypoints;
ts->set_failed_test_info( CvTS::FAIL_BAD_ACCURACY ); read( fs["keypoints"], validKeypoints );
return; if( validKeypoints.empty() )
} {
ts->printf( CvTS::LOG, "Keypoints can nod be read\n" );
ts->set_failed_test_info( CvTS::FAIL_INVALID_TEST_DATA );
return;
} }
else // write
compareKeypointSets( validKeypoints, calcKeypoints );
}
else // Write detector parameters and computed keypoints as validation data.
{
fs.open( resFilename, FileStorage::WRITE );
if( !fs.isOpened() )
{ {
fs.open( resFilename, FileStorage::WRITE ); ts->printf( CvTS::LOG, "file %s can not be opened to write\n", resFilename.c_str() );
if( !fs.isOpened() ) ts->set_failed_test_info( CvTS::FAIL_INVALID_TEST_DATA );
{ return;
ts->printf( CvTS::LOG, "file %s can not be opened to write\n", resFilename.c_str() ); }
ts->set_failed_test_info( CvTS::FAIL_INVALID_TEST_DATA ); else
return; {
} fs << "detector_params" << "{";
else fdetector->write( fs );
{ fs << "}";
fs << "detector_params" << "{";
fdetector->write( fs );
fs << "}";
write( fs, "keypoints", calcKeypoints ); write( fs, "keypoints", calcKeypoints );
}
} }
ts->set_failed_test_info( CvTS::OK );
} }
}
Ptr<FeatureDetector> fdetector; void CV_FeatureDetectorTest::run( int /*start_from*/ )
}; {
if( fdetector.empty() )
{
ts->printf( CvTS::LOG, "Feature detector is empty" );
ts->set_failed_test_info( CvTS::FAIL_INVALID_TEST_DATA );
return;
}
emptyDataTest();
regressionTest();
ts->set_failed_test_info( CvTS::OK );
}
/****************************************************************************************\ /****************************************************************************************\
* Regression tests for descriptor extractors. * * Regression tests for descriptor extractors. *
@ -707,6 +770,7 @@ void CV_DescriptorMatcherTest::run( int )
/* /*
* Detectors * Detectors
* "detector-fast, detector-gftt, detector-harris, detector-mser, detector-sift, detector-star, detector-surf"
*/ */
CV_FeatureDetectorTest fastTest( "detector-fast", createFeatureDetector("FAST") ); CV_FeatureDetectorTest fastTest( "detector-fast", createFeatureDetector("FAST") );
CV_FeatureDetectorTest gfttTest( "detector-gftt", createFeatureDetector("GFTT") ); CV_FeatureDetectorTest gfttTest( "detector-gftt", createFeatureDetector("GFTT") );

Loading…
Cancel
Save