-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
hashcompute.go
197 lines (169 loc) · 5.64 KB
/
hashcompute.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2017 The goimagehash Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package goimagehash
import (
"errors"
"image"
"sync"
"github.com/corona10/goimagehash/etcs"
"github.com/corona10/goimagehash/transforms"
"github.com/nfnt/resize"
)
// AverageHash function returns a hash computation of average hash.
// Implementation follows
// http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
func AverageHash(img image.Image) (*ImageHash, error) {
if img == nil {
return nil, errors.New("image object can not be nil")
}
// Create 64bits hash.
ahash := NewImageHash(0, AHash)
resized := resize.Resize(8, 8, img, resize.Bilinear)
pixels := transforms.Rgb2Gray(resized)
flattens := transforms.FlattenPixels(pixels, 8, 8)
avg := etcs.MeanOfPixels(flattens)
for idx, p := range flattens {
if p > avg {
ahash.leftShiftSet(len(flattens) - idx - 1)
}
}
return ahash, nil
}
// DifferenceHash function returns a hash computation of difference hash.
// Implementation follows
// http://www.hackerfactor.com/blog/?/archives/529-Kind-of-Like-That.html
func DifferenceHash(img image.Image) (*ImageHash, error) {
if img == nil {
return nil, errors.New("image object can not be nil")
}
dhash := NewImageHash(0, DHash)
resized := resize.Resize(9, 8, img, resize.Bilinear)
pixels := transforms.Rgb2Gray(resized)
idx := 0
for i := 0; i < len(pixels); i++ {
for j := 0; j < len(pixels[i])-1; j++ {
if pixels[i][j] < pixels[i][j+1] {
dhash.leftShiftSet(64 - idx - 1)
}
idx++
}
}
return dhash, nil
}
// PerceptionHash function returns a hash computation of phash.
// Implementation follows
// http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
func PerceptionHash(img image.Image) (*ImageHash, error) {
if img == nil {
return nil, errors.New("image object can not be nil")
}
phash := NewImageHash(0, PHash)
resized := resize.Resize(64, 64, img, resize.Bilinear)
pixels := pixelPool64.Get().(*[]float64)
transforms.Rgb2GrayFast(resized, pixels)
flattens := transforms.DCT2DFast64(pixels)
pixelPool64.Put(pixels)
median := etcs.MedianOfPixelsFast64(flattens[:])
for idx, p := range flattens {
if p > median {
phash.leftShiftSet(64 - idx - 1) // leftShiftSet
}
}
return phash, nil
}
var pixelPool64 = sync.Pool{
New: func() interface{} {
p := make([]float64, 4096)
return &p
},
}
// ExtPerceptionHash function returns phash of which the size can be set larger than uint64
// Some variable name refer to https://github.com/JohannesBuchner/imagehash/blob/master/imagehash/__init__.py
// Support 64bits phash (width=8, height=8) and 256bits phash (width=16, height=16)
// Important: width * height should be the power of 2
func ExtPerceptionHash(img image.Image, width, height int) (*ExtImageHash, error) {
imgSize := width * height
if img == nil {
return nil, errors.New("image object can not be nil")
}
if imgSize <= 0 || imgSize&(imgSize-1) != 0 {
return nil, errors.New("width * height should be power of 2")
}
var phash []uint64
resized := resize.Resize(uint(imgSize), uint(imgSize), img, resize.Bilinear)
pixels := transforms.Rgb2Gray(resized)
dct := transforms.DCT2D(pixels, imgSize, imgSize)
flattens := transforms.FlattenPixels(dct, width, height)
median := etcs.MedianOfPixels(flattens)
lenOfUnit := 64
if imgSize%lenOfUnit == 0 {
phash = make([]uint64, imgSize/lenOfUnit)
} else {
phash = make([]uint64, imgSize/lenOfUnit+1)
}
for idx, p := range flattens {
indexOfArray := idx / lenOfUnit
indexOfBit := lenOfUnit - idx%lenOfUnit - 1
if p > median {
phash[indexOfArray] |= 1 << uint(indexOfBit)
}
}
return NewExtImageHash(phash, PHash, imgSize), nil
}
// ExtAverageHash function returns ahash of which the size can be set larger than uint64
// Support 64bits ahash (width=8, height=8) and 256bits ahash (width=16, height=16)
func ExtAverageHash(img image.Image, width, height int) (*ExtImageHash, error) {
if img == nil {
return nil, errors.New("image object can not be nil")
}
var ahash []uint64
imgSize := width * height
resized := resize.Resize(uint(width), uint(height), img, resize.Bilinear)
pixels := transforms.Rgb2Gray(resized)
flattens := transforms.FlattenPixels(pixels, width, height)
avg := etcs.MeanOfPixels(flattens)
lenOfUnit := 64
if imgSize%lenOfUnit == 0 {
ahash = make([]uint64, imgSize/lenOfUnit)
} else {
ahash = make([]uint64, imgSize/lenOfUnit+1)
}
for idx, p := range flattens {
indexOfArray := idx / lenOfUnit
indexOfBit := lenOfUnit - idx%lenOfUnit - 1
if p > avg {
ahash[indexOfArray] |= 1 << uint(indexOfBit)
}
}
return NewExtImageHash(ahash, AHash, imgSize), nil
}
// ExtDifferenceHash function returns dhash of which the size can be set larger than uint64
// Support 64bits dhash (width=8, height=8) and 256bits dhash (width=16, height=16)
func ExtDifferenceHash(img image.Image, width, height int) (*ExtImageHash, error) {
if img == nil {
return nil, errors.New("image object can not be nil")
}
var dhash []uint64
imgSize := width * height
resized := resize.Resize(uint(width)+1, uint(height), img, resize.Bilinear)
pixels := transforms.Rgb2Gray(resized)
lenOfUnit := 64
if imgSize%lenOfUnit == 0 {
dhash = make([]uint64, imgSize/lenOfUnit)
} else {
dhash = make([]uint64, imgSize/lenOfUnit+1)
}
idx := 0
for i := 0; i < len(pixels); i++ {
for j := 0; j < len(pixels[i])-1; j++ {
indexOfArray := idx / lenOfUnit
indexOfBit := lenOfUnit - idx%lenOfUnit - 1
if pixels[i][j] < pixels[i][j+1] {
dhash[indexOfArray] |= 1 << uint(indexOfBit)
}
idx++
}
}
return NewExtImageHash(dhash, DHash, imgSize), nil
}