forked from PavelVGordeev/imagehash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwt.go
148 lines (136 loc) · 3.57 KB
/
dwt.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
package imagehash
import (
"sort"
)
// waveletCoefficients struct holds the coefficients used in wavelet transforms.
type waveletCoefficients struct {
LowPass []float64
HighPass []float64
}
// Haar wavelet coefficients are specified here.
// https://wavelets.pybytes.com/wavelet/haar/
// ....┌──┐
// ....│ │
// . ──┘ │ ┌──
// .......│ │
// .......└──┘
var haar = waveletCoefficients{
HighPass: []float64{0.5, -0.5},
LowPass: []float64{0.5, 0.5},
}
// DWT1d applies 1D Discrete Wavelet Transform on data using Haar wavelet coefficients.
func DWT1d(data []float64) {
temp := make([]float64, len(data))
half := len(data) / 2
for i := 0; i < half; i++ {
k := i * 2
temp[i] = haar.LowPass[0]*data[k] + haar.LowPass[1]*data[k+1]
temp[i+half] = haar.HighPass[0]*data[k] + haar.HighPass[1]*data[k+1]
}
copy(data, temp)
}
// DWT2d applies 2D Discrete Wavelet Transform on data at specified level.
func DWT2d(data [][]float64, level int) {
dims := len(data)
for k := 0; k < level; k++ {
curlvl := 1 << k
curdims := dims / curlvl
row := make([]float64, curdims)
for i := 0; i < curdims; i++ {
copy(row, data[i])
DWT1d(row)
copy(data[i], row)
}
col := make([]float64, curdims)
for j := 0; j < curdims; j++ {
for i := 0; i < curdims; i++ {
col[i] = data[i][j]
}
DWT1d(col)
for i := 0; i < curdims; i++ {
data[i][j] = col[i]
}
}
}
}
// iDWT1d applies 1D Inverse Discrete Wavelet Transform on data using Haar wavelet coefficients.
func iDWT1d(data []float64) {
temp := make([]float64, len(data))
half := len(data) / 2
for i := 0; i < half; i++ {
k := i * 2
temp[k] = (haar.LowPass[0]*data[i] + haar.HighPass[0]*data[i+half]) / haar.HighPass[0]
temp[k+1] = (haar.LowPass[1]*data[i] + haar.HighPass[1]*data[i+half]) / haar.LowPass[0]
}
copy(data, temp)
}
// IDWT2d applies 2D Inverse Discrete Wavelet Transform on data at specified level.
func IDWT2d(data [][]float64, level int) {
dims := len(data)
for k := level - 1; k >= 0; k-- {
curlvl := 1 << k
curdims := dims / curlvl
col := make([]float64, curdims)
for j := 0; j < curdims; j++ {
for i := 0; i < curdims; i++ {
col[i] = data[i][j]
}
iDWT1d(col)
for i := 0; i < curdims; i++ {
data[i][j] = col[i]
}
}
row := make([]float64, curdims)
for i := 0; i < curdims; i++ {
copy(row, data[i])
iDWT1d(row)
copy(data[i], row)
}
}
}
// floorp2 finds the largest power of 2 less than or equal to val.
func floorp2(val int) uint {
val |= val >> 1
val |= val >> 2
val |= val >> 4
val |= val >> 8
val |= val >> 16
return uint(val - (val >> 1))
}
// flatten transforms a 2D slice to a 1D slice.
func flatten(data [][]float64) []float64 {
flat := make([]float64, len(data)*len(data))
offset := 0
for _, row := range data {
copy(flat[offset:offset+len(row)], row)
offset += len(row)
}
return flat
}
// median finds the median value in a 2D slice.
func median(data []float64) float64 {
temp := make([]float64, len(data))
copy(temp, data)
sort.Float64s(temp)
if len(temp)%2 == 1 {
return temp[len(temp)/2]
} else {
return 0.5 * (temp[len(temp)/2-1] + temp[len(temp)/2])
}
}
// extractSquareRegion extracts a square region of the given width from the data.
func extractSquareRegion(data [][]float64, width uint) [][]float64 {
excerpt := make([][]float64, width)
for i := 0; i < int(width); i++ {
excerpt[i] = make([]float64, width)
copy(excerpt[i], data[i][:width])
}
return excerpt
}
// min returns the minimum of two comparable values.
func min[T int | uint | float64](x, y T) T {
if x < y {
return x
}
return y
}