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