-
Notifications
You must be signed in to change notification settings - Fork 3
/
colormoment.go
72 lines (66 loc) · 1.97 KB
/
colormoment.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
package imghash
import (
"image"
"github.com/ajdnik/imghash/hashtype"
"github.com/ajdnik/imghash/imgproc"
)
// ColorMoment is a perceptual hash that uses the method described in
// Perceptual Hashing for Color Images Using Invariant Moments; Tang et. al.
//
// See https://www.researchgate.net/publication/286870507_Perceptual_hashing_for_color_images_using_invariant_moments for more information.
type ColorMoment struct {
// Resized image width.
width uint
// Resized image height.
height uint
// Resize interpolation method.
interp imgproc.ResizeType
// Gaussian kernel size.
kernel int
// Gaussian kernel sigma.
sigma float64
}
// NewColorMoment creates a new ColorMoment struct using default values.
func NewColorMoment() ColorMoment {
return ColorMoment{
width: 512,
height: 512,
interp: imgproc.Bicubic,
kernel: 3,
sigma: 0,
}
}
// NewColorMomentWithParams creates a new ColorMoment struct based on supplied parameters.
func NewColorMomentWithParams(resizeWidth, resizeHeight uint, resizeType imgproc.ResizeType, kernelSize int, sigma float64) ColorMoment {
return ColorMoment{
width: resizeWidth,
height: resizeHeight,
interp: resizeType,
kernel: kernelSize,
sigma: sigma,
}
}
// Calculate returns a perceptual image hash.
func (ch *ColorMoment) Calculate(img image.Image) hashtype.Float64 {
r := imgproc.Resize(ch.width, ch.height, img, ch.interp)
b := imgproc.GaussianBlur(r, ch.kernel, ch.sigma)
yrb, _ := imgproc.YCrCb(b)
hsv, _ := imgproc.HSV(b)
yrbMom := imgproc.GetMoments(yrb)
// Switch R and B channels
yrbMom[0], yrbMom[2] = yrbMom[2], yrbMom[0]
hsvMom := imgproc.GetMoments(hsv)
// Switch R and B channels
hsvMom[0], hsvMom[2] = hsvMom[2], hsvMom[0]
yHuMom := imgproc.HuMoments(yrbMom)
hHuMom := imgproc.HuMoments(hsvMom)
hash := make(hashtype.Float64, len(hHuMom)+len(yHuMom))
var i int
for i = 0; i < len(hHuMom); i++ {
hash[i] = hHuMom[i]
}
for ; i < len(hHuMom)+len(yHuMom); i++ {
hash[i] = yHuMom[i-len(hHuMom)]
}
return hash
}