-
Notifications
You must be signed in to change notification settings - Fork 1
/
twoConv.go
84 lines (78 loc) · 2.08 KB
/
twoConv.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
package main
func conv2d(imgA [][]int, kernel [][]float64) [][]int {
if len(kernel) > len(imgA) || len(kernel[0]) > len(imgA[0]) {
panic("Kernel should be smaller than image")
}
w := len(imgA) - len(kernel) + 1
h := len(imgA[0]) - len(kernel[0]) + 1
resultImg := make([][]int, w)
for i := 0; i < w; i++ {
resultImg[i] = make([]int, h)
}
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
resultImg[x][y] = innerProduct(imgA, kernel, point{x, y, 0})
}
}
return resultImg
}
func conv2d2(imgA [][]uint8, kernel [][]uint8) [][]uint8 {
if len(kernel) > len(imgA) || len(kernel[0]) > len(imgA[0]) {
panic("Kernel should be smaller than image")
}
w := len(imgA) - len(kernel) + 1
h := len(imgA[0]) - len(kernel[0]) + 1
resultImg := make([][]uint8, w)
for i := 0; i < w; i++ {
resultImg[i] = make([]uint8, h)
}
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
resultImg[x][y] = innerProductA(imgA, kernel, upoint{uint8(x), uint8(y), 0})
}
}
return resultImg
}
type upoint struct {
x uint8
y uint8
val uint8
}
func innerProductA(imgA [][]uint8, kernel [][]uint8, point upoint) uint8 {
result := uint8(0)
for x := uint8(0); x < uint8(len(kernel)); x++ {
if len(kernel) > len(imgA[point.x:]) {
panic("Out of bounds")
}
for y := uint8(0); y < uint8(len(kernel[x])); y++ {
if len(kernel[x]) > len(imgA[x][y:]) {
panic("Out of bounds")
}
result += uint8(imgA[point.x+x][point.y+y]) * kernel[x][y]
}
}
point.val = result
return result
}
func flip2d(s [][]int) [][]int {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
for b := range s {
for i, j := 0, len(s[b])-1; i < j; i, j = i+1, j-1 {
s[b][i], s[b][j] = s[b][j], s[b][i]
}
}
return s
}
func padImage(img [][]int) [][]int {
resultImg := make([][]int, len(img)+1)
resultImg[0] = make([]int, len(img[0])+2)
for i := 1; i < len(resultImg); i++ {
resultImg[i] = append(resultImg[i], 0)
resultImg[i] = append(resultImg[i], img[i-1]...)
resultImg[i] = append(resultImg[i], 0)
}
resultImg[len(resultImg)-1] = make([]int, len(img[0])+2)
return resultImg
}