marr_hildreth_hash.cpp 5.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 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
// 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 "precomp.hpp"

using namespace cv;
using namespace cv::img_hash;
using namespace std;

namespace {

void getMHKernel(float alpha, float level, cv::Mat &kernel)
{
    int const sigma = static_cast<int>(4*std::pow(alpha,level));

    float const ratio = std::pow(alpha, -level);
    kernel.create(2*sigma+1, 2*sigma+1, CV_32F);
    for(int row = 0; row != kernel.rows; ++row)
    {
        float const ydiff = static_cast<float>(row - sigma);
        float const ypos = ratio * ydiff;
        float const yposPow2 = ypos * ypos;
        float *kPtr = kernel.ptr<float>(row);
        for(int col = 0; col != kernel.cols; ++col)
        {
            float const xpos = ratio * static_cast<float>((col - sigma));
            float const a = xpos * xpos + yposPow2;
            kPtr[col] = (2-a)*std::exp(a/2);
        }
    }
}

void fillBlocks(cv::Mat const &freImg, cv::Mat &blocks)
{
    //TODO : use forEach may provide better speed, however,
    //it is quite tedious to apply without lambda
    blocks.setTo(0);
    for(int row = 0; row != blocks.rows; ++row)
    {
        float *bptr = blocks.ptr<float>(row);
        int const rOffset = row*16;
        for(int col = 0; col != blocks.cols; ++col)
        {
            cv::Rect const roi(rOffset,col*16,16,16);
            bptr[col] =
                    static_cast<float>(cv::sum(freImg(roi))[0]);
        }
    }
}

void createHash(cv::Mat const &blocks, cv::Mat &hash)
{
    int hash_index = 0;
    int bit_index = 0;
    uchar hashbyte = 0;
    uchar *hashPtr = hash.ptr<uchar>(0);
    for (int row=0; row < 29; row += 4)
    {
        for (int col=0; col < 29; col += 4)
        {
            cv::Rect const roi(col,row,3,3);
            cv::Mat const blockROI = blocks(roi);
            float const avg =
                    static_cast<float>(cv::sum(blockROI)[0]/9.0);
            for(int i = 0; i != blockROI.rows; ++i)
            {
                float const *bptr = blockROI.ptr<float>(i);
                for(int j = 0; j != blockROI.cols; ++j)
                {
                    hashbyte <<= 1;
                    if (bptr[j] > avg)
                    {
                        hashbyte |= 0x01;
                    }
                    ++bit_index;
                    if ((bit_index%8) == 0)
                    {
                        hash_index = (bit_index/8) - 1;
                        hashPtr[hash_index] = hashbyte;
                        hashbyte = 0x00;
                    }
                }
            }
        }
    }
}

89
class MarrHildrethHashImpl CV_FINAL : public ImgHashBase::ImgHashImpl
90 91 92 93 94 95 96 97 98
{
public:

    MarrHildrethHashImpl(float alpha = 2.0f, float scale = 1.0f) : alphaVal(alpha), scaleVal(scale)
    {
        getMHKernel(alphaVal, scaleVal, mhKernel);
        blocks.create(31,31, CV_32F);
    }

99
    ~MarrHildrethHashImpl() CV_OVERRIDE { }
100

101
    virtual void compute(cv::InputArray inputArr, cv::OutputArray outputArr) CV_OVERRIDE
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
    {
        cv::Mat const input = inputArr.getMat();
        CV_Assert(input.type() == CV_8UC4 ||
                  input.type() == CV_8UC3 ||
                  input.type() == CV_8U);

        if(input.type() == CV_8UC3)
        {
            cv::cvtColor(input, grayImg, CV_BGR2GRAY);
        }
        else if(input.type() == CV_8UC4)
        {
            cv::cvtColor(input, grayImg, CV_BGRA2GRAY);
        }
        else
        {
            grayImg = input;
        }
        //pHash use Canny-deritch filter to blur the image
        cv::GaussianBlur(grayImg, blurImg, cv::Size(7, 7), 0);
        cv::resize(blurImg, resizeImg, cv::Size(512, 512), 0, 0, INTER_CUBIC);
        cv::equalizeHist(resizeImg, equalizeImg);

        //extract frequency info by mh kernel
        cv::filter2D(equalizeImg, freImg, CV_32F, mhKernel);
        fillBlocks(freImg, blocks);

        outputArr.create(1, 72, CV_8U);
        cv::Mat hash = outputArr.getMat();
        createHash(blocks, hash);
    }

134
    virtual double compare(cv::InputArray hashOne, cv::InputArray hashTwo) const CV_OVERRIDE
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
    {
        return norm(hashOne, hashTwo, NORM_HAMMING);
    }

    float getAlpha() const
    {
        return alphaVal;
    }

    float getScale() const
    {
        return scaleVal;
    }

    void setKernelParam(float alpha, float scale)
    {
        alphaVal = alpha;
        scaleVal = scale;
        getMHKernel(alphaVal, scaleVal, mhKernel);
    }

friend class MarrHildrethHash;

private:
    float alphaVal;
    cv::Mat blocks;
    cv::Mat blurImg;
    cv::Mat equalizeImg;
    cv::Mat freImg; //frequency response image
    cv::Mat grayImg;
    cv::Mat mhKernel;
    cv::Mat resizeImg;
    float scaleVal;
};

inline MarrHildrethHashImpl *getLocalImpl(ImgHashBase::ImgHashImpl *ptr)
{
    MarrHildrethHashImpl * impl = static_cast<MarrHildrethHashImpl*>(ptr);
    CV_Assert(impl);
    return impl;
}

}

//==================================================================================================

namespace cv { namespace img_hash {

float MarrHildrethHash::getAlpha() const
{
    return getLocalImpl(pImpl)->getAlpha();
}

float MarrHildrethHash::getScale() const
{
    return getLocalImpl(pImpl)->getScale();
}

void MarrHildrethHash::setKernelParam(float alpha, float scale)
{
    getLocalImpl(pImpl)->setKernelParam(alpha, scale);
}

Ptr<MarrHildrethHash> MarrHildrethHash::create(float alpha, float scale)
{
    Ptr<MarrHildrethHash> res(new MarrHildrethHash);
    res->pImpl = makePtr<MarrHildrethHashImpl>(alpha, scale);
    return res;
}

void marrHildrethHash(cv::InputArray inputArr,
                      cv::OutputArray outputArr,
                      float alpha, float scale)
{
    MarrHildrethHashImpl(alpha, scale).compute(inputArr, outputArr);
}

} } // cv::img_hash::