Comparer
TheComparer interface defines the contract for measuring similarity between two hashes using a distance metric appropriate for the algorithm that produced them.
Methods
Computes the distance between two hashes using an algorithm-appropriate metric.
The first hash to compare.
The second hash to compare.
The computed distance between the two hashes. Lower values indicate more similar images.
- For binary hashes: typically Hamming distance (number of differing bits)
- For numeric hashes: typically Euclidean (L2) distance
- Some algorithms use specialized metrics (e.g., chi-square, cosine distance)
Returns an error if the comparison fails. Common errors:
ErrIncompatibleHash- Hash types are incompatibleErrHashLengthMismatch- Hash lengths don’t match
HasherComparer
Most algorithms in imghash implement bothHasher and Comparer interfaces through the HasherComparer interface:
Usage Example
Algorithm-Specific Metrics
Each algorithm uses an appropriate distance metric for its hash type:Custom Distance Functions
You can also use the genericCompare function with custom distance metrics:
DistanceFunc Type
TheDistanceFunc type allows passing distance functions as parameters:
Error Handling
See Also
- Hasher Interface - For computing hashes
- Similarity Functions - Distance metric implementations
- Hash Types - Hash type definitions