perf_matching.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "perf_precomp.hpp"
  42. namespace opencv_test { namespace {
  43. #define QUERY_DES_COUNT 300
  44. #define DIM 32
  45. #define COUNT_FACTOR 4
  46. #define RADIUS 3
  47. void generateData( Mat& query, Mat& train );
  48. uchar invertSingleBits( uchar dividend_char, int numBits );
  49. /* invert numBits bits in input char */
  50. uchar invertSingleBits( uchar dividend_char, int numBits )
  51. {
  52. std::vector<int> bin_vector;
  53. long dividend;
  54. long bin_num;
  55. /* convert input char to a long */
  56. dividend = (long) dividend_char;
  57. /*if a 0 has been obtained, just generate a 8-bit long vector of zeros */
  58. if( dividend == 0 )
  59. bin_vector = std::vector<int>( 8, 0 );
  60. /* else, apply classic decimal to binary conversion */
  61. else
  62. {
  63. while ( dividend >= 1 )
  64. {
  65. bin_num = dividend % 2;
  66. dividend /= 2;
  67. bin_vector.push_back( bin_num );
  68. }
  69. }
  70. /* ensure that binary vector always has length 8 */
  71. if( bin_vector.size() < 8 )
  72. {
  73. std::vector<int> zeros( 8 - bin_vector.size(), 0 );
  74. bin_vector.insert( bin_vector.end(), zeros.begin(), zeros.end() );
  75. }
  76. /* invert numBits bits */
  77. for ( int index = 0; index < numBits; index++ )
  78. {
  79. if( bin_vector[index] == 0 )
  80. bin_vector[index] = 1;
  81. else
  82. bin_vector[index] = 0;
  83. }
  84. /* reconvert to decimal */
  85. uchar result = 0;
  86. for ( int i = (int) bin_vector.size() - 1; i >= 0; i-- )
  87. result += (uchar) ( bin_vector[i] * ( 1 << i ) );
  88. return result;
  89. }
  90. void generateData( Mat& query, Mat& train )
  91. {
  92. RNG& rng = theRNG();
  93. Mat buf( QUERY_DES_COUNT, DIM, CV_8UC1 );
  94. rng.fill( buf, RNG::UNIFORM, Scalar( 0 ), Scalar( 255 ) );
  95. buf.convertTo( query, CV_8UC1 );
  96. for ( int i = 0; i < query.rows; i++ )
  97. {
  98. for ( int j = 0; j < COUNT_FACTOR; j++ )
  99. {
  100. train.push_back( query.row( i ) );
  101. int randCol = rand() % 32;
  102. uchar u = query.at<uchar>( i, randCol );
  103. uchar modified_u = invertSingleBits( u, j + 1 );
  104. train.at<uchar>( i * COUNT_FACTOR + j, randCol ) = modified_u;
  105. }
  106. }
  107. }
  108. PERF_TEST(matching, single_match)
  109. {
  110. Mat query, train;
  111. std::vector<DMatch> dm;
  112. Ptr<BinaryDescriptorMatcher> bd = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
  113. generateData( query, train );
  114. TEST_CYCLE()
  115. bd->match( query, train, dm );
  116. SANITY_CHECK_NOTHING();
  117. }
  118. PERF_TEST(knn_matching, knn_match_distances_test)
  119. {
  120. Mat query, train, distances;
  121. std::vector<std::vector<DMatch> > dm;
  122. Ptr<BinaryDescriptorMatcher> bd = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
  123. generateData( query, train );
  124. TEST_CYCLE()
  125. {
  126. bd->knnMatch( query, train, dm, QUERY_DES_COUNT );
  127. for ( int i = 0; i < (int) dm.size(); i++ )
  128. {
  129. for ( int j = 0; j < (int) dm[i].size(); j++ )
  130. distances.push_back( dm[i][j].distance );
  131. }
  132. }
  133. SANITY_CHECK_NOTHING();
  134. }
  135. PERF_TEST(radius_match, radius_match_distances_test)
  136. {
  137. Mat query, train, distances;
  138. std::vector<std::vector<DMatch> > dm;
  139. Ptr<BinaryDescriptorMatcher> bd = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
  140. generateData( query, train );
  141. TEST_CYCLE()
  142. {
  143. bd->radiusMatch( query, train, dm, RADIUS );
  144. for ( int i = 0; i < (int) dm.size(); i++ )
  145. {
  146. for ( int j = 0; j < (int) dm[i].size(); j++ )
  147. distances.push_back( dm[i][j].distance );
  148. }
  149. }
  150. SANITY_CHECK_NOTHING();
  151. }
  152. }} // namespace