-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththreshold_test.go
125 lines (112 loc) · 2.89 KB
/
threshold_test.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
package pixl
import (
"image"
"image/color"
_ "image/jpeg"
"testing"
)
func TestThresholdStatic(t *testing.T) {
size := 10
image := image.NewNRGBA(image.Rectangle{Max: image.Point{X: size, Y: size}})
for i := 0; i < size; i++ {
for j := 0; j < size; j++ {
if j < 5 {
image.Set(i, j, color.Gray{Y: 200})
} else if j < 9 {
image.Set(i, j, color.Gray{Y: 50})
} else {
image.Set(i, j, color.Gray{Y: 160})
}
}
}
out := Threshold{Algorithm: ThresholdAlgorithms.Static, StaticLevel: 155}.Convert(image)
blackPixels := 0
for i := 0; i < size; i++ {
for j := 0; j < size; j++ {
if r, _, _, _ := out.At(i, j).RGBA(); (r >> 8) == 0xFF {
blackPixels++
}
}
}
if expected := 60; blackPixels != expected {
t.Errorf("Invalid number of black pixels in image, got: %d, want: %d.", blackPixels, expected)
}
}
func TestThresholdStaticInversion(t *testing.T) {
size := 16
image := image.NewNRGBA(image.Rectangle{Max: image.Point{X: size, Y: size}})
for i := 0; i < size; i++ {
counter := uint8(0)
for i := 0; i < size; i++ {
for j := 0; j < size; j++ {
image.Set(i, j, color.Gray{Y: counter})
counter++
}
}
}
out := Threshold{Algorithm: ThresholdAlgorithms.Static, StaticLevel: 99, InvertColors: true}.Convert(image)
blackPixels := 0
for i := 0; i < size; i++ {
for j := 0; j < size; j++ {
if r, _, _, _ := out.At(i, j).RGBA(); (r >> 8) == 0xFF {
blackPixels++
}
}
}
if expected := 100; blackPixels != expected {
t.Errorf("Invalid number of black pixels in image, got: %d, want: %d.", blackPixels, expected)
}
}
func TestThresholdStaticDefaultLevel(t *testing.T) {
size := 16
image := image.NewNRGBA(image.Rectangle{Max: image.Point{X: size, Y: size}})
counter := uint8(0)
for i := 0; i < size; i++ {
for j := 0; j < size; j++ {
image.Set(i, j, color.Gray{Y: counter})
counter++
}
}
out := Threshold{Algorithm: ThresholdAlgorithms.Static, InvertColors: false}.Convert(image)
blackPixels := 0
for i := 0; i < size; i++ {
for j := 0; j < size; j++ {
if r, _, _, _ := out.At(i, j).RGBA(); (r >> 8) == 0xFF {
blackPixels++
}
}
}
if expected := 128; blackPixels != expected {
t.Errorf("Invalid number of black pixels in image, got: %d, want: %d.", blackPixels, expected)
}
}
func BenchmarkThresholdStatic(b *testing.B) {
b.StopTimer()
input := generateImage()
b.StartTimer()
for n := 0; n < b.N; n++ {
Threshold{
Algorithm: ThresholdAlgorithms.Static,
}.Convert(input)
}
}
func BenchmarkThresholdOtsu(b *testing.B) {
b.StopTimer()
input := generateImage()
b.StartTimer()
for n := 0; n < b.N; n++ {
Threshold{
Algorithm: ThresholdAlgorithms.Otsu,
}.Convert(input)
}
}
func BenchmarkThresholdStaticWithInvert(b *testing.B) {
b.StopTimer()
input := generateImage()
b.StartTimer()
for n := 0; n < b.N; n++ {
Threshold{
Algorithm: ThresholdAlgorithms.Static, InvertColors: true,
}.Convert(input)
}
}