Ask Your Question

Phash Algorithm in img_hash

asked 2019-11-11 01:19:23 -0500

jlok gravatar image

updated 2019-11-11 01:19:49 -0500

I want to get the phash with opencv on Android then compare to a database of image phashes on a server.

  1. Is the phash algorithm in opencv img_hash the same as one from at all?

  2. I see img_hash.phash has compute function which just converts a mat to an output mat. How do I get the string representation of this output mat to then run against my phash database? This is what I have but I am getting some unrecognizable characters.


Mat outMat = new Mat();
phash.compute(inMat, outMat);
byte[] data = new byte[outMat.rows() * outMat.cols()];
outMat.get(0, 0, data);
String hashString = new String(data, StandardCharsets.UTF_8);
edit retag flag offensive close merge delete


it IS called a "bitstring", but no, it's not a java String at all.

berak gravatar imageberak ( 2019-11-11 01:43:57 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-11-11 01:42:27 -0500

berak gravatar image

updated 2019-11-11 02:01:19 -0500

  1. it's a clean-room rewrite, due to license issues.

  2. phash return 64 bits, that you have to compare to another phash value using e.g. the hamming norm.

while the original phash lib returns an int64(and you have to come up with your own comparison) , opencv returns a [1x8] CV_8U Mat (that's 64 bits, too !).

you're might use it like this:

Mat img1 = ...
Mat img2 = ...

Mat hash1 = new Mat();
phash.compute(img1, hash1);

Mat hash2 = new Mat();
phash.compute(img2, hash2);

double bitDist =, hash2);
// same as:
//double bitDist = Core.norm(hash1, hash2, Core.NORM_HAMMING);
edit flag offensive delete link more


Thanks for the info berak. I'm trying to save the phash as a string and compare it on my server using my own hamming comparison and not with opencv's compare function. My server will use the original phash. If I convert opencv's phash mat to int64, I assume it is not the same as the int64 result from the original phash. Is that correct? Maybe this is what you meant by clean-room but I am not really familiar with the term.

jlok gravatar imagejlok ( 2019-11-12 22:18:04 -0500 )edit

If I convert opencv's phash mat to int64, I assume it is not the same as the int64 result from the original phash.

i'd think so, too, unfortunately.

"clean room" means: rewrite from scratch.

berak gravatar imageberak ( 2019-11-13 01:04:45 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-11-11 01:19:23 -0500

Seen: 63 times

Last updated: Nov 11 '19