1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html
4 
5 namespace opencv_test { namespace {
6 
7 /****************************************************************************************\
8 *            Regression tests for feature detectors comparing keypoints.                 *
9 \****************************************************************************************/
10 
11 class CV_FeatureDetectorTest : public cvtest::BaseTest
12 {
13 public:
CV_FeatureDetectorTest(const string & _name,const Ptr<FeatureDetector> & _fdetector)14     CV_FeatureDetectorTest( const string& _name, const Ptr<FeatureDetector>& _fdetector ) :
15         name(_name), fdetector(_fdetector) {}
16 
17 protected:
18     bool isSimilarKeypoints( const KeyPoint& p1, const KeyPoint& p2 );
19     void compareKeypointSets( const vector<KeyPoint>& validKeypoints, const vector<KeyPoint>& calcKeypoints );
20 
21     void emptyDataTest();
22     void regressionTest(); // TODO test of detect() with mask
23 
24     virtual void run( int );
25 
26     string name;
27     Ptr<FeatureDetector> fdetector;
28 };
29 
emptyDataTest()30 void CV_FeatureDetectorTest::emptyDataTest()
31 {
32     // One image.
33     Mat image;
34     vector<KeyPoint> keypoints;
35     try
36     {
37         fdetector->detect( image, keypoints );
38     }
39     catch(...)
40     {
41         ts->printf( cvtest::TS::LOG, "detect() on empty image must not generate exception (1).\n" );
42         ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
43     }
44 
45     if( !keypoints.empty() )
46     {
47         ts->printf( cvtest::TS::LOG, "detect() on empty image must return empty keypoints vector (1).\n" );
48         ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
49         return;
50     }
51 
52     // Several images.
53     vector<Mat> images;
54     vector<vector<KeyPoint> > keypointCollection;
55     try
56     {
57         fdetector->detect( images, keypointCollection );
58     }
59     catch(...)
60     {
61         ts->printf( cvtest::TS::LOG, "detect() on empty image vector must not generate exception (2).\n" );
62         ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
63     }
64 }
65 
isSimilarKeypoints(const KeyPoint & p1,const KeyPoint & p2)66 bool CV_FeatureDetectorTest::isSimilarKeypoints( const KeyPoint& p1, const KeyPoint& p2 )
67 {
68     const float maxPtDif = 1.f;
69     const float maxSizeDif = 1.f;
70     const float maxAngleDif = 2.f;
71     const float maxResponseDif = 0.1f;
72 
73     float dist = (float)cv::norm( p1.pt - p2.pt );
74     return (dist < maxPtDif &&
75             fabs(p1.size - p2.size) < maxSizeDif &&
76             abs(p1.angle - p2.angle) < maxAngleDif &&
77             abs(p1.response - p2.response) < maxResponseDif &&
78             p1.octave == p2.octave &&
79             p1.class_id == p2.class_id );
80 }
81 
compareKeypointSets(const vector<KeyPoint> & validKeypoints,const vector<KeyPoint> & calcKeypoints)82 void CV_FeatureDetectorTest::compareKeypointSets( const vector<KeyPoint>& validKeypoints, const vector<KeyPoint>& calcKeypoints )
83 {
84     const float maxCountRatioDif = 0.01f;
85 
86     // Compare counts of validation and calculated keypoints.
87     float countRatio = (float)validKeypoints.size() / (float)calcKeypoints.size();
88     if( countRatio < 1 - maxCountRatioDif || countRatio > 1.f + maxCountRatioDif )
89     {
90         ts->printf( cvtest::TS::LOG, "Bad keypoints count ratio (validCount = %d, calcCount = %d).\n",
91                     validKeypoints.size(), calcKeypoints.size() );
92         ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
93         return;
94     }
95 
96     int progress = 0, progressCount = (int)(validKeypoints.size() * calcKeypoints.size());
97     int badPointCount = 0, commonPointCount = max((int)validKeypoints.size(), (int)calcKeypoints.size());
98     for( size_t v = 0; v < validKeypoints.size(); v++ )
99     {
100         int nearestIdx = -1;
101         float minDist = std::numeric_limits<float>::max();
102 
103         for( size_t c = 0; c < calcKeypoints.size(); c++ )
104         {
105             progress = update_progress( progress, (int)(v*calcKeypoints.size() + c), progressCount, 0 );
106             float curDist = (float)cv::norm( calcKeypoints[c].pt - validKeypoints[v].pt );
107             if( curDist < minDist )
108             {
109                 minDist = curDist;
110                 nearestIdx = (int)c;
111             }
112         }
113 
114         assert( minDist >= 0 );
115         if( !isSimilarKeypoints( validKeypoints[v], calcKeypoints[nearestIdx] ) )
116             badPointCount++;
117     }
118     ts->printf( cvtest::TS::LOG, "badPointCount = %d; validPointCount = %d; calcPointCount = %d\n",
119                 badPointCount, validKeypoints.size(), calcKeypoints.size() );
120     if( badPointCount > 0.9 * commonPointCount )
121     {
122         ts->printf( cvtest::TS::LOG, " - Bad accuracy!\n" );
123         ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
124         return;
125     }
126     ts->printf( cvtest::TS::LOG, " - OK\n" );
127 }
128 
regressionTest()129 void CV_FeatureDetectorTest::regressionTest()
130 {
131     assert( !fdetector.empty() );
132     string imgFilename = string(ts->get_data_path()) + FEATURES2D_DIR + "/" + IMAGE_FILENAME;
133     string resFilename = string(ts->get_data_path()) + DETECTOR_DIR + "/" + string(name) + ".xml.gz";
134 
135     // Read the test image.
136     Mat image = imread( imgFilename );
137     if( image.empty() )
138     {
139         ts->printf( cvtest::TS::LOG, "Image %s can not be read.\n", imgFilename.c_str() );
140         ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
141         return;
142     }
143 
144     FileStorage fs( resFilename, FileStorage::READ );
145 
146     // Compute keypoints.
147     vector<KeyPoint> calcKeypoints;
148     fdetector->detect( image, calcKeypoints );
149 
150     if( fs.isOpened() ) // Compare computed and valid keypoints.
151     {
152         // TODO compare saved feature detector params with current ones
153 
154         // Read validation keypoints set.
155         vector<KeyPoint> validKeypoints;
156         read( fs["keypoints"], validKeypoints );
157         if( validKeypoints.empty() )
158         {
159             ts->printf( cvtest::TS::LOG, "Keypoints can not be read.\n" );
160             ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
161             return;
162         }
163 
164         compareKeypointSets( validKeypoints, calcKeypoints );
165     }
166     else // Write detector parameters and computed keypoints as validation data.
167     {
168         fs.open( resFilename, FileStorage::WRITE );
169         if( !fs.isOpened() )
170         {
171             ts->printf( cvtest::TS::LOG, "File %s can not be opened to write.\n", resFilename.c_str() );
172             ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
173             return;
174         }
175         else
176         {
177             fs << "detector_params" << "{";
178             fdetector->write( fs );
179             fs << "}";
180 
181             write( fs, "keypoints", calcKeypoints );
182         }
183     }
184 }
185 
run(int)186 void CV_FeatureDetectorTest::run( int /*start_from*/ )
187 {
188     if( !fdetector )
189     {
190         ts->printf( cvtest::TS::LOG, "Feature detector is empty.\n" );
191         ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
192         return;
193     }
194 
195     emptyDataTest();
196     regressionTest();
197 
198     ts->set_failed_test_info( cvtest::TS::OK );
199 }
200 
201 }} // namespace
202