Documentation ¶
Overview ¶
Package perceptive implements perceptual hash algorithms for comparing images.
Perceptual hash algorithms are a family of comparable hash functions which generate distinct (but not unique) fingerprints, these fingerprints are then comparable.
Perceptual hash algorithms are mainly used for detecting duplicates of the same files, in a way that standard and cryptographic hashes generally fail.
The following perceptual hash algorithms are implemented:
- Average Hash (Ahash) - Fast but generates a huge number of false positives.
- Difference Hash (Dhash) - Fast and very few false positives.
Below are some examples on how to use the library:
package main import ( "log" "github.com/disintegration/imaging" "github.com/umahmood/perceptive" ) func openImage(filePath string) image.Image { img, err := imaging.Open(filePath) if err != nil { log.Fatalln(err) } return img } func main() { imgA := openImage("lena.jpg") imgB := openImage("lena.jpg") distance, err := perceptive.CompareImages(imgA, imgB, perceptive.Difference) if distance == 0 { // images are likely the same } else if distance >= 1 && distance <= 10 { // images are potentially a variation } else { // images are likely different } }
You can also use the perceptual hash algorithms directly, this is good if you want to store the hashes in a database or some look up table:
func main() { imgA := openImage("lena.jpg") imgB := openImage("lena.jpg") hash1, err := perceptive.Dhash(imgA) if err != nil { // handle error } hash2, err := perceptive.Dhash(imgB) if err != nil { // handle error } // hash1 and hash2 can be stored (in a database, etc...) for later use // hash1 and hash2 can be compared directly distance := perceptive.HammingDistance(hash1, hash2) ... }
When performing a Hamming distance on two hashes from Ahash or Dhash, the distance output has the following meaning:
- A distance of 0 means that the images are likely the same.
- A distance between 1-10 indicates the images are likely a variation of each other.
- A distance greater than 10 indicates the images are likely different.
Index ¶
Constants ¶
const ( Major = 1 Minor = 0 Patch = 1 )
Semantic versioning - http://semver.org/
Variables ¶
var ( ErrNilImage = errors.New("perceptive: nil image") ErrInvalidHash = errors.New("perceptive: invalid perceptual hash") )
Errors
Functions ¶
func CompareImages ¶
func CompareImages(img1, img2 image.Image, hash PerceptualHash) (int, error)
CompareImages compares two images using the given perceptual hash algorithm. The function returns a distance value indicating how similar the two images are. The distance value differs depending on the perceptual hash algorithm used. If perceptual hash type is Average or Difference:
- A distance of 0 indicates the same hash and likely a similar image.
- A distance between 1 and 10 the image is potentially a variation.
- A distance greater than 10 the image is likely a different image.
func HammingDistance ¶
HammingDistance computes the Hamming distance of two integers.
Types ¶
type PerceptualHash ¶
type PerceptualHash uint8
PerceptualHash perceptual hash algorithms
const ( Average PerceptualHash = iota // Average hash (Ahash) Difference // Difference hash (Dhash) )
Valid perceptual hashes