/
threshold.go
160 lines (132 loc) · 3.11 KB
/
threshold.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
package sudoku
import (
"image"
"sync"
)
type thresholdType int
const (
threshBinary thresholdType = iota
threshBinaryInv
)
func inRange(val, max int) int {
if val < 0 {
return 0
}
if val > max-1 {
return max - 1
}
return val
}
func meanHorizontal(src image.Gray, radius int) (dst image.Gray) {
var wg sync.WaitGroup
norm := float64(radius*2 + 1)
dst = *image.NewGray(src.Bounds())
width, height := src.Bounds().Max.X, src.Bounds().Max.Y
for y := 0; y < height; y++ {
wg.Add(1)
go func(y int) {
total := 0.0
for kx := -radius; kx <= radius; kx++ {
total += float64(src.Pix[src.PixOffset(inRange(kx, width), y)])
}
dst.Pix[dst.PixOffset(0, y)] = uint8(total / norm)
for x := 1; x < width; x++ {
total -= float64(src.Pix[src.PixOffset(inRange(x-radius-1, width), y)])
total += float64(src.Pix[src.PixOffset(inRange(x+radius, width), y)])
dst.Pix[dst.PixOffset(x, y)] = uint8(total / norm)
}
wg.Done()
}(y)
}
wg.Wait()
return
}
func meanVertical(src image.Gray, radius int) (dst image.Gray) {
var wg sync.WaitGroup
norm := float64(radius*2 + 1)
dst = *image.NewGray(src.Bounds())
width, height := src.Bounds().Max.X, src.Bounds().Max.Y
for x := 0; x < width; x++ {
wg.Add(1)
go func(x int) {
total := 0.0
for ky := -radius; ky <= radius; ky++ {
total += float64(src.Pix[src.PixOffset(x, inRange(ky, height))])
}
dst.Pix[dst.PixOffset(x, 0)] = uint8(total / norm)
for y := 1; y < height; y++ {
total -= float64(src.Pix[src.PixOffset(x, inRange(y-radius-1, height))])
total += float64(src.Pix[src.PixOffset(x, inRange(y+radius, height))])
dst.Pix[dst.PixOffset(x, y)] = uint8(total / norm)
}
wg.Done()
}(x)
}
wg.Wait()
return
}
func mean(src image.Gray, radius int) image.Gray {
return meanVertical(meanHorizontal(src, radius), radius)
}
func adaptiveThreshold(src image.Gray, maxValue uint8, threshold thresholdType, radius int, delta int) image.Gray {
dst := mean(src, radius)
var newVal uint8
for i, srcVal := range src.Pix {
newVal = 0
dstVal := int(dst.Pix[i]) - (delta)
if threshold == threshBinary {
if int(srcVal) > dstVal {
newVal = maxValue
}
} else if threshold == threshBinaryInv {
if int(srcVal) < dstVal {
newVal = maxValue
}
}
dst.Pix[i] = newVal
}
return dst
}
// Port from: https://en.wikipedia.org/wiki/Otsu%27s_method
func otsuValue(img image.Gray) uint8 {
size := float64(len(img.Pix))
histogram := make([]float64, 256, 256)
for _, pix := range img.Pix {
histogram[pix] += 1.0
}
sum := 0.0
for i, val := range histogram {
sum += float64(i) * val
}
sumB := 0.0
wB := 0.0
wF := 0.0
mB := 0.0
mF := 0.0
max := 0.0
between := 0.0
threshold1 := 0.0
threshold2 := 0.0
for i, val := range histogram {
wB += val
if wB == 0 {
continue
}
wF = size - wB
if wF == 0 {
break
}
sumB += float64(i) * val
mB = sumB / wB
mF = (sum - sumB) / wF
between = wB * wF * (mB - mF) * (mB - mF)
if between >= max {
threshold1 = float64(i)
if between > max {
threshold2 = float64(i)
}
max = between
}
}
return uint8((threshold1 + threshold2) / 2.0)
}