-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdhash.go
41 lines (32 loc) · 838 Bytes
/
dhash.go
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
package imgsim
import (
"image"
"github.com/nfnt/resize"
)
// DifferenceHash calculates the average hash of an image. First the image is converted to grayscale
// Then it is resized to 9x8. Lastly the average hash is computed.
func DifferenceHash(img image.Image) Hash {
img = rgbaToGray(img)
img = resize.Resize(9, 8, img, resize.NearestNeighbor)
return calcDiffHash(img)
}
// calcDiff computes the average hash for the given image and mean.
func calcDiffHash(img image.Image) Hash {
var x, y int
var hash, p Hash
p = 1
var r, left uint32
rect := img.Bounds()
for y = rect.Min.Y; y < rect.Max.Y; y++ {
left, _, _, _ = img.At(rect.Min.X, y).RGBA()
for x = rect.Min.X + 1; x < rect.Max.X; x++ {
r, _, _, _ = img.At(x, y).RGBA()
if r > left {
hash |= p
}
p = p << 1
left = r
}
}
return hash
}