-
Notifications
You must be signed in to change notification settings - Fork 2
/
perceptual-hash.go
165 lines (139 loc) · 4.26 KB
/
perceptual-hash.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package perceptualhash
import (
"image/jpeg"
"image"
"image/png"
"os"
"image/color"
"math"
"github.com/nfnt/resize"
"strconv"
"strings"
"path"
)
func decodeImage(img *os.File, imageExt string) (image.Image, error) {
var decodedImage image.Image
var err error
switch imageExt {
case ".jpg", ".JPG", ".JPEG", ".jpeg":
decodedImage, err = jpeg.Decode(img)
case ".png", ".PNG":
decodedImage, err = png.Decode(img)
}
if err != nil {
return nil, err
}
return decodedImage, nil
}
func convertToGrayscale(img image.Image) *image.Gray16 {
imageX, imageY := img.Bounds().Max.X, img.Bounds().Max.Y
grayImage := image.NewGray16(image.Rectangle{image.Point{0, 0}, image.Point{imageX, imageY}})
for i := 0; i < imageX; i++ {
for j := 0; j < imageY; j++ {
r, g, b, _ := img.At(i, j).RGBA()
pixelAvg := math.Ceil((float64(r) + float64(g) + float64(b)) / 3.0)
grayColor := color.Gray16{uint16(pixelAvg)}
grayImage.SetGray16(i, j, grayColor)
}
}
return grayImage
}
func downsizeImage(img image.Image, hashLength int) image.Image {
widthAndLength := uint(math.Ceil(math.Sqrt(float64(hashLength)/2.0)) + 1)
return resize.Resize(widthAndLength, widthAndLength, img, resize.Bicubic)
}
func calculateRowHash(img image.Image) string {
imageX, imageY := img.Bounds().Max.X, img.Bounds().Max.Y
completeHash := [][]string{}
for j := 0; j < imageY-1; j++ {
rowHash := []string{}
for i := 0; i < imageX-1; i++ {
r0, _, _, _ := img.At(i, j).RGBA()
r1, _, _, _ := img.At(i+1, j).RGBA()
if r1 >= r0 {
rowHash = append(rowHash, strconv.Itoa(1))
}
if r1 < r0 {
rowHash = append(rowHash, strconv.Itoa(0))
}
}
completeHash = append(completeHash, rowHash)
}
completeHashString := []string{}
for i := 0; i < len(completeHash); i++ {
rowHashString := strings.Join(completeHash[i], "")
completeHashString = append(completeHashString, rowHashString)
}
return strings.Join(completeHashString, "")
}
func calculateColumnHash(img image.Image) string {
imageX, imageY := img.Bounds().Max.X, img.Bounds().Max.Y
completeHash := [][]string{}
for i := 0; i < imageX-1; i++ {
columnHash := []string{}
for j := 0; j < imageY-1; j++ {
r0, _, _, _ := img.At(i, j).RGBA()
r1, _, _, _ := img.At(i, j+1).RGBA()
if r1 >= r0 {
columnHash = append(columnHash, strconv.Itoa(1))
}
if r1 < r0 {
columnHash = append(columnHash, strconv.Itoa(0))
}
}
completeHash = append(completeHash, columnHash)
}
completeHashString := []string{}
for i := 0; i < len(completeHash); i++ {
columnHashString := strings.Join(completeHash[i], "")
completeHashString = append(completeHashString, columnHashString)
}
return strings.Join(completeHashString, "")
}
func CalculateHash(img image.Image) string {
rowHash := calculateRowHash(img)
columnHash := calculateColumnHash(img)
hash := ""
hash += rowHash
hash += columnHash
return hash
}
func diffHashes(hash1, hash2 string, hashLength float32) float32 {
var similarityInTwoHashes float32
for i := 0; i < len(hash1); i++ {
if (byte(hash1[i]) ^ byte(hash2[i])) == 0 {
similarityInTwoHashes++
}
}
return (similarityInTwoHashes / hashLength) * 100
}
func loadFiles(firstImagePath, secondImagePath string) (*os.File, *os.File, error) {
firstImage, err := os.Open(firstImagePath)
if err != nil {
return nil, nil, err
}
secondImage, err := os.Open(secondImagePath)
if err != nil {
return nil, nil, err
}
return firstImage, secondImage, nil
}
func CalculateSimilarity(firstImagePath, secondImagePath string, hashLength int) (float32, error) {
firstImage, secondImage, err := loadFiles(firstImagePath, secondImagePath)
if err != nil {
return 0.0, err
}
defer firstImage.Close()
defer secondImage.Close()
firstDecodedImage, err := decodeImage(firstImage, path.Ext(firstImagePath))
if err != nil {
return 0.0, err
}
secondDecodedImage, err := decodeImage(secondImage, path.Ext(secondImagePath))
if err != nil {
return 0.0, err
}
firstGrayImage, secondGrayImage := convertToGrayscale(firstDecodedImage), convertToGrayscale(secondDecodedImage)
firstDownsizedImage, secondDownsizedImage := downsizeImage(firstGrayImage, hashLength), downsizeImage(secondGrayImage, hashLength)
return diffHashes(CalculateHash(firstDownsizedImage), CalculateHash(secondDownsizedImage), float32(hashLength)), nil
}