Overview
PHash (Perceptual Hash) is a DCT-based perceptual hashing algorithm described in “Implementation and Benchmarking of Perceptual Image Hash Functions” by Zauner et al. It produces a 64-bit hash by analyzing low-frequency DCT coefficients. This algorithm is more robust than simple hashing methods and supports weighted Hamming distance for improved accuracy.When to Use
Use PHash when you need:- DCT-based robustness to minor transformations
- 64-bit compact hash for efficient storage
- Weighted distance metrics for better discrimination
- Research-backed algorithm with benchmarking results
- Balance between robustness and hash size
PHash is widely used in academic research and commercial applications for perceptual image matching.
Constructor
Available Options
WithSize(width, height uint)- Sets the resize dimensions (DCT input size)WithInterpolation(interp Interpolation)- Sets the resize interpolation methodWithWeights(weights []float64)- Sets per-byte weights for weighted Hamming distanceWithDistance(fn DistanceFunc)- Overrides the default weighted Hamming distance function
Supported Interpolation Methods
NearestNeighborBilinearBilinearExact(default)BicubicMitchellNetravaliLanczos2Lanczos3
Usage Example
With Custom Options
With Weighted Distance
Default Settings
- Hash size: 64 bits (8 bytes)
- Resize dimensions: 32×32 pixels
- Interpolation: BilinearExact
- DCT block: Top-left 8×8 coefficients (excluding DC)
- Distance metric: Weighted Hamming distance
- Default weights: All 1.0 (uniform weighting)
How It Works
The PHash algorithm:- Resizes the image to 32×32 pixels
- Converts to grayscale
- Computes 2D Discrete Cosine Transform (DCT)
- Extracts top-left 8×8 DCT coefficients
- Removes the DC component (strongest frequency)
- Computes the mean of remaining coefficients
- Thresholds coefficients against the mean:
- Sets bit to 1 if coefficient > mean
- Sets bit to 0 if coefficient ≤ mean
- Produces a 64-bit binary hash
Weighted Hamming Distance
PHash supports weighted Hamming distance, where each byte can have a different weight:Comparison
PHash uses weighted Hamming distance by default:Performance Characteristics
- Speed: Moderate (DCT computation required)
- Memory: Low (64-bit hash)
- Robustness: High for minor edits, compression
- Rotation: Not rotation-invariant
- Scaling: Robust to scaling
PHash vs. Other Algorithms
| Algorithm | Hash Size | Speed | Robustness |
|---|---|---|---|
| PHash | 64 bits | Medium | High |
| Average Hash | 64 bits | Very Fast | Low |
| PDQ | 256 bits | Medium | Very High |
| Block Mean | Variable | Slow | Very High |
References
- Implementation and Benchmarking of Perceptual Image Hash Functions - Zauner et al.
- Source:
phash.go:15-18