Skip to content

modules/img_hash/src/marr_hildreth_hash.cpp: error in kernel computation ? #3940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
orzel opened this issue May 21, 2025 · 2 comments
Open

Comments

@orzel
Copy link

orzel commented May 21, 2025

In modules/img_hash/src/marr_hildreth_hash.cpp:getMHKernel(), the line reads

kPtr[col] = (2-a)*std::exp(a/2);

While it seems it should be

kPtr[col] = (2-a)*std::exp(-a/2);

At least that's what the original code does: https://github.com/aetilius/pHash/blob/master/src/pHash.cpp#L610

CImg<float> *GetMHKernel(float alpha, float level) {
    int sigma = (int)4 * pow((float)alpha, (float)level);
    static CImg<float> *pkernel = NULL;
    float xpos, ypos, A;
    if (!pkernel) {
        pkernel = new CImg<float>(2 * sigma + 1, 2 * sigma + 1, 1, 1, 0);
        cimg_forXY(*pkernel, X, Y) {
            xpos = pow(alpha, -level) * (X - sigma);
            ypos = pow(alpha, -level) * (Y - sigma);
            A = xpos * xpos + ypos * ypos;
            pkernel->atXY(X, Y) = (2 - A) * exp(-A / 2);
        }
    }
    return pkernel;
}

And that also matches the formula given on wikipedia: https://en.wikipedia.org/wiki/Ricker_wavelet

@orzel
Copy link
Author

orzel commented May 21, 2025

Once fixed, that would really be useful to fix/redo the tests from https://qtandopencv.blogspot.com/2016/06/introduction-to-image-hash-module-of.html

@orzel
Copy link
Author

orzel commented May 22, 2025

There are other important problems with this code. For ex. it's not thread-safe. Or (related, but different) also all temporaries are class variables (blocks, blurImg, freImg, ...) for no good reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant