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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// OCR 专用二值化算法
// Sauvola 阈值、Niblack 阈值、OCR 自适应阈值
///|
///|
/// 辅助函数:计算局部窗口的均值和标准差
fn local_mean_std(
data : Array[Int],
width : Int,
height : Int,
x : Int,
y : Int,
radius : Int,
) -> (Double, Double) {
let mut sum = 0.0
let mut sum_sq = 0.0
let mut count = 0
for dy in -radius..<(radius + 1) {
for dx in -radius..<(radius + 1) {
let nx = x + dx
let ny = y + dy
if nx >= 0 && nx < width && ny >= 0 && ny < height {
let v = data[ny * width + nx].to_double()
sum += v
sum_sq += v * v
count += 1
}
}
}
if count == 0 {
return (0.0, 0.0)
}
let mean = sum / count.to_double()
let variance = sum_sq / count.to_double() - mean * mean
let std = if variance < 0.0 { 0.0 } else { variance.sqrt() }
(mean, std)
}
///|
/// Niblack 局部阈值二值化
/// T(x,y) = mean(x,y) + k * std(x,y)
/// k: 修正系数,通常 -0.2
/// window_size: 局部窗口大小(奇数)
pub fn niblack_threshold(
img : @types.Image,
window_size? : Int = 15,
k? : Double = -0.2,
) -> @types.Image raise @types.LoadError {
// 输入验证
if img.width <= 0 || img.height <= 0 {
raise @types.LoadError::DecodeFailed(
"niblack_threshold: 图像尺寸必须大于0",
)
}
if img.channels != 1 {
raise @types.LoadError::DecodeFailed(
"niblack_threshold: 仅支持单通道灰度图",
)
}
let expected_size = img.width * img.height
if img.data.length() != expected_size {
raise @types.LoadError::DecodeFailed(
"niblack_threshold: 数据大小不匹配,期望 " +
expected_size.to_string() +
",实际 " +
img.data.length().to_string(),
)
}
if window_size <= 0 || window_size % 2 == 0 {
raise @types.LoadError::DecodeFailed(
"niblack_threshold: window_size必须为正奇数",
)
}
let w = img.width
let h = img.height
let radius = window_size / 2
// 转换为 Int 数组
let data : Array[Int] = Array::make(w * h, 0)
for i in 0..<(w * h) {
data[i] = img.data[i].to_int()
}
let out : Array[Byte] = Array::make(w * h, b'\x00')
for y in 0..<h {
for x in 0..<w {
let (mean, std) = local_mean_std(data, w, h, x, y, radius)
let threshold = mean + k * std
let v = data[y * w + x].to_double()
out[y * w + x] = if v > threshold { b'\xFF' } else { b'\x00' }
}
}
@types.Image::{
width: w,
height: h,
channels: 1,
data: Bytes::from_array(out),
}
}
///|
/// Sauvola 局部阈值二值化(Niblack 的改进版,适合文档图像)
/// T(x,y) = mean(x,y) * (1 + k * (std(x,y)/R - 1))
/// k: 修正系数,通常 0.5
/// R: 标准差的动态范围,通常 128
/// window_size: 局部窗口大小(奇数)
pub fn sauvola_threshold(
img : @types.Image,
window_size? : Int = 15,
k? : Double = 0.5,
r? : Double = 128.0,
) -> @types.Image raise @types.LoadError {
// 输入验证
if img.width <= 0 || img.height <= 0 {
raise @types.LoadError::DecodeFailed(
"sauvola_threshold: 图像尺寸必须大于0",
)
}
if img.channels != 1 {
raise @types.LoadError::DecodeFailed(
"sauvola_threshold: 仅支持单通道灰度图",
)
}
let expected_size = img.width * img.height
if img.data.length() != expected_size {
raise @types.LoadError::DecodeFailed(
"sauvola_threshold: 数据大小不匹配,期望 " +
expected_size.to_string() +
",实际 " +
img.data.length().to_string(),
)
}
if window_size <= 0 || window_size % 2 == 0 {
raise @types.LoadError::DecodeFailed(
"sauvola_threshold: window_size必须为正奇数",
)
}
if r <= 0.0 {
raise @types.LoadError::DecodeFailed("sauvola_threshold: r必须大于0")
}
let w = img.width
let h = img.height
let radius = window_size / 2
// 转换为 Int 数组
let data : Array[Int] = Array::make(w * h, 0)
for i in 0..<(w * h) {
data[i] = img.data[i].to_int()
}
let out : Array[Byte] = Array::make(w * h, b'\x00')
for y in 0..<h {
for x in 0..<w {
let (mean, std) = local_mean_std(data, w, h, x, y, radius)
let threshold = mean * (1.0 + k * (std / r - 1.0))
let v = data[y * w + x].to_double()
out[y * w + x] = if v > threshold { b'\xFF' } else { b'\x00' }
}
}
@types.Image::{
width: w,
height: h,
channels: 1,
data: Bytes::from_array(out),
}
}
///|
/// OCR 专用自适应阈值(结合全局阈值和局部阈值)
/// 适用于光照不均匀的文档图像
pub fn adaptive_threshold_ocr(
img : @types.Image,
block_size? : Int = 15,
c? : Int = 10,
) -> @types.Image raise @types.LoadError {
// 输入验证
if img.width <= 0 || img.height <= 0 {
raise @types.LoadError::DecodeFailed(
"adaptive_threshold_ocr: 图像尺寸必须大于0",
)
}
if img.channels != 1 {
raise @types.LoadError::DecodeFailed(
"adaptive_threshold_ocr: 仅支持单通道灰度图",
)
}
let expected_size = img.width * img.height
if img.data.length() != expected_size {
raise @types.LoadError::DecodeFailed(
"adaptive_threshold_ocr: 数据大小不匹配,期望 " +
expected_size.to_string() +
",实际 " +
img.data.length().to_string(),
)
}
if block_size <= 0 || block_size % 2 == 0 {
raise @types.LoadError::DecodeFailed(
"adaptive_threshold_ocr: block_size必须为正奇数",
)
}
let w = img.width
let h = img.height
let radius = block_size / 2
// 转换为 Int 数组
let data : Array[Int] = Array::make(w * h, 0)
for i in 0..<(w * h) {
data[i] = img.data[i].to_int()
}
let out : Array[Byte] = Array::make(w * h, b'\x00')
for y in 0..<h {
for x in 0..<w {
// 计算局部均值
let (mean, _) = local_mean_std(data, w, h, x, y, radius)
let threshold = mean - c.to_double()
let v = data[y * w + x].to_double()
out[y * w + x] = if v > threshold { b'\xFF' } else { b'\x00' }
}
}
@types.Image::{
width: w,
height: h,
channels: 1,
data: Bytes::from_array(out),
}
}
///|
/// Otsu 全局阈值二值化(自动计算最佳阈值)
pub fn otsu_threshold(
img : @types.Image,
) -> @types.Image raise @types.LoadError {
// 输入验证
if img.width <= 0 || img.height <= 0 {
raise @types.LoadError::DecodeFailed(
"otsu_threshold: 图像尺寸必须大于0",
)
}
if img.channels != 1 {
raise @types.LoadError::DecodeFailed(
"otsu_threshold: 仅支持单通道灰度图",
)
}
let expected_size = img.width * img.height
if img.data.length() != expected_size {
raise @types.LoadError::DecodeFailed(
"otsu_threshold: 数据大小不匹配,期望 " +
expected_size.to_string() +
",实际 " +
img.data.length().to_string(),
)
}
let w = img.width
let h = img.height
let n = w * h
// 计算直方图
let histogram : Array[Int] = Array::make(256, 0)
for i in 0..<n {
histogram[img.data[i].to_int()] += 1
}
// 计算 Otsu 最佳阈值
let mut sum = 0
for t in 0..<256 {
sum += t * histogram[t]
}
let mut sum_b = 0
let mut w_b = 0
let mut max_variance = 0.0
let mut threshold = 128
for t in 0..<256 {
w_b += histogram[t]
if w_b == 0 {
continue
}
let w_f = n - w_b
if w_f == 0 {
break
}
sum_b += t * histogram[t]
let m_b = sum_b.to_double() / w_b.to_double()
let m_f = (sum - sum_b).to_double() / w_f.to_double()
let variance = w_b.to_double() * w_f.to_double() * (m_b - m_f) * (m_b - m_f)
if variance > max_variance {
max_variance = variance
threshold = t
}
}
// 应用阈值
let out : Array[Byte] = Array::make(n, b'\x00')
for i in 0..<n {
out[i] = if img.data[i].to_int() > threshold { b'\xFF' } else { b'\x00' }
}
@types.Image::{
width: w,
height: h,
channels: 1,
data: Bytes::from_array(out),
}
}