-
Notifications
You must be signed in to change notification settings - Fork 0
/
phash.go
66 lines (60 loc) · 1.29 KB
/
phash.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
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
package main
import (
"os"
"github.com/nfnt/resize"
"image/png"
)
func obtainDCT(filename string) [][]float64 {
file, _ := os.Open(filename)
img, _ := png.Decode(file)
defer file.Close()
resized := resize.Resize(32, 32, img, resize.NearestNeighbor)
imgArr := make([][]float64, 32)
for x := 0; x < 32; x ++ {
for y := 0; y < 32; y ++ {
pixel := resized.At(x, y)
r, g, b, _ := pixel.RGBA()
lum := 0.299*float64(r) + 0.587*float64(g) + 0.114*float64(b)
imgArr[x] = append(imgArr[x], float64((lum / 256) - 128))
}
}
dctArr := dctEquation(imgArr)
return dctArr
}
func phash(dct [][]float64) string {
var runningSum float64
runningSum = 0
for x := 0; x < len(dct); x ++ {
for y := 0; y < len(dct[0]); y ++ {
if x == 0 && y == 0 {
continue
} else {
runningSum += dct[x][y]
}
}
}
avgDCT := float64(runningSum) / float64(((len(dct) * len(dct)) - 1))
output := ""
for x := 0; x < len(dct); x ++ {
for y := 0; y < len(dct[0]); y ++ {
if x == 0 && y == 0 {
continue
}
if dct[x][y] < avgDCT {
output = output + "0"
} else {
output = output + "1"
}
}
}
return output
}
func hammingDistance(val1 string, val2 string) int {
distance := 0
for x := 0; x < len(val1); x ++ {
if (val1[x] != val2[x]) {
distance ++
}
}
return distance
}