The pHash algorithm for computing perceptual hashes of images is as follows:

  1. Convert the image to greyscale and scale to 32x32.
  2. Compute the discrete cosine transform of this scaled image.
  3. Discard all components of the DCT except the upper-left 8x8 portion.
  4. Find the median value of the reduced DCT.
  5. For each bit in the output hash, set it to 1 if the corresponding component of the reduced DCT is greater than the median, or 0 otherwise.

Why compare against the median when building the final hash value? Since the purpose of perceptual hashing is to compare multiple images for similarity, wouldn't it be more effective to compare against something that is independent of the image being processed, such as the value 0?


1 Answer 1


It has to do with the number of ones and zeros that the median induces.

By definition it will create a binary sequence of 32 ones and 32 zeros in most cases.

So now similarity is well defined and normalized.

This comes from:

Coskun, B. and Sankur, B.: "Robust video hash extraction." In Proceedings of the Signal Processing and Communications Applications Conference, pp. 292–295. IEEE, Apr. 2004.

where they say:

Finally these DCT coefficients were reduced to 1-bit by
thresholding with respect to their median value. Any coef-
ficient above the median value is declared as a 1 , and
any below as 0 , so that we are guaranteed to have 32 1 s
and 32 0 s. The one-bit quantization adds robustness to
the scheme, in turn for some loss in uniqueness of the
signature. On the other, equipartition of the 1 s and 0 s in
the signature brings in maximum randomness on the 64-
bit patterns and thus increases uniqueness.

  • $\begingroup$ After playing around with perceptual-hash functions for a bit, I'm not convinced this is actually a useful thing to do. Usually, when using perceptual hashes, you want collisions; maximizing uniqueness will increase the rate of false negatives when looking for near-duplicates. (Using 0 as the threshold has the specific benefit that you can compute the phash of the mirror of the image by a simple XOR operation.) $\endgroup$
    – Mark
    Oct 5, 2023 at 6:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.