forked from hybridgroup/gocv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg_hash.go
181 lines (154 loc) · 5.99 KB
/
img_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package contrib
//#include <stdlib.h>
//#include "img_hash.h"
import "C"
import (
"gocv.io/x/gocv"
)
// ImgHashBase defines the interface used for all of the img_hash algorithms.
type ImgHashBase interface {
Compare(a, b gocv.Mat) float64
Compute(inputArr gocv.Mat, outputArr *gocv.Mat)
}
// PHash is implementation of the PHash algorithm.
//
type PHash struct{}
// Compute computes hash of the input image using PHash.
//
// For further information, see:
// https://docs.opencv.org/master/de/d29/classcv_1_1img__hash_1_1ImgHashBase.html#ae2d9288db370089dfd8aab85d5e0b0f3
//
func (hash PHash) Compute(input gocv.Mat, output *gocv.Mat) {
C.pHashCompute(C.Mat(input.Ptr()), C.Mat(output.Ptr()))
}
// Compare compares the hash value between a and b using PHash.
//
// For further information, see:
// https://docs.opencv.org/master/de/d29/classcv_1_1img__hash_1_1ImgHashBase.html#a444a3e9ec792cf029385809393f84ad5
//
func (hash PHash) Compare(a, b gocv.Mat) float64 {
return float64(C.pHashCompare(C.Mat(a.Ptr()), C.Mat(b.Ptr())))
}
// AverageHash is implementation of the AverageHash algorithm.
//
type AverageHash struct{}
// Compute computes hash of the input image using AverageHash.
//
// For further information, see:
// https://docs.opencv.org/master/de/d29/classcv_1_1img__hash_1_1ImgHashBase.html#ae2d9288db370089dfd8aab85d5e0b0f3
//
func (hash AverageHash) Compute(input gocv.Mat, output *gocv.Mat) {
C.averageHashCompute(C.Mat(input.Ptr()), C.Mat(output.Ptr()))
}
// Compare compares the hash value between a and b using AverageHash.
//
// For further information, see:
// https://docs.opencv.org/master/de/d29/classcv_1_1img__hash_1_1ImgHashBase.html#a444a3e9ec792cf029385809393f84ad5
//
func (hash AverageHash) Compare(a, b gocv.Mat) float64 {
return float64(C.averageHashCompare(C.Mat(a.Ptr()), C.Mat(b.Ptr())))
}
// BlockMeanHash is implementation of the BlockMeanHash algorithm.
//
type BlockMeanHash struct {
Mode BlockMeanHashMode
}
type BlockMeanHashMode int
const (
BlockMeanHashMode0 BlockMeanHashMode = iota
BlockMeanHashMode1
BlockMeanHashModeDefault = BlockMeanHashMode0
)
// Compute computes hash of the input image using BlockMeanHash.
//
// For further information, see:
// https://docs.opencv.org/master/de/d29/classcv_1_1img__hash_1_1ImgHashBase.html#ae2d9288db370089dfd8aab85d5e0b0f3
//
func (hash BlockMeanHash) Compute(input gocv.Mat, output *gocv.Mat) {
C.blockMeanHashCompute(C.Mat(input.Ptr()), C.Mat(output.Ptr()), C.int(hash.Mode))
}
// Compare compares the hash value between a and b using BlockMeanHash.
//
// For further information, see:
// https://docs.opencv.org/master/de/d29/classcv_1_1img__hash_1_1ImgHashBase.html#a444a3e9ec792cf029385809393f84ad5
//
func (hash BlockMeanHash) Compare(a, b gocv.Mat) float64 {
return float64(C.blockMeanHashCompare(C.Mat(a.Ptr()), C.Mat(b.Ptr()), C.int(hash.Mode)))
}
// TODO: BlockMeanHash.GetMean isn't implemented, because it requires state from the last
// call to Compute, and there's no easy way to keep it.
// ColorMomentHash is implementation of the ColorMomentHash algorithm.
//
type ColorMomentHash struct{}
// Compute computes hash of the input image using ColorMomentHash.
//
// For further information, see:
// https://docs.opencv.org/master/de/d29/classcv_1_1img__hash_1_1ImgHashBase.html#ae2d9288db370089dfd8aab85d5e0b0f3
//
func (hash ColorMomentHash) Compute(input gocv.Mat, output *gocv.Mat) {
C.colorMomentHashCompute(C.Mat(input.Ptr()), C.Mat(output.Ptr()))
}
// Compare compares the hash value between a and b using ColorMomentHash.
//
// For further information, see:
// https://docs.opencv.org/master/de/d29/classcv_1_1img__hash_1_1ImgHashBase.html#a444a3e9ec792cf029385809393f84ad5
//
func (hash ColorMomentHash) Compare(a, b gocv.Mat) float64 {
return float64(C.colorMomentHashCompare(C.Mat(a.Ptr()), C.Mat(b.Ptr())))
}
// MarrHildrethHash is implementation of the MarrHildrethHash algorithm.
//
type MarrHildrethHash struct {
Alpha float32
Scale float32
}
func NewMarrHildrethHash() MarrHildrethHash {
return MarrHildrethHash{2.0, 1.0}
}
// Compute computes hash of the input image using MarrHildrethHash.
//
// For further information, see:
// https://docs.opencv.org/master/de/d29/classcv_1_1img__hash_1_1ImgHashBase.html#ae2d9288db370089dfd8aab85d5e0b0f3
//
func (hash MarrHildrethHash) Compute(input gocv.Mat, output *gocv.Mat) {
C.marrHildrethHashCompute(C.Mat(input.Ptr()), C.Mat(output.Ptr()),
C.float(hash.Alpha), C.float(hash.Scale))
}
// Compare compares the hash value between a and b using MarrHildrethHash.
//
// For further information, see:
// https://docs.opencv.org/master/de/d29/classcv_1_1img__hash_1_1ImgHashBase.html#a444a3e9ec792cf029385809393f84ad5
//
func (hash MarrHildrethHash) Compare(a, b gocv.Mat) float64 {
return float64(C.marrHildrethHashCompare(C.Mat(a.Ptr()), C.Mat(b.Ptr()),
C.float(hash.Alpha), C.float(hash.Scale)))
}
// RadialVarianceHash is implementation of the RadialVarianceHash algorithm.
//
type RadialVarianceHash struct {
Sigma float64
NumOfAngleLine int
}
func NewRadialVarianceHash() RadialVarianceHash {
return RadialVarianceHash{1, 180}
}
// Compute computes hash of the input image using RadialVarianceHash.
//
// For further information, see:
// https://docs.opencv.org/master/de/d29/classcv_1_1img__hash_1_1ImgHashBase.html#ae2d9288db370089dfd8aab85d5e0b0f3
//
func (hash RadialVarianceHash) Compute(input gocv.Mat, output *gocv.Mat) {
C.radialVarianceHashCompute(C.Mat(input.Ptr()), C.Mat(output.Ptr()),
C.double(hash.Sigma), C.int(hash.NumOfAngleLine))
}
// Compare compares the hash value between a and b using RadialVarianceHash.
//
// For further information, see:
// https://docs.opencv.org/master/de/d29/classcv_1_1img__hash_1_1ImgHashBase.html#a444a3e9ec792cf029385809393f84ad5
//
func (hash RadialVarianceHash) Compare(a, b gocv.Mat) float64 {
return float64(C.radialVarianceHashCompare(C.Mat(a.Ptr()), C.Mat(b.Ptr()),
C.double(hash.Sigma), C.int(hash.NumOfAngleLine)))
}
// TODO: RadialVariance getFeatures, getHash, getPixPerLine, getProjection are not
// implemented here, because they're stateful.