Overview
Block Mean Hash is a perceptual hashing algorithm based on the method described in “Block Mean Value Based Image Perceptual Hashing” by Yang et al. It divides the image into blocks, computes the mean intensity of each block, and thresholds against the global mean. The algorithm supports multiple methods including direct blocking, overlapping blocks, and rotation-aware variants.When to Use
Use Block Mean Hash when you need:- Block-based features for structural matching
- Rotation robustness (with rotation methods)
- Flexible block sizes for different use cases
- Overlapping blocks for smoother features
- High robustness to various transformations
Block Mean with rotation variants produces very large hashes (3,840 bits for default settings) but offers exceptional rotation robustness.
Constructor
Available Options
WithSize(width, height uint)- Sets the resize dimensionsWithBlockSize(width, height uint)- Sets the block dimensionsWithBlockMeanMethod(method BlockMeanMethod)- Sets the block construction methodWithInterpolation(interp Interpolation)- Sets the resize interpolation methodWithDistance(fn DistanceFunc)- Overrides the default Hamming distance function
Block Mean Methods
Direct- Non-overlapping blocks (default)Overlap- Overlapping blocks with 50% overlapRotation- Direct method with 24 rotated variants (15° steps)RotationOverlap- Overlap method with 24 rotated variants
Supported Interpolation Methods
NearestNeighborBilinearBilinearExact(default)BicubicMitchellNetravaliLanczos2Lanczos3
Usage Example
With Overlapping Blocks
With Rotation Support
With Custom Block Size
Default Settings
- Hash size: 256 bits (32 bytes) for Direct method
- Resize dimensions: 256×256 pixels
- Block size: 16×16 pixels
- Method: Direct (non-overlapping)
- Interpolation: BilinearExact
- Distance metric: Hamming distance
Hash Sizes by Method
| Method | Blocks | Hash Bits | Hash Bytes |
|---|---|---|---|
| Direct | 16×16 | 256 | 32 |
| Overlap | 31×31 | 961 | 121 |
| Rotation | 16×16×24 | 6,144 | 768 |
| RotationOverlap | 31×31×24 | 23,064 | 2,884 |
How It Works
Direct Method
- Resizes image to specified dimensions (default 256×256)
- Converts to grayscale
- Divides image into non-overlapping blocks (default 16×16 pixels)
- Computes mean intensity of each block
- Computes global mean of the image
- For each block:
- Sets bit to 1 if block mean ≥ global mean
- Sets bit to 0 if block mean < global mean
- Produces a binary hash (number of blocks bits)
Overlap Method
- Same as Direct, but blocks overlap by 50%
- Block stride = block_size / 2
- Creates 2×blocks-1 in each dimension
- Results in 4× more blocks (e.g., 31×31 instead of 16×16)
Rotation Methods
ForRotation or RotationOverlap:
- Performs the same process as Direct/Overlap
- Repeats for 24 rotated versions of the image (0°, 15°, 30°, …, 345°)
- Concatenates all rotation hashes
- Produces a hash 24× larger
Block Size Trade-offs
Comparison
Block Mean hashes are compared using Hamming distance:Similarity thresholds depend on the hash size. For 256-bit Direct method, use thresholds around 20-40. For rotation methods with larger hashes, proportionally increase thresholds.
Performance Characteristics
| Method | Speed | Memory | Robustness |
|---|---|---|---|
| Direct | Fast | Low | Good |
| Overlap | Fast | Low | Better |
| Rotation | Very Slow | High | Excellent |
| RotationOverlap | Very Slow | Very High | Excellent |
Use Case Recommendations
- Direct: General-purpose hashing, good speed/accuracy balance
- Overlap: Smoother features, better robustness to minor shifts
- Rotation: When rotation-invariance is critical
- RotationOverlap: Maximum robustness (rotation + smooth features)
References
- Block Mean Value Based Image Perceptual Hashing - Yang et al.
- Source:
blockmean.go:12-15