-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdoc.go
More file actions
88 lines (62 loc) · 2.37 KB
/
doc.go
File metadata and controls
88 lines (62 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
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.
*/
package perceptive