test_radial_variance_hash.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. namespace opencv_test { namespace {
  6. using namespace cv::img_hash;
  7. /**
  8. *The expected results of this test case are come from the phash library,
  9. *I use it as golden model
  10. */
  11. class CV_RadialVarianceHashTest : public cvtest::BaseTest
  12. {
  13. public:
  14. CV_RadialVarianceHashTest();
  15. protected:
  16. void run(int /* idx */);
  17. //this test case do not use the original "golden data"
  18. //of pHash library, I add a small value to nb_pixels in
  19. //the function "ph_feature_vector" to avoid NaN value
  20. void testComputeHash();
  21. void testFeatures();
  22. //void testHash(); // TODO unused
  23. void testPixPerLine();
  24. void testProjection();
  25. cv::Mat input;
  26. Ptr<cv::img_hash::RadialVarianceHash> rvh;
  27. };
  28. CV_RadialVarianceHashTest::CV_RadialVarianceHashTest()
  29. {
  30. input.create(8, 8, CV_8U);
  31. uchar *inPtr = input.ptr<uchar>(0);
  32. for(size_t i = 0; i != input.total(); ++i)
  33. {
  34. inPtr[i] = static_cast<uchar>(i);
  35. }
  36. rvh = RadialVarianceHash::create(1, 10);
  37. }
  38. void CV_RadialVarianceHashTest::testComputeHash()
  39. {
  40. cv::Mat hashOne(1, 40, CV_8U);
  41. uchar buffer[] =
  42. {
  43. 52, 41, 49, 64, 40, 67, 76, 71, 69,
  44. 55, 58, 68, 72, 78, 63, 73, 66, 77,
  45. 60, 57, 48, 59, 62, 74, 70, 47, 46,
  46. 51, 45, 44, 42, 61, 54, 75, 50, 79,
  47. 65, 43, 53, 56
  48. };
  49. cv::Mat hashTwo(1, 40, CV_8U, buffer);
  50. for(uchar i = 0; i != 40; ++i)
  51. {
  52. hashOne.at<uchar>(0, i) = i;
  53. }
  54. double const actual = rvh->compare(hashOne, hashTwo);
  55. ASSERT_NEAR(0.481051, actual, 0.0001);
  56. }
  57. void CV_RadialVarianceHashTest::testFeatures()
  58. {
  59. std::vector<double> const &features = rvh->getFeatures();
  60. double const expectResult[] =
  61. {-1.35784,-0.42703,0.908487,-1.39327,1.17313,
  62. 1.47515,-0.0156121,0.774335,-0.116755,-1.02059};
  63. for(size_t i = 0; i != features.size(); ++i)
  64. {
  65. ASSERT_NEAR(features[i], expectResult[i], 0.0001);
  66. }
  67. }
  68. #if 0 // unused
  69. void CV_RadialVarianceHashTest::testHash()
  70. {
  71. cv::Mat const hash = rvh->getHash();
  72. uchar const expectResult[] =
  73. {
  74. 127, 92, 0, 158, 101,
  75. 88, 14, 136, 227, 160,
  76. 127, 94, 27, 118, 240,
  77. 166, 153, 96, 254, 162,
  78. 127, 162, 255, 96, 153,
  79. 166, 240, 118, 27, 94,
  80. 127, 160, 227, 136, 14,
  81. 88, 101, 158, 0, 92
  82. };
  83. for(int i = 0; i != hash.cols; ++i)
  84. {
  85. EXPECT_EQ(hash.at<uchar>(0, i), expectResult[i]);
  86. }
  87. }
  88. #endif
  89. void CV_RadialVarianceHashTest::testPixPerLine()
  90. {
  91. cv::Mat const pixPerLine = rvh->getPixPerLine(input);
  92. uchar const expectResult[] =
  93. {
  94. 8,8,8,0,8,15,7,5,8,8,
  95. };
  96. bool const equal =
  97. std::equal(expectResult, expectResult + pixPerLine.total(),
  98. pixPerLine.ptr<int>(0));
  99. if(equal == false)
  100. {
  101. ts->printf(cvtest::TS::LOG, "Wrong pixel per line value \n");
  102. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  103. }
  104. }
  105. void CV_RadialVarianceHashTest::testProjection()
  106. {
  107. cv::Mat const proj = rvh->getProjection();
  108. uchar const expectResult[] =
  109. {
  110. 32, 33, 34, 35, 36, 37, 38, 39,
  111. 16, 17, 18, 27, 36, 37, 46, 47,
  112. 0, 9, 18, 19, 36, 45, 46, 55,
  113. 0, 0, 0, 0, 0, 0, 0, 0,
  114. 2, 10, 18, 27, 36, 44, 53, 61,
  115. 4, 59, 51, 44, 36, 29, 22, 14,
  116. 0, 58, 51, 43, 36, 30, 22, 15,
  117. 0, 0, 58, 43, 36, 21, 6, 0,
  118. 56, 49, 42, 43, 36, 21, 22, 15,
  119. 40, 41, 42, 35, 36, 29, 22, 23
  120. };
  121. bool const equal =
  122. std::equal(expectResult, expectResult + proj.total(),
  123. proj.ptr<uchar>(0));
  124. if(equal == false)
  125. {
  126. ts->printf(cvtest::TS::LOG, "Wrong projection value \n");
  127. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  128. }
  129. }
  130. void CV_RadialVarianceHashTest::run(int)
  131. {
  132. testPixPerLine();
  133. testProjection();
  134. testFeatures();
  135. testComputeHash();
  136. }
  137. TEST(radial_variance_hash_test, accuracy) { CV_RadialVarianceHashTest test; test.safe_run(); }
  138. }} // namespace