forked from Th1nkK1D/gocr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter.go
61 lines (45 loc) · 1020 Bytes
/
filter.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
// gOCR - Median Filter Module
package main
import (
"sort"
)
func getAbs(num int) int {
if num < 0 {
num = -num
}
return num
}
func getMedian(list []uint8) uint8 {
sort.Slice(list, func(i, j int) bool { return list[i] < list[j] })
return list[(len(list)-1)/2]
}
func getMirror(p, bound int) int {
if p < 0 {
p = getAbs(p + 1)
} else if p >= bound {
p = bound*2 - p - 1
}
return p
}
// MedianFilter - 3x3 median filter
func MedianFilter(imgArr [][][]uint8, rad int) [][][]uint8 {
height := len(imgArr)
width := len(imgArr[0])
// Init new array
newArr := make([][][]uint8, height)
for r := 0; r < height; r++ {
newArr[r] = make([][]uint8, width)
for c := 0; c < width; c++ {
newArr[r][c] = make([]uint8, 1)
list := make([]uint8, 0)
// Matrix loop
for j := -rad; j <= rad; j++ {
for i := -rad; i <= rad; i++ {
list = append(list, imgArr[getMirror(r+j, height)][getMirror(c+i, width)][0])
}
}
newArr[r][c][0] = getMedian(list)
}
}
return newArr
}