Overview
imghash uses three distinct hash types to represent perceptual hashes. Each type is optimized for different algorithms and comparison methods. All types implement the commonHash interface.
Binary
Binary hashes represent image fingerprints where each element is a single bit. This is the most compact representation and is used by bit-based algorithms like Average, Difference, Median, and PHash.Type Definition
Creating Binary Hashes
The
NewBinary function automatically calculates the required number of bytes using (bits+7)/8 to ensure proper storage.Setting Bits
Binary hashes provide two methods for setting bits:- Set (LSB first)
- SetReverse (MSB first)
Methods
Returns a string representation formatted as a byte array.
Returns the number of bytes in the hash.
Returns the byte at the given index as a float64.
Checks if two binary hashes are identical.
Algorithms Using Binary
- Average - Compares pixels to mean value
- Difference - Compares adjacent pixels
- Median - Compares pixels to median value
- PHash - DCT-based perceptual hash
- MarrHildreth - Edge detection based
- BlockMean - Block-based averaging
- WHash - Wavelet-based hash
- PDQ - Facebook’s robust hashing algorithm
UInt8
UInt8 hashes represent image fingerprints where each element is an unsigned 8-bit integer (0-255). Used by histogram-based and quantized algorithms.Type Definition
Example
Methods
Returns a string representation as a uint8 slice.
Returns the number of uint8 elements in the hash.
Returns the element at the given index as float64.
Checks element-wise equality. Returns false if lengths differ.
Algorithms Using UInt8
- ColorMoment - Color distribution moments
- CLD (Color Layout Descriptor) - Spatial color distribution
- EHD (Edge Histogram Descriptor) - Edge distribution
- LBP (Local Binary Patterns) - Texture descriptor
- HOGHash - Histogram of Oriented Gradients
- BoVW - Bag of Visual Words with integer storage
Compatible Metrics
UInt8 hashes work with:L1, L2, Cosine, ChiSquare, Jaccard (MinHash mode)
Float64
Float64 hashes represent image fingerprints as floating-point vectors. These provide the highest precision and are used by advanced feature descriptors and moment-based algorithms.Type Definition
Example
Methods
Returns a string representation as a float64 slice.
Returns the number of float64 elements in the hash.
Returns the element at the given index.
Checks equality using epsilon-based comparison for floating-point tolerance.
Float64 uses
epsilon = math.Nextafter(1.0, 2.0) - 1.0 (machine epsilon) for equality comparison.Algorithms Using Float64
- RadialVariance - Radial projection features
- Zernike - Zernike moments
- GIST - Global scene descriptor
- RASH - Radon transform based
- BoVW - Bag of Visual Words with float storage
Compatible Metrics
Float64 hashes work with:L1, L2, Cosine, ChiSquare, PCC, Jaccard (MinHash mode)
Hash Interface
All three types implement the commonHash interface, allowing generic comparison functions:
Type Compatibility
Best Practices
Use Binary for Speed
Binary hashes are fastest for comparison and use minimal memory. Ideal for large-scale image matching.
Use UInt8 for Histograms
UInt8 is perfect for color and edge histograms where you need 256 bins of granularity.
Use Float64 for Precision
Float64 provides maximum accuracy for moment-based and feature descriptors requiring sub-integer precision.
Match Hash Types
Always ensure both hashes are the same type before comparison to avoid runtime errors.
Error Handling
Related
- Similarity Metrics - Learn how to compare different hash types
- Choosing an Algorithm - Choose the right algorithm and hash type
- Hash Types API - Complete hashtype package documentation