test_block_mean_hash.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #include "test_precomp.hpp"
  5. #include <bitset>
  6. namespace opencv_test { namespace {
  7. using namespace cv::img_hash;
  8. /**
  9. *The expected results of this test case are come from the Phash library,
  10. *I use it as golden model
  11. */
  12. class CV_BlockMeanHashTest : public cvtest::BaseTest
  13. {
  14. public:
  15. CV_BlockMeanHashTest();
  16. protected:
  17. void run(int /* idx */);
  18. void testMeanMode0();
  19. void testMeanMode1();
  20. void testHashMode0();
  21. void testHashMode1();
  22. cv::Mat input;
  23. cv::Mat hash;
  24. Ptr<cv::img_hash::BlockMeanHash> bmh;
  25. };
  26. CV_BlockMeanHashTest::CV_BlockMeanHashTest()
  27. {
  28. input.create(256, 256, CV_8U);
  29. for(int row = 0; row != input.rows; ++row)
  30. {
  31. uchar value = static_cast<uchar>(row);
  32. for(int col = 0; col != input.cols; ++col)
  33. {
  34. input.at<uchar>(row, col) = value++;
  35. }
  36. }
  37. bmh = BlockMeanHash::create(BLOCK_MEAN_HASH_MODE_0);
  38. }
  39. void CV_BlockMeanHashTest::testMeanMode0()
  40. {
  41. std::vector<double> const &features = bmh->getMean();
  42. double const expectResult[] =
  43. {15,31,47,63,79,95,111,127,143,159,175,191,207,223,239,135,
  44. 31,47,63,79,95,111,127,143,159,175,191,207,223,239,135,15,
  45. 47,63,79,95,111,127,143,159,175,191,207,223,239,135,15,31,
  46. 63,79,95,111,127,143,159,175,191,207,223,239,135,15,31,47,
  47. 79,95,111,127,143,159,175,191,207,223,239,135,15,31,47,63,
  48. 95,111,127,143,159,175,191,207,223,239,135,15,31,47,63,79,
  49. 111,127,143,159,175,191,207,223,239,135,15,31,47,63,79,95,
  50. 127,143,159,175,191,207,223,239,135,15,31,47,63,79,95,111,
  51. 143,159,175,191,207,223,239,135,15,31,47,63,79,95,111,127,
  52. 159,175,191,207,223,239,135,15,31,47,63,79,95,111,127,143,
  53. 175,191,207,223,239,135,15,31,47,63,79,95,111,127,143,159,
  54. 191,207,223,239,135,15,31,47,63,79,95,111,127,143,159,175,
  55. 207,223,239,135,15,31,47,63,79,95,111,127,143,159,175,191,
  56. 223,239,135,15,31,47,63,79,95,111,127,143,159,175,191,207,
  57. 239,135,15,31,47,63,79,95,111,127,143,159,175,191,207,223,
  58. 135,15,31,47,63,79,95,111,127,143,159,175,191,207,223,239,};
  59. for(size_t i = 0; i != features.size(); ++i)
  60. {
  61. ASSERT_NEAR(features[i], expectResult[i], 0.0001);
  62. }
  63. }
  64. void CV_BlockMeanHashTest::testMeanMode1()
  65. {
  66. std::vector<double> const &features = bmh->getMean();
  67. double const expectResult[] =
  68. {15,23,31,39,47,55,63,71,79,87,95,103,111,119,127,135,143,151,159,167,175,183,191,199,207,215,223,231,239,219,135,
  69. 23,31,39,47,55,63,71,79,87,95,103,111,119,127,135,143,151,159,167,175,183,191,199,207,215,223,231,239,219,135,43,
  70. 31,39,47,55,63,71,79,87,95,103,111,119,127,135,143,151,159,167,175,183,191,199,207,215,223,231,239,219,135,43,15,
  71. 39,47,55,63,71,79,87,95,103,111,119,127,135,143,151,159,167,175,183,191,199,207,215,223,231,239,219,135,43,15,23,
  72. 47,55,63,71,79,87,95,103,111,119,127,135,143,151,159,167,175,183,191,199,207,215,223,231,239,219,135,43,15,23,31,
  73. 55,63,71,79,87,95,103,111,119,127,135,143,151,159,167,175,183,191,199,207,215,223,231,239,219,135,43,15,23,31,39,
  74. 63,71,79,87,95,103,111,119,127,135,143,151,159,167,175,183,191,199,207,215,223,231,239,219,135,43,15,23,31,39,47,
  75. 71,79,87,95,103,111,119,127,135,143,151,159,167,175,183,191,199,207,215,223,231,239,219,135,43,15,23,31,39,47,55,
  76. 79,87,95,103,111,119,127,135,143,151,159,167,175,183,191,199,207,215,223,231,239,219,135,43,15,23,31,39,47,55,63,
  77. 87,95,103,111,119,127,135,143,151,159,167,175,183,191,199,207,215,223,231,239,219,135,43,15,23,31,39,47,55,63,71,
  78. 95,103,111,119,127,135,143,151,159,167,175,183,191,199,207,215,223,231,239,219,135,43,15,23,31,39,47,55,63,71,79,
  79. 103,111,119,127,135,143,151,159,167,175,183,191,199,207,215,223,231,239,219,135,43,15,23,31,39,47,55,63,71,79,87,
  80. 111,119,127,135,143,151,159,167,175,183,191,199,207,215,223,231,239,219,135,43,15,23,31,39,47,55,63,71,79,87,95,
  81. 119,127,135,143,151,159,167,175,183,191,199,207,215,223,231,239,219,135,43,15,23,31,39,47,55,63,71,79,87,95,103,
  82. 127,135,143,151,159,167,175,183,191,199,207,215,223,231,239,219,135,43,15,23,31,39,47,55,63,71,79,87,95,103,111,
  83. 135,143,151,159,167,175,183,191,199,207,215,223,231,239,219,135,43,15,23,31,39,47,55,63,71,79,87,95,103,111,119,
  84. 143,151,159,167,175,183,191,199,207,215,223,231,239,219,135,43,15,23,31,39,47,55,63,71,79,87,95,103,111,119,127,
  85. 151,159,167,175,183,191,199,207,215,223,231,239,219,135,43,15,23,31,39,47,55,63,71,79,87,95,103,111,119,127,135,
  86. 159,167,175,183,191,199,207,215,223,231,239,219,135,43,15,23,31,39,47,55,63,71,79,87,95,103,111,119,127,135,143,
  87. 167,175,183,191,199,207,215,223,231,239,219,135,43,15,23,31,39,47,55,63,71,79,87,95,103,111,119,127,135,143,151,
  88. 175,183,191,199,207,215,223,231,239,219,135,43,15,23,31,39,47,55,63,71,79,87,95,103,111,119,127,135,143,151,159,
  89. 183,191,199,207,215,223,231,239,219,135,43,15,23,31,39,47,55,63,71,79,87,95,103,111,119,127,135,143,151,159,167,
  90. 191,199,207,215,223,231,239,219,135,43,15,23,31,39,47,55,63,71,79,87,95,103,111,119,127,135,143,151,159,167,175,
  91. 199,207,215,223,231,239,219,135,43,15,23,31,39,47,55,63,71,79,87,95,103,111,119,127,135,143,151,159,167,175,183,
  92. 207,215,223,231,239,219,135,43,15,23,31,39,47,55,63,71,79,87,95,103,111,119,127,135,143,151,159,167,175,183,191,
  93. 215,223,231,239,219,135,43,15,23,31,39,47,55,63,71,79,87,95,103,111,119,127,135,143,151,159,167,175,183,191,199,
  94. 223,231,239,219,135,43,15,23,31,39,47,55,63,71,79,87,95,103,111,119,127,135,143,151,159,167,175,183,191,199,207,
  95. 231,239,219,135,43,15,23,31,39,47,55,63,71,79,87,95,103,111,119,127,135,143,151,159,167,175,183,191,199,207,215,
  96. 239,219,135,43,15,23,31,39,47,55,63,71,79,87,95,103,111,119,127,135,143,151,159,167,175,183,191,199,207,215,223,
  97. 219,135,43,15,23,31,39,47,55,63,71,79,87,95,103,111,119,127,135,143,151,159,167,175,183,191,199,207,215,223,231,
  98. 135,43,15,23,31,39,47,55,63,71,79,87,95,103,111,119,127,135,143,151,159,167,175,183,191,199,207,215,223,231,239,};
  99. for(size_t i = 0; i != features.size(); ++i)
  100. {
  101. ASSERT_NEAR(features[i], expectResult[i], 0.0001);
  102. }
  103. }
  104. void CV_BlockMeanHashTest::testHashMode0()
  105. {
  106. bool const expectResult[] =
  107. {0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,
  108. 0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,
  109. 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,
  110. 0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,
  111. 0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,
  112. 0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,
  113. 0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
  114. 0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,
  115. 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,
  116. 1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,
  117. 1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,
  118. 1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,
  119. 1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,
  120. 1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,
  121. 1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,
  122. 1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,
  123. };
  124. for(int i = 0; i != hash.cols; ++i)
  125. {
  126. std::bitset<8> const bits = hash.at<uchar>(0, i);
  127. for(size_t j = 0; j != bits.size(); ++j)
  128. {
  129. EXPECT_EQ(expectResult[i*8+j], bits[j]);
  130. }
  131. }
  132. }
  133. void CV_BlockMeanHashTest::testHashMode1()
  134. {
  135. bool const expectResult[] =
  136. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  137. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
  138. 0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,
  139. 0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,
  140. 0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,
  141. 0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,
  142. 0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
  143. 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,
  144. 0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,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,1,1,1,1,1,
  160. 1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,
  161. 1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,
  162. 1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,
  163. 1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,
  164. 1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,
  165. 1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,
  166. 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  167. };
  168. for(int i = 0; i != hash.cols; ++i)
  169. {
  170. std::bitset<8> const bits = hash.at<uchar>(0, i);
  171. if(i != hash.cols-1)
  172. {
  173. for(size_t j = 0; j != bits.size(); ++j)
  174. {
  175. EXPECT_EQ(expectResult[i*8+j], bits[j]);
  176. }
  177. }
  178. else
  179. {
  180. //when mode == 1, there will be 961 block mean
  181. //that is why we only check one bit at here
  182. EXPECT_EQ(expectResult[i*8], bits[0]);
  183. }
  184. }
  185. }
  186. void CV_BlockMeanHashTest::run(int)
  187. {
  188. bmh->compute(input, hash);
  189. testMeanMode0();
  190. testHashMode0();
  191. bmh->setMode(BLOCK_MEAN_HASH_MODE_1);
  192. bmh->compute(input, hash);
  193. testMeanMode1();
  194. testHashMode1();
  195. }
  196. TEST(block_mean_hash_test, accuracy) { CV_BlockMeanHashTest test; test.safe_run(); }
  197. }} // namespace