test_average_hash.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. class CV_AverageHashTest : public cvtest::BaseTest
  8. {
  9. public:
  10. CV_AverageHashTest();
  11. ~CV_AverageHashTest();
  12. protected:
  13. void run(int /* idx */);
  14. };
  15. CV_AverageHashTest::CV_AverageHashTest(){}
  16. CV_AverageHashTest::~CV_AverageHashTest(){}
  17. void CV_AverageHashTest::run(int )
  18. {
  19. cv::Mat const input = (cv::Mat_<uchar>(8, 8) <<
  20. 1, 5, 4, 6, 3, 2, 7, 8,
  21. 2, 4, 8, 9, 2, 1, 4, 3,
  22. 3, 4, 5, 7, 9, 8, 7, 6,
  23. 1, 2, 3, 4, 5, 6, 7, 8,
  24. 8, 7, 2, 3, 6, 4, 5, 1,
  25. 3, 4, 1, 2, 9, 8, 4, 2,
  26. 6, 7, 8, 9, 7, 4, 3, 2,
  27. 8, 7, 6, 5, 4, 3, 2, 1);
  28. cv::Mat hash;
  29. cv::img_hash::averageHash(input, hash);
  30. bool const expectResult[] =
  31. {
  32. 0,0,0,1,0,0,1,1,
  33. 0,0,1,1,0,0,0,0,
  34. 0,0,0,1,1,1,1,1,
  35. 0,0,0,0,0,1,1,1,
  36. 1,1,0,0,1,0,0,0,
  37. 0,0,0,0,1,1,0,0,
  38. 1,1,1,1,1,0,0,0,
  39. 1,1,1,0,0,0,0,0
  40. };
  41. uchar const *hashPtr = hash.ptr<uchar>(0);
  42. for(int i = 0; i != hash.cols; ++i)
  43. {
  44. std::bitset<8> const bits = hashPtr[i];
  45. for(int j = 0; j != 8; ++j)
  46. {
  47. EXPECT_EQ(bits[j], expectResult[i*8+j]);
  48. }
  49. }
  50. }
  51. TEST(average_hash_test, accuracy) { CV_AverageHashTest test; test.safe_run(); }
  52. }} // namespace