-
Notifications
You must be signed in to change notification settings - Fork 0
/
convolution64.go
225 lines (197 loc) · 5.02 KB
/
convolution64.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package dip
import (
"errors"
"math"
"sync"
)
// A 2n+1 x 2n+1 kernel for convolution/filtering.
type Kernel64 interface {
// The weights of the kernel in row major order.
Kernel() []float64
}
var (
ErrKernelNotSquare = errors.New("kernel is not square")
ErrKernelNotOdd = errors.New("kernel size is not odd")
)
type fullKernel64 []float64
// Creates a new kernel from weights in row major order.
func NewKernel64(weights []float64) (Kernel64, error) {
size := int(math.Sqrt(float64(len(weights))))
if size*size != len(weights) {
return nil, ErrKernelNotSquare
}
if size%2 == 0 {
return nil, ErrKernelNotOdd
}
return fullKernel64(weights), nil
}
func (k fullKernel64) Kernel() []float64 {
return k
}
type separableKernel64 struct {
x, y []float64
}
// Creates a new kernel from horizontal and vertical weights.
func NewSeparableKernel64(x, y []float64) (Kernel64, error) {
if len(x) != len(y) {
return nil, ErrKernelNotSquare
}
if len(x)%2 == 0 {
return nil, ErrKernelNotOdd
}
return &separableKernel64{x, y}, nil
}
func (sk *separableKernel64) Kernel() []float64 {
size := len(sk.x)
weights := make([]float64, size*size)
for y := range sk.y {
r := y * size
for x := range sk.x {
weights[r+x] = sk.y[y] * sk.x[x]
}
}
return weights
}
func kernelSize(k Kernel64) int {
switch k.(type) {
case fullKernel64:
return int(math.Sqrt(float64(len(k.(fullKernel64))))) / 2
case *separableKernel64:
return len(k.(*separableKernel64).x) / 2
default:
return int(math.Sqrt(float64(len(k.Kernel())))) / 2
}
panic("unreachable")
}
// Returns a Image64 that has been convolved with the kernel.
func (p *Image64) Convolved(k Kernel64) *Image64 {
switch k.(type) {
case *separableKernel64:
return p.separableConvolution(k.(*separableKernel64))
default:
return p.fullConvolution(k)
}
panic("unreachable")
}
func (p *Image64) fullConvolution(k Kernel64) *Image64 {
result := NewImage64(p.Rect)
halfKernel := kernelSize(k)
kernelStride := halfKernel*2 + 1
weights := k.Kernel()
width := p.Rect.Dx()
height := p.Rect.Dy()
var wg sync.WaitGroup
// divide rows between go routines
wg.Add(goRoutines)
for t := 0; t < goRoutines; t++ {
i0, i1 := goRountineRanges(0, height, t)
go func(y0, y1 int) {
// for each pixel
for y := y0; y < y1; y++ {
resultIndex := y * result.Stride
for x := 0; x < width; x++ {
// convolve
v := float64(0)
for yk := -halfKernel; yk <= halfKernel; yk++ {
imageRowIndex := y + yk
if imageRowIndex < 0 {
imageRowIndex = 0
} else if imageRowIndex >= height {
imageRowIndex = height - 1
}
imageRowIndex *= p.Stride
kernelRowIndex := (yk + halfKernel) * kernelStride
for xk := -halfKernel; xk <= halfKernel; xk++ {
imageColumnIndex := x + xk
if imageColumnIndex < 0 {
imageColumnIndex = 0
} else if imageColumnIndex >= width {
imageColumnIndex = width - 1
}
iv := float64(p.Pix[imageRowIndex+imageColumnIndex])
kv := weights[kernelRowIndex+xk+halfKernel]
v += iv * kv
}
}
result.Pix[resultIndex] = Color64(v)
resultIndex++
}
}
wg.Done()
}(i0, i1)
}
wg.Wait()
return result
}
func (image *Image64) separableConvolution(k *separableKernel64) *Image64 {
result := NewImage64(image.Rect)
buffer := NewImage64(image.Rect)
halfKernel := kernelSize(k)
width := image.Rect.Dx()
height := image.Rect.Dy()
var wg sync.WaitGroup
// divide rows between go routines
wg.Add(goRoutines)
for t := 0; t < goRoutines; t++ {
i0, i1 := goRountineRanges(0, height, t)
go func(y0, y1 int) {
// for each pixel
for y := y0; y < y1; y++ {
imageRow := y * image.Stride
bufferIndex := y * buffer.Stride
for x := 0; x < width; x++ {
// horizontal convolution
v := float64(0)
for xk := -halfKernel; xk <= halfKernel; xk++ {
imageIndex := x + xk
if imageIndex < 0 {
imageIndex = 0
} else if imageIndex >= width {
imageIndex = width - 1
}
imageIndex += imageRow
iv := float64(image.Pix[imageIndex])
kv := k.x[xk+halfKernel]
v += iv * kv
}
buffer.Pix[bufferIndex] = Color64(v)
bufferIndex++
}
}
wg.Done()
}(i0, i1)
}
wg.Wait()
// divide rows between go routines
wg.Add(goRoutines)
for t := 0; t < goRoutines; t++ {
i0, i1 := goRountineRanges(0, height, t)
go func(y0, y1 int) {
// for each pixel
for y := y0; y < y1; y++ {
resultIndex := y * result.Stride
for x := 0; x < width; x++ {
// vertical convolution
v := float64(0)
for yk := -halfKernel; yk <= halfKernel; yk++ {
bufferIndex := y + yk
if bufferIndex < 0 {
bufferIndex = 0
} else if bufferIndex >= height {
bufferIndex = height - 1
}
bufferIndex = bufferIndex*buffer.Stride + x
iv := float64(buffer.Pix[bufferIndex])
kv := k.y[yk+halfKernel]
v += iv * kv
}
result.Pix[resultIndex] = Color64(v)
resultIndex++
}
}
wg.Done()
}(i0, i1)
}
wg.Wait()
return result
}