What Is Perceptual Hashing?
A perceptual hash is a compact digital fingerprint of an image based on its visual content. Unlike a cryptographic hash which changes entirely if even one pixel differs, a perceptual hash produces similar values for visually similar images. This makes it ideal for finding near-duplicate photos: frames from a burst sequence, slightly different exposures of the same scene, or resized versions of the same image.
How Perceptual Hashing Works
The most common perceptual hashing algorithms follow a similar pattern:
- Resize: Scale the image down to a very small size, typically 8x8 or 16x16 pixels, discarding high-frequency detail and keeping only fundamental visual structure.
- Convert to greyscale: Remove colour information so white balance shifts do not prevent matching visually identical compositions.
- Compute a hash: The average hash method compares each pixel to the mean brightness and encodes the result as a bitstring. More sophisticated algorithms use DCT (discrete cosine transform) to capture the most perceptually significant frequency components.
- Compare with Hamming distance: Two images are compared by counting bit positions where their hashes differ. Small distances indicate visually similar images.
Common Perceptual Hash Algorithms
Average Hash (aHash)
Fast and simple. Reduces to 8x8 greyscale, computes the mean, sets each bit based on whether the pixel is above or below mean. Produces a 64-bit hash. Works well for near-exact duplicates.
Difference Hash (dHash)
Computes the gradient between adjacent pixels rather than comparing to a mean. More robust to brightness differences. Preferred for burst detection where exposure may vary slightly between frames.
Perceptual Hash (pHash)
Uses DCT to capture the most significant low-frequency components. More computationally expensive but more robust to scaling, compression, and minor transformations. Most reliable for near-duplicate detection.
How imagic Uses Perceptual Hashing
imagic generates a perceptual hash for every image during the Analyse step. Images with similar hash values are clustered into groups presented as burst sequences in the Review step. The highest-quality image from each group is pre-selected based on quality scores for sharpness, exposure, noise, composition, and detail.
On a shoot with hundreds of burst sequences, this automation saves hours of comparison work. Install imagic with pip install imagic to see it in action.
Limitations
Perceptual hashing is not perfect. Very different exposures of similar scenes may produce different hashes despite compositional similarity. Images with overlapping but not identical framing may or may not be grouped depending on the algorithm and threshold. These edge cases require manual review but represent a small fraction of typical shoot volumes.