test_block_mean_hash.cpp 10 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
using namespace cv::img_hash;

/**
 *The expected results of this test case are come from the Phash library,
 *I use it as golden model
 */
class CV_BlockMeanHashTest : public cvtest::BaseTest
{
public:
    CV_BlockMeanHashTest();
protected:
    void run(int /* idx */);

    void testMeanMode0();
    void testMeanMode1();
    void testHashMode0();
    void testHashMode1();

    cv::Mat input;
    cv::Mat hash;
    Ptr<cv::img_hash::BlockMeanHash> bmh;
};

CV_BlockMeanHashTest::CV_BlockMeanHashTest()
{
    input.create(256, 256, CV_8U);
    for(int row = 0; row != input.rows; ++row)
    {
        uchar value = static_cast<uchar>(row);
        for(int col = 0; col != input.cols; ++col)
        {
            input.at<uchar>(row, col) = value++;
        }
    }
    bmh = BlockMeanHash::create(BLOCK_MEAN_HASH_MODE_0);
}

void CV_BlockMeanHashTest::testMeanMode0()
{
    std::vector<double> const &features = bmh->getMean();
    double const expectResult[] =
    {15,31,47,63,79,95,111,127,143,159,175,191,207,223,239,135,
     31,47,63,79,95,111,127,143,159,175,191,207,223,239,135,15,
     47,63,79,95,111,127,143,159,175,191,207,223,239,135,15,31,
     63,79,95,111,127,143,159,175,191,207,223,239,135,15,31,47,
     79,95,111,127,143,159,175,191,207,223,239,135,15,31,47,63,
     95,111,127,143,159,175,191,207,223,239,135,15,31,47,63,79,
     111,127,143,159,175,191,207,223,239,135,15,31,47,63,79,95,
     127,143,159,175,191,207,223,239,135,15,31,47,63,79,95,111,
     143,159,175,191,207,223,239,135,15,31,47,63,79,95,111,127,
     159,175,191,207,223,239,135,15,31,47,63,79,95,111,127,143,
     175,191,207,223,239,135,15,31,47,63,79,95,111,127,143,159,
     191,207,223,239,135,15,31,47,63,79,95,111,127,143,159,175,
     207,223,239,135,15,31,47,63,79,95,111,127,143,159,175,191,
     223,239,135,15,31,47,63,79,95,111,127,143,159,175,191,207,
     239,135,15,31,47,63,79,95,111,127,143,159,175,191,207,223,
     135,15,31,47,63,79,95,111,127,143,159,175,191,207,223,239,};
    for(size_t i = 0; i != features.size(); ++i)
    {
        ASSERT_NEAR(features[i], expectResult[i], 0.0001);
    }
}

void CV_BlockMeanHashTest::testMeanMode1()
{
    std::vector<double> const &features = bmh->getMean();
    double const expectResult[] =
    {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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,};
    for(size_t i = 0; i != features.size(); ++i)
    {
        ASSERT_NEAR(features[i], expectResult[i], 0.0001);
    }
}

void CV_BlockMeanHashTest::testHashMode0()
{
    bool const expectResult[] =
    {0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,
     0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,
     0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,
     0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,
     0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,
     0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,
     0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
     0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,
     1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,
     1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,
     1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,
     1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,
     1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,
     1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,
     1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,
     1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,
    };

    for(int i = 0; i != hash.cols; ++i)
    {
        std::bitset<8> const bits = hash.at<uchar>(0, i);
        for(size_t j = 0; j != bits.size(); ++j)
        {
            EXPECT_EQ(expectResult[i*8+j], bits[j]);
        }
    }
}

void CV_BlockMeanHashTest::testHashMode1()
{
    bool const expectResult[] =
    {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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,
     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,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,0,0,
     0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,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0,0,0,1,1,1,1,1,1,1,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,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,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,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,1,1,1,
    };

    for(int i = 0; i != hash.cols; ++i)
    {
        std::bitset<8> const bits = hash.at<uchar>(0, i);
        if(i != hash.cols-1)
        {
            for(size_t j = 0; j != bits.size(); ++j)
            {
                EXPECT_EQ(expectResult[i*8+j], bits[j]);
            }
        }
        else
        {
            //when mode == 1, there will be 961 block mean
            //that is why we only check one bit at here
            EXPECT_EQ(expectResult[i*8], bits[0]);
        }
    }
}

void CV_BlockMeanHashTest::run(int)
{
    bmh->compute(input, hash);
    testMeanMode0();
    testHashMode0();

    bmh->setMode(BLOCK_MEAN_HASH_MODE_1);
    bmh->compute(input, hash);
    testMeanMode1();
    testHashMode1();
}

TEST(block_mean_hash_test, accuracy) { CV_BlockMeanHashTest test; test.safe_run(); }
213 214

}} // namespace