test_matcher_regression.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  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.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2014, Biagio Montesano, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  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.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  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.
  39. //
  40. //M*/
  41. #include "test_precomp.hpp"
  42. namespace opencv_test { namespace {
  43. class CV_BinaryDescriptorMatcherTest : public cvtest::BaseTest
  44. {
  45. public:
  46. CV_BinaryDescriptorMatcherTest( float _badPart ) :
  47. badPart( _badPart )
  48. {
  49. dmatcher = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
  50. }
  51. protected:
  52. static const int dim = 32;
  53. static const int queryDescCount = 300; // must be even number because we split train data in some cases in two
  54. static const int countFactor = 4; // do not change it
  55. const float badPart;
  56. virtual void run( int );
  57. void generateData( Mat& query, Mat& train );
  58. uchar invertSingleBits( uchar dividend_char, int numBits );
  59. void emptyDataTest();
  60. void matchTest( const Mat& query, const Mat& train );
  61. void knnMatchTest( const Mat& query, const Mat& train );
  62. void radiusMatchTest( const Mat& query, const Mat& train );
  63. std::string name;
  64. Ptr<BinaryDescriptorMatcher> dmatcher;
  65. private:
  66. CV_BinaryDescriptorMatcherTest& operator=( const CV_BinaryDescriptorMatcherTest& )
  67. {
  68. return *this;
  69. }
  70. };
  71. /* invert numBits bits in input char */
  72. uchar CV_BinaryDescriptorMatcherTest::invertSingleBits( uchar dividend_char, int numBits )
  73. {
  74. std::vector<int> bin_vector;
  75. long dividend;
  76. long bin_num;
  77. /* convert input char to a long */
  78. dividend = (long) dividend_char;
  79. /*if a 0 has been obtained, just generate a 8-bit long vector of zeros */
  80. if( dividend == 0 )
  81. bin_vector = std::vector<int>( 8, 0 );
  82. /* else, apply classic decimal to binary conversion */
  83. else
  84. {
  85. while ( dividend >= 1 )
  86. {
  87. bin_num = dividend % 2;
  88. dividend /= 2;
  89. bin_vector.push_back( bin_num );
  90. }
  91. }
  92. /* ensure that binary vector always has length 8 */
  93. if( bin_vector.size() < 8 )
  94. {
  95. std::vector<int> zeros( 8 - bin_vector.size(), 0 );
  96. bin_vector.insert( bin_vector.end(), zeros.begin(), zeros.end() );
  97. }
  98. /* invert numBits bits */
  99. for ( int index = 0; index < numBits; index++ )
  100. {
  101. if( bin_vector[index] == 0 )
  102. bin_vector[index] = 1;
  103. else
  104. bin_vector[index] = 0;
  105. }
  106. /* reconvert to decimal */
  107. uchar result = 0;
  108. for ( int i = (int) bin_vector.size() - 1; i >= 0; i-- )
  109. result += (uchar) ( bin_vector[i] * ( 1 << i ) );
  110. return result;
  111. }
  112. void CV_BinaryDescriptorMatcherTest::emptyDataTest()
  113. {
  114. Mat queryDescriptors, trainDescriptors, mask;
  115. std::vector<Mat> trainDescriptorCollection, masks;
  116. std::vector<DMatch> matches;
  117. std::vector<std::vector<DMatch> > vmatches;
  118. try
  119. {
  120. dmatcher->match( queryDescriptors, trainDescriptors, matches, mask );
  121. }
  122. catch ( ... )
  123. {
  124. ts->printf( cvtest::TS::LOG, "match() on empty descriptors must not generate exception (1).\n" );
  125. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  126. }
  127. try
  128. {
  129. dmatcher->knnMatch( queryDescriptors, trainDescriptors, vmatches, 2, mask );
  130. }
  131. catch ( ... )
  132. {
  133. ts->printf( cvtest::TS::LOG, "knnMatch() on empty descriptors must not generate exception (1).\n" );
  134. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  135. }
  136. try
  137. {
  138. dmatcher->radiusMatch( queryDescriptors, trainDescriptors, vmatches, 10.f, mask );
  139. }
  140. catch ( ... )
  141. {
  142. ts->printf( cvtest::TS::LOG, "radiusMatch() on empty descriptors must not generate exception (1).\n" );
  143. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  144. }
  145. try
  146. {
  147. dmatcher->add( trainDescriptorCollection );
  148. }
  149. catch ( ... )
  150. {
  151. ts->printf( cvtest::TS::LOG, "add() on empty descriptors must not generate exception.\n" );
  152. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  153. }
  154. try
  155. {
  156. dmatcher->match( queryDescriptors, matches, masks );
  157. }
  158. catch ( ... )
  159. {
  160. ts->printf( cvtest::TS::LOG, "match() on empty descriptors must not generate exception (2).\n" );
  161. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  162. }
  163. try
  164. {
  165. dmatcher->knnMatch( queryDescriptors, vmatches, 2, masks );
  166. }
  167. catch ( ... )
  168. {
  169. ts->printf( cvtest::TS::LOG, "knnMatch() on empty descriptors must not generate exception (2).\n" );
  170. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  171. }
  172. try
  173. {
  174. dmatcher->radiusMatch( queryDescriptors, vmatches, 10.f, masks );
  175. }
  176. catch ( ... )
  177. {
  178. ts->printf( cvtest::TS::LOG, "radiusMatch() on empty descriptors must not generate exception (2).\n" );
  179. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  180. }
  181. }
  182. void CV_BinaryDescriptorMatcherTest::generateData( Mat& query, Mat& train )
  183. {
  184. RNG& rng = theRNG();
  185. /* Generate query descriptors randomly.
  186. Descriptor vector elements are binary values. */
  187. Mat buf( queryDescCount, dim, CV_8UC1 );
  188. rng.fill( buf, RNG::UNIFORM, Scalar( 0 ), Scalar( 255 ) );
  189. buf.convertTo( query, CV_8UC1 );
  190. for ( int i = 0; i < query.rows; i++ )
  191. {
  192. for ( int j = 0; j < countFactor; j++ )
  193. {
  194. train.push_back( query.row( i ) );
  195. int randCol = rand() % 32;
  196. uchar u = query.at<uchar>( i, randCol );
  197. uchar modified_u = invertSingleBits( u, j + 1 );
  198. train.at<uchar>( i * countFactor + j, randCol ) = modified_u;
  199. }
  200. }
  201. }
  202. void CV_BinaryDescriptorMatcherTest::matchTest( const Mat& query, const Mat& train )
  203. {
  204. dmatcher->clear();
  205. // test const version of match()
  206. {
  207. std::vector<DMatch> matches;
  208. dmatcher->match( query, train, matches );
  209. if( (int) matches.size() != queryDescCount )
  210. {
  211. ts->printf( cvtest::TS::LOG, "Incorrect matches count while test match() function (1).\n" );
  212. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  213. }
  214. else
  215. {
  216. int badCount = 0;
  217. for ( size_t i = 0; i < matches.size(); i++ )
  218. {
  219. DMatch& match = matches[i];
  220. if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor ) || ( match.imgIdx != 0 ) )
  221. badCount++;
  222. }
  223. if( (float) badCount > (float) queryDescCount * badPart )
  224. {
  225. ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test match() function (1).\n",
  226. (float) badCount / (float) queryDescCount );
  227. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  228. }
  229. }
  230. }
  231. // test const version of match() for the same query and test descriptors
  232. {
  233. std::vector<DMatch> matches;
  234. dmatcher->match( query, query, matches );
  235. if( (int) matches.size() != query.rows )
  236. {
  237. ts->printf( cvtest::TS::LOG, "Incorrect matches count while test match() function for the same query and test descriptors (1).\n" );
  238. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  239. }
  240. else
  241. {
  242. for ( size_t i = 0; i < matches.size(); i++ )
  243. {
  244. DMatch& match = matches[i];
  245. if( match.queryIdx != (int) i || match.trainIdx != (int) i || std::abs( match.distance ) > FLT_EPSILON )
  246. {
  247. ts->printf(
  248. cvtest::TS::LOG,
  249. "Bad match (i=%d, queryIdx=%d, trainIdx=%d, distance=%f) while test match() function for the same query and test descriptors (1).\n", i,
  250. match.queryIdx, match.trainIdx, match.distance );
  251. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  252. }
  253. }
  254. }
  255. }
  256. // test version of match() with add()
  257. {
  258. dmatcher->clear();
  259. std::vector<DMatch> matches;
  260. // make add() twice to test such case
  261. dmatcher->add( std::vector<Mat>( 1, train.rowRange( 0, train.rows / 2 ) ) );
  262. dmatcher->add( std::vector<Mat>( 1, train.rowRange( train.rows / 2, train.rows ) ) );
  263. // prepare masks (make first nearest match illegal)
  264. std::vector<Mat> masks( 2 );
  265. for ( int mi = 0; mi < 2; mi++ )
  266. masks[mi] = Mat::ones( query.rows, 1/*train.rows / 2*/, CV_8UC1 );
  267. dmatcher->match( query, matches, masks );
  268. if( (int) matches.size() != queryDescCount )
  269. {
  270. ts->printf( cvtest::TS::LOG, "Incorrect matches count while test match() function (2).\n" );
  271. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  272. }
  273. else
  274. {
  275. int badCount = 0;
  276. for ( size_t i = 0; i < matches.size(); i++ )
  277. {
  278. DMatch& match = matches[i];
  279. if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor /*+ shift*/) || ( match.imgIdx > 1 ) )
  280. badCount++;
  281. }
  282. if( (float) badCount > (float) queryDescCount * badPart )
  283. {
  284. ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test match() function (2).\n",
  285. (float) badCount / (float) queryDescCount );
  286. ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
  287. }
  288. }
  289. }
  290. }
  291. void CV_BinaryDescriptorMatcherTest::knnMatchTest( const Mat& query, const Mat& train )
  292. {
  293. dmatcher->clear();
  294. // test const version of knnMatch()
  295. {
  296. const int knn = 3;
  297. std::vector<std::vector<DMatch> > matches;
  298. dmatcher->knnMatch( query, train, matches, knn );
  299. if( (int) matches.size() != queryDescCount )
  300. {
  301. ts->printf( cvtest::TS::LOG, "Incorrect matches count while test knnMatch() function (1).\n" );
  302. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  303. }
  304. else
  305. {
  306. int badCount = 0;
  307. for ( size_t i = 0; i < matches.size(); i++ )
  308. {
  309. if( (int) matches[i].size() != knn )
  310. badCount++;
  311. else
  312. {
  313. int localBadCount = 0;
  314. for ( int k = 0; k < knn; k++ )
  315. {
  316. DMatch& match = matches[i][k];
  317. if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor + k ) || ( match.imgIdx != 0 ) )
  318. localBadCount++;
  319. }
  320. badCount += localBadCount > 0 ? 1 : 0;
  321. }
  322. }
  323. if( (float) badCount > (float) queryDescCount * badPart )
  324. {
  325. ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test knnMatch() function (1).\n",
  326. (float) badCount / (float) queryDescCount );
  327. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  328. }
  329. }
  330. }
  331. // // test version of knnMatch() with add()
  332. {
  333. const int knn = 2;
  334. std::vector<std::vector<DMatch> > matches;
  335. // make add() twice to test such case
  336. dmatcher->add( std::vector<Mat>( 1, train.rowRange( 0, train.rows / 2 ) ) );
  337. dmatcher->add( std::vector<Mat>( 1, train.rowRange( train.rows / 2, train.rows ) ) );
  338. // prepare masks (make first nearest match illegal)
  339. std::vector<Mat> masks( 2 );
  340. for ( int mi = 0; mi < 2; mi++ )
  341. {
  342. masks[mi] = Mat::ones( query.rows, 1, CV_8UC1 );
  343. }
  344. dmatcher->knnMatch( query, matches, knn, masks );
  345. if( (int) matches.size() != queryDescCount )
  346. {
  347. ts->printf( cvtest::TS::LOG, "Incorrect matches count while test knnMatch() function (2).\n" );
  348. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  349. }
  350. else
  351. {
  352. int badCount = 0;
  353. for ( size_t i = 0; i < matches.size(); i++ )
  354. {
  355. if( (int) matches[i].size() != knn )
  356. badCount++;
  357. else
  358. {
  359. int localBadCount = 0;
  360. for ( int k = 0; k < knn; k++ )
  361. {
  362. DMatch& match = matches[i][k];
  363. {
  364. if( i < queryDescCount / 2 )
  365. {
  366. if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor + k ) || ( match.imgIdx != 0 ) )
  367. localBadCount++;
  368. }
  369. else
  370. {
  371. if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor + k ) || ( match.imgIdx != 1 ) )
  372. localBadCount++;
  373. }
  374. }
  375. }
  376. badCount += localBadCount > 0 ? 1 : 0;
  377. }
  378. }
  379. if( (float) badCount > (float) queryDescCount * badPart )
  380. {
  381. ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test knnMatch() function (2).\n",
  382. (float) badCount / (float) queryDescCount );
  383. ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
  384. }
  385. }
  386. }
  387. }
  388. void CV_BinaryDescriptorMatcherTest::radiusMatchTest( const Mat& query, const Mat& train )
  389. {
  390. dmatcher->clear();
  391. // test const version of match()
  392. {
  393. const float radius = 1;
  394. std::vector<std::vector<DMatch> > matches;
  395. dmatcher->radiusMatch( query, train, matches, radius );
  396. if( (int) matches.size() != queryDescCount )
  397. {
  398. ts->printf( cvtest::TS::LOG, "Incorrect matches count while test radiusMatch() function (1).\n" );
  399. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  400. }
  401. else
  402. {
  403. int badCount = 0;
  404. for ( size_t i = 0; i < matches.size(); i++ )
  405. {
  406. if( (int) matches[i].size() != 1 )
  407. {
  408. badCount++;
  409. }
  410. else
  411. {
  412. DMatch& match = matches[i][0];
  413. if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor ) || ( match.imgIdx != 0 ) )
  414. badCount++;
  415. }
  416. }
  417. if( (float) badCount > (float) queryDescCount * badPart )
  418. {
  419. ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test radiusMatch() function (1).\n",
  420. (float) badCount / (float) queryDescCount );
  421. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  422. }
  423. }
  424. }
  425. {
  426. const float radius = 3;
  427. std::vector<std::vector<DMatch> > matches;
  428. // make add() twice to test such case
  429. dmatcher->add( std::vector<Mat>( 1, train.rowRange( 0, train.rows / 2 ) ) );
  430. dmatcher->add( std::vector<Mat>( 1, train.rowRange( train.rows / 2, train.rows ) ) );
  431. // prepare masks
  432. std::vector<Mat> masks( 2 );
  433. for ( int mi = 0; mi < 2; mi++ )
  434. masks[mi] = Mat::ones( query.rows, 1, CV_8UC1 );
  435. dmatcher->radiusMatch( query, matches, radius, masks );
  436. //int curRes = cvtest::TS::OK;
  437. if( (int) matches.size() != queryDescCount )
  438. {
  439. ts->printf( cvtest::TS::LOG, "Incorrect matches count while test radiusMatch() function (1).\n" );
  440. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  441. }
  442. int badCount = 0;
  443. for ( size_t i = 0; i < matches.size(); i++ )
  444. {
  445. if( (int) matches[i].size() != radius )
  446. badCount++;
  447. else
  448. {
  449. int localBadCount = 0;
  450. for ( int k = 0; k < radius; k++ )
  451. {
  452. DMatch& match = matches[i][k];
  453. {
  454. if( i < queryDescCount / 2 )
  455. {
  456. if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor + k ) || ( match.imgIdx != 0 ) )
  457. localBadCount++;
  458. }
  459. else
  460. {
  461. if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor + k ) || ( match.imgIdx != 1 ) )
  462. localBadCount++;
  463. }
  464. }
  465. }
  466. badCount += localBadCount > 0 ? 1 : 0;
  467. }
  468. }
  469. if( (float) badCount > (float) queryDescCount * badPart )
  470. {
  471. //curRes = cvtest::TS::FAIL_INVALID_OUTPUT;
  472. ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test radiusMatch() function (2).\n",
  473. (float) badCount / (float) queryDescCount );
  474. ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
  475. }
  476. }
  477. }
  478. void CV_BinaryDescriptorMatcherTest::run( int )
  479. {
  480. Mat query, train;
  481. emptyDataTest();
  482. generateData( query, train );
  483. matchTest( query, train );
  484. knnMatchTest( query, train );
  485. radiusMatchTest( query, train );
  486. }
  487. /****************************************************************************************\
  488. * Tests registrations *
  489. \****************************************************************************************/
  490. TEST( BinaryDescriptor_Matcher, regression)
  491. {
  492. CV_BinaryDescriptorMatcherTest test( 0.01f );
  493. test.safe_run();
  494. }
  495. }} // namespace