test_phash.cpp 1.31 KB
Newer Older
1 2 3 4 5 6 7 8
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.

#include "test_precomp.hpp"

#include <bitset>

9
namespace opencv_test { namespace {
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

class CV_PHashTest : public cvtest::BaseTest
{
public:
    CV_PHashTest();
    ~CV_PHashTest();
protected:
    void run(int /* idx */);
};

CV_PHashTest::CV_PHashTest(){}
CV_PHashTest::~CV_PHashTest(){}

void CV_PHashTest::run(int )
{
    cv::Mat input(32, 32, CV_8U);
    cv::Mat hash;

    uchar value = 0;
    uchar *inPtr = input.ptr<uchar>(0);
    for(size_t i = 0; i != 32*32; ++i)
    {
        inPtr[i] = value++;
    }

    cv::img_hash::pHash(input, hash);
    bool const expectResult[] =
    {
        1,0,1,1,1,1,1,1,
        0,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,
        0,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,
        0,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,
        0,1,1,1,1,1,1,1,
    };
    uchar const *hashPtr = hash.ptr<uchar>(0);
    for(int i = 0; i != hash.cols; ++i)
    {
        std::bitset<8> const bits = hashPtr[i];
        for(int j = 0; j != 8; ++j)
        {
            EXPECT_EQ(bits[j], expectResult[i*8+j]);
        }
    }
}

TEST(average_phash_test, accuracy) { CV_PHashTest test; test.safe_run(); }
59 60

}} // namespace