RightSignal
1 of 4

Your RightSignal

Concept of the day1 / 3

Perceptual hashing

The concept

How a resized, recompressed photo still matches the original

A normal hash like SHA-256 turns a file into a short code. Change one pixel and the code changes completely. A perceptual hash does the opposite: images that look alike get codes that are almost the same.

A simple version, the difference hash (dHash), shrinks a photo to 9×8 grey pixels and checks whether each one is brighter than its right-hand neighbour. That gives a 64-bit code, and similar images differ in only a few bits.

What it’s forDecides whether two images are the same picture.

match if popcount(h₁ ⊕ h₂) ≤ t

h₁, h₂
the two 64-bit perceptual hashes being compared
⊕
XOR: gives a 1 wherever the two hashes differ
popcount
counts the 1 bits, giving the Hamming distance
t
how many differing bits you allow, e.g. 5 to 10 out of 64

What it givesYes or no: are these probably the same picture?

  1. h₁ = 10110010, h₂ = 10100011
  2. h₁ ⊕ h₂ = 00010001, so 2 bits differ
  3. 2 ≤ t = 5, so the images match