-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadaptive_threshold_test.mbt
More file actions
68 lines (64 loc) · 2.34 KB
/
Copy pathadaptive_threshold_test.mbt
File metadata and controls
68 lines (64 loc) · 2.34 KB
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
///|
/// Local adaptive threshold tests use small hand-computable luma patterns so
/// edge clipping and the alpha invariant remain observable.
///|
test "sauvola uses clipped windows and preserves alpha" {
let img = @pixelforge.Image::new(5, 1)
let values = [20, 20, 200, 200, 200]
for x in 0..<5 {
let v = @pixelforge.clamp_byte(values[x])
img.set_pixel(x, 0, v, v, v, if x == 0 { b'\x2A' } else { b'\xFF' })
}
// k=0 makes Sauvola equal the local mean, avoiding floating-point
// sensitivity while still exercising the radius-one edge windows.
let out = img.sauvola(1, 0.0, 128)
assert_eq(out.get_pixel(0, 0).0.to_int(), 0)
assert_eq(out.get_pixel(1, 0).0.to_int(), 0)
assert_eq(out.get_pixel(2, 0).0.to_int(), 255)
assert_eq(out.get_pixel(4, 0).0.to_int(), 0)
assert_eq(out.get_pixel(0, 0).3.to_int(), 42)
}
///|
test "sauvola keeps a flat field in one class" {
let img = @pixelforge.Image::new(3, 2)
for y in 0..<2 {
for x in 0..<3 {
img.set_pixel(x, y, b'\x80', b'\x80', b'\x80', b'\xA5')
}
}
// With k=0 the threshold is exactly the flat-field mean, so strict
// foreground comparison puts every equal pixel in the background class.
let out = img.sauvola(1, 0.0, 128)
for y in 0..<2 {
for x in 0..<3 {
assert_eq(out.get_pixel(x, y).0.to_int(), 0)
assert_eq(out.get_pixel(x, y).3.to_int(), 165)
}
}
}
///|
test "sauvola handles a single pixel and invalid parameters" {
let img = @pixelforge.Image::new(1, 1)
img.set_pixel(0, 0, b'\x64', b'\x64', b'\x64', b'\x7B')
// Negative radius, k above one, and zero dynamic range are normalized.
let out = img.sauvola(-3, 4.0, 0)
assert_eq(out.get_pixel(0, 0).0.to_int(), 255)
assert_eq(out.get_pixel(0, 0).1.to_int(), 255)
assert_eq(out.get_pixel(0, 0).2.to_int(), 255)
assert_eq(out.get_pixel(0, 0).3.to_int(), 123)
}
///|
test "adaptive mean threshold separates a local bright run" {
let img = @pixelforge.Image::new(5, 1)
let values = [20, 20, 200, 200, 200]
for x in 0..<5 {
let v = @pixelforge.clamp_byte(values[x])
img.set_pixel(x, 0, v, v, v, b'\xFF')
}
let out = img.adaptive_mean(1, 0)
assert_eq(out.get_pixel(0, 0).0.to_int(), 0)
assert_eq(out.get_pixel(1, 0).0.to_int(), 0)
assert_eq(out.get_pixel(2, 0).0.to_int(), 255)
assert_eq(out.get_pixel(3, 0).0.to_int(), 0)
assert_eq(out.get_pixel(4, 0).0.to_int(), 0)
}