Ask Your Question
0

How to obtain the hash representation from the image_hash module

asked 2018-02-09 17:06:48 -0600

Diegohi gravatar image

I 'm trying to compute the perceptual hash for an image (Using for example the phash algorithm). I want to compute and save the hash in a database for future comparissons.

The compute method from the PHashImpl class set an cv::OutputArray with the result of the computation (the hash). Is it posible to convert it into a alphanumeric of numeric representation of the hash?

Like: image -> phash_algrithm -> 8a0303f6df3ec8cd

Am i thinking in the right way or my limited knowledge at the moment of the OpenCv is leading me to the wrong path?

edit retag flag offensive close merge delete

Comments

I think that you will have to convert data using your own function

LBerger gravatar imageLBerger ( 2018-02-10 00:42:20 -0600 )edit

Thanks for your answer Laurent. Do you mean like for example looping through the pixel and transform the values to some number or string equivalent and then concatenate them or similar? I need to generate same alphanumeric size hashes in order to compare them using Hamming distance. I'm a bit lost in how to start from the Mat object, but reading a lot :)

Diegohi gravatar imageDiegohi ( 2018-02-10 15:00:05 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-02-11 08:24:18 -0600

LBerger gravatar image

You can do something like this to print hex value

   Mat img1 = imread("g:/lib/opencv/samples/data/rubberwhale2.png", IMREAD_COLOR);
    Mat img2 = imread("g:/lib/opencv/samples/data/rubberwhale1.png", IMREAD_COLOR);
    Mat d1,d2;

    Ptr<cv::img_hash::AverageHash> m= cv::img_hash::AverageHash::create();
    m->compute(img1, d1);
    m->compute(img2, d2);
    for (int i = 0; i<d1.cols; i++)
        cout << std::hex<<int(d1.at<uchar>(0, i)) << "\t";
    cout << "\n";
    for (int i = 0; i<d2.cols; i++)
        cout << std::hex<<int(d2.at<uchar>(0, i)) << "\t";
    cout << "\n";

and to get hamming norm :

    cout <<"Hamming norm "<< norm(d1, d2, NORM_HAMMING)<<"\n";
edit flag offensive delete link more

Comments

1

Thanks Laurent, that was what i was looking. Converting the values to Hex was the trick to obtain same size strings, I 'll try if i it differs a lot applying the hamming norm to the string hashes instead to the Mat objects, but should be the same distance in proportion i guess. Thanks a lot :)

Diegohi gravatar imageDiegohi ( 2018-02-13 13:54:03 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-02-09 17:06:48 -0600

Seen: 1,266 times

Last updated: Feb 11 '18