test_descriptors_regression.impl.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. namespace opencv_test { namespace {
  5. /****************************************************************************************\
  6. * Regression tests for descriptor extractors. *
  7. \****************************************************************************************/
  8. static void writeMatInBin( const Mat& mat, const string& filename )
  9. {
  10. FILE* f = fopen( filename.c_str(), "wb");
  11. if( f )
  12. {
  13. CV_Assert(4 == sizeof(int));
  14. int type = mat.type();
  15. fwrite( (void*)&mat.rows, sizeof(int), 1, f );
  16. fwrite( (void*)&mat.cols, sizeof(int), 1, f );
  17. fwrite( (void*)&type, sizeof(int), 1, f );
  18. int dataSize = (int)(mat.step * mat.rows);
  19. fwrite( (void*)&dataSize, sizeof(int), 1, f );
  20. fwrite( (void*)mat.ptr(), 1, dataSize, f );
  21. fclose(f);
  22. }
  23. }
  24. static Mat readMatFromBin( const string& filename )
  25. {
  26. FILE* f = fopen( filename.c_str(), "rb" );
  27. if( f )
  28. {
  29. CV_Assert(4 == sizeof(int));
  30. int rows, cols, type, dataSize;
  31. size_t elements_read1 = fread( (void*)&rows, sizeof(int), 1, f );
  32. size_t elements_read2 = fread( (void*)&cols, sizeof(int), 1, f );
  33. size_t elements_read3 = fread( (void*)&type, sizeof(int), 1, f );
  34. size_t elements_read4 = fread( (void*)&dataSize, sizeof(int), 1, f );
  35. CV_Assert(elements_read1 == 1 && elements_read2 == 1 && elements_read3 == 1 && elements_read4 == 1);
  36. int step = dataSize / rows / CV_ELEM_SIZE(type);
  37. CV_Assert(step >= cols);
  38. Mat returnMat = Mat(rows, step, type).colRange(0, cols);
  39. size_t elements_read = fread( returnMat.ptr(), 1, dataSize, f );
  40. CV_Assert(elements_read == (size_t)(dataSize));
  41. fclose(f);
  42. return returnMat;
  43. }
  44. return Mat();
  45. }
  46. template<class Distance>
  47. class CV_DescriptorExtractorTest : public cvtest::BaseTest
  48. {
  49. public:
  50. typedef typename Distance::ValueType ValueType;
  51. typedef typename Distance::ResultType DistanceType;
  52. CV_DescriptorExtractorTest( const string _name, DistanceType _maxDist, const Ptr<DescriptorExtractor>& _dextractor,
  53. Distance d = Distance(), Ptr<FeatureDetector> _detector = Ptr<FeatureDetector>()):
  54. name(_name), maxDist(_maxDist), dextractor(_dextractor), distance(d) , detector(_detector) {}
  55. ~CV_DescriptorExtractorTest()
  56. {
  57. }
  58. protected:
  59. virtual void createDescriptorExtractor() {}
  60. void compareDescriptors( const Mat& validDescriptors, const Mat& calcDescriptors )
  61. {
  62. if( validDescriptors.size != calcDescriptors.size || validDescriptors.type() != calcDescriptors.type() )
  63. {
  64. ts->printf(cvtest::TS::LOG, "Valid and computed descriptors matrices must have the same size and type.\n");
  65. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
  66. return;
  67. }
  68. CV_Assert( DataType<ValueType>::type == validDescriptors.type() );
  69. int dimension = validDescriptors.cols;
  70. DistanceType curMaxDist = 0;
  71. size_t exact_count = 0, failed_count = 0;
  72. for( int y = 0; y < validDescriptors.rows; y++ )
  73. {
  74. DistanceType dist = distance( validDescriptors.ptr<ValueType>(y), calcDescriptors.ptr<ValueType>(y), dimension );
  75. if (dist == 0)
  76. exact_count++;
  77. if( dist > curMaxDist )
  78. {
  79. if (dist > maxDist)
  80. failed_count++;
  81. curMaxDist = dist;
  82. }
  83. #if 0
  84. if (dist > 0)
  85. {
  86. std::cout << "i=" << y << " fail_count=" << failed_count << " dist=" << dist << std::endl;
  87. std::cout << "valid: " << validDescriptors.row(y) << std::endl;
  88. std::cout << " calc: " << calcDescriptors.row(y) << std::endl;
  89. }
  90. #endif
  91. }
  92. float exact_percents = (100 * (float)exact_count / validDescriptors.rows);
  93. float failed_percents = (100 * (float)failed_count / validDescriptors.rows);
  94. std::stringstream ss;
  95. ss << "Exact count (dist == 0): " << exact_count << " (" << (int)exact_percents << "%)" << std::endl
  96. << "Failed count (dist > " << maxDist << "): " << failed_count << " (" << (int)failed_percents << "%)" << std::endl
  97. << "Max distance between valid and computed descriptors (" << validDescriptors.size() << "): " << curMaxDist;
  98. EXPECT_LE(failed_percents, 20.0f);
  99. std::cout << ss.str() << std::endl;
  100. }
  101. void emptyDataTest()
  102. {
  103. assert( dextractor );
  104. // One image.
  105. Mat image;
  106. vector<KeyPoint> keypoints;
  107. Mat descriptors;
  108. try
  109. {
  110. dextractor->compute( image, keypoints, descriptors );
  111. }
  112. catch(...)
  113. {
  114. ts->printf( cvtest::TS::LOG, "compute() on empty image and empty keypoints must not generate exception (1).\n");
  115. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
  116. }
  117. RNG rng;
  118. image = cvtest::randomMat(rng, Size(50, 50), CV_8UC3, 0, 255, false);
  119. try
  120. {
  121. dextractor->compute( image, keypoints, descriptors );
  122. }
  123. catch(...)
  124. {
  125. ts->printf( cvtest::TS::LOG, "compute() on nonempty image and empty keypoints must not generate exception (1).\n");
  126. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
  127. }
  128. // Several images.
  129. vector<Mat> images;
  130. vector<vector<KeyPoint> > keypointsCollection;
  131. vector<Mat> descriptorsCollection;
  132. try
  133. {
  134. dextractor->compute( images, keypointsCollection, descriptorsCollection );
  135. }
  136. catch(...)
  137. {
  138. ts->printf( cvtest::TS::LOG, "compute() on empty images and empty keypoints collection must not generate exception (2).\n");
  139. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
  140. }
  141. }
  142. void regressionTest()
  143. {
  144. assert( dextractor );
  145. // Read the test image.
  146. string imgFilename = string(ts->get_data_path()) + FEATURES2D_DIR + "/" + IMAGE_FILENAME;
  147. Mat img = imread( imgFilename );
  148. if( img.empty() )
  149. {
  150. ts->printf( cvtest::TS::LOG, "Image %s can not be read.\n", imgFilename.c_str() );
  151. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
  152. return;
  153. }
  154. const std::string keypoints_filename = string(ts->get_data_path()) +
  155. (detector.empty()
  156. ? (FEATURES2D_DIR + "/" + std::string("keypoints.xml.gz"))
  157. : (DESCRIPTOR_DIR + "/" + name + "_keypoints.xml.gz"));
  158. FileStorage fs(keypoints_filename, FileStorage::READ);
  159. vector<KeyPoint> keypoints;
  160. EXPECT_TRUE(fs.isOpened()) << "Keypoint testdata is missing. Re-computing and re-writing keypoints testdata...";
  161. if (!fs.isOpened())
  162. {
  163. fs.open(keypoints_filename, FileStorage::WRITE);
  164. ASSERT_TRUE(fs.isOpened()) << "File for writing keypoints can not be opened.";
  165. if (detector.empty())
  166. {
  167. Ptr<ORB> fd = ORB::create();
  168. fd->detect(img, keypoints);
  169. }
  170. else
  171. {
  172. detector->detect(img, keypoints);
  173. }
  174. write(fs, "keypoints", keypoints);
  175. fs.release();
  176. }
  177. else
  178. {
  179. read(fs.getFirstTopLevelNode(), keypoints);
  180. fs.release();
  181. }
  182. if(!detector.empty())
  183. {
  184. vector<KeyPoint> calcKeypoints;
  185. detector->detect(img, calcKeypoints);
  186. // TODO validate received keypoints
  187. int diff = abs((int)calcKeypoints.size() - (int)keypoints.size());
  188. if (diff > 0)
  189. {
  190. std::cout << "Keypoints difference: " << diff << std::endl;
  191. EXPECT_LE(diff, (int)(keypoints.size() * 0.03f));
  192. }
  193. }
  194. ASSERT_FALSE(keypoints.empty());
  195. {
  196. Mat calcDescriptors;
  197. double t = (double)getTickCount();
  198. dextractor->compute(img, keypoints, calcDescriptors);
  199. t = getTickCount() - t;
  200. ts->printf(cvtest::TS::LOG, "\nAverage time of computing one descriptor = %g ms.\n", t/((double)getTickFrequency()*1000.)/calcDescriptors.rows);
  201. if (calcDescriptors.rows != (int)keypoints.size())
  202. {
  203. ts->printf( cvtest::TS::LOG, "Count of computed descriptors and keypoints count must be equal.\n" );
  204. ts->printf( cvtest::TS::LOG, "Count of keypoints is %d.\n", (int)keypoints.size() );
  205. ts->printf( cvtest::TS::LOG, "Count of computed descriptors is %d.\n", calcDescriptors.rows );
  206. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  207. return;
  208. }
  209. if (calcDescriptors.cols != dextractor->descriptorSize() || calcDescriptors.type() != dextractor->descriptorType())
  210. {
  211. ts->printf( cvtest::TS::LOG, "Incorrect descriptor size or descriptor type.\n" );
  212. ts->printf( cvtest::TS::LOG, "Expected size is %d.\n", dextractor->descriptorSize() );
  213. ts->printf( cvtest::TS::LOG, "Calculated size is %d.\n", calcDescriptors.cols );
  214. ts->printf( cvtest::TS::LOG, "Expected type is %d.\n", dextractor->descriptorType() );
  215. ts->printf( cvtest::TS::LOG, "Calculated type is %d.\n", calcDescriptors.type() );
  216. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  217. return;
  218. }
  219. // TODO read and write descriptor extractor parameters and check them
  220. Mat validDescriptors = readDescriptors();
  221. EXPECT_FALSE(validDescriptors.empty()) << "Descriptors testdata is missing. Re-writing descriptors testdata...";
  222. if (!validDescriptors.empty())
  223. {
  224. compareDescriptors(validDescriptors, calcDescriptors);
  225. }
  226. else
  227. {
  228. ASSERT_TRUE(writeDescriptors(calcDescriptors)) << "Descriptors can not be written.";
  229. }
  230. }
  231. }
  232. void run(int)
  233. {
  234. createDescriptorExtractor();
  235. if( !dextractor )
  236. {
  237. ts->printf(cvtest::TS::LOG, "Descriptor extractor is empty.\n");
  238. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
  239. return;
  240. }
  241. emptyDataTest();
  242. regressionTest();
  243. ts->set_failed_test_info( cvtest::TS::OK );
  244. }
  245. virtual Mat readDescriptors()
  246. {
  247. Mat res = readMatFromBin( string(ts->get_data_path()) + DESCRIPTOR_DIR + "/" + string(name) );
  248. return res;
  249. }
  250. virtual bool writeDescriptors( Mat& descs )
  251. {
  252. writeMatInBin( descs, string(ts->get_data_path()) + DESCRIPTOR_DIR + "/" + string(name) );
  253. return true;
  254. }
  255. string name;
  256. const DistanceType maxDist;
  257. Ptr<DescriptorExtractor> dextractor;
  258. Distance distance;
  259. Ptr<FeatureDetector> detector;
  260. private:
  261. CV_DescriptorExtractorTest& operator=(const CV_DescriptorExtractorTest&) { return *this; }
  262. };
  263. }} // namespace