Overview
Median Hash is a variant of Average Hash that uses the median pixel value instead of the mean for thresholding. This makes it more robust to outliers and extreme pixel values. By using the median, the algorithm is less sensitive to bright spots, dark shadows, or other extreme values that might skew the mean.When to Use
Use Median Hash when you need:- Robustness to outliers and extreme pixel values
- Better handling of images with bright spots or dark shadows
- Simple, fast computation like Average Hash
- Resistance to noise in certain scenarios
- Median-based thresholding instead of mean-based
Median Hash is particularly useful for images with uneven lighting, lens flare, or other artifacts that create extreme pixel values.
Constructor
Available Options
WithSize(width, height uint)- Sets the resize dimensionsWithInterpolation(interp Interpolation)- Sets the resize interpolation methodWithDistance(fn DistanceFunc)- Overrides the default Hamming distance function
Supported Interpolation Methods
NearestNeighborBilinear(default)BicubicMitchellNetravaliLanczos2Lanczos3BilinearExact
Usage Example
With Custom Options
Default Settings
- Hash size: 64 bits (8 bytes)
- Resize dimensions: 8×8 pixels
- Interpolation: Bilinear
- Distance metric: Hamming distance
How It Works
The Median Hash algorithm:- Resizes the image to the specified dimensions (default 8×8)
- Converts to grayscale
- Computes the median pixel value (not mean)
- For each pixel:
- Sets bit to 1 if pixel value > median
- Sets bit to 0 if pixel value ≤ median
- Produces a binary hash (width × height bits)
Comparison
Median hashes are compared using Hamming distance. Lower distances indicate more similar images.Median vs. Mean
| Aspect | Median Hash | Average Hash |
|---|---|---|
| Threshold | Median pixel value | Mean pixel value |
| Outlier sensitivity | Low (robust) | High (sensitive) |
| Computation | Requires sorting | Simple sum/count |
| Best for | Images with extreme values | Clean, well-lit images |
| Speed | Slightly slower | Fastest |
Performance
Median Hash is slightly slower than Average Hash due to median calculation:- Simple resize operation
- Median computation requires sorting
- Direct pixel comparison
- No complex transforms
References
- DupImageLib implementation
- Source:
medianhash.go:12-14