image.vision\ocr\binarization.mbt
77.6% · 83 / 107 points · 314 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 二值化 ocr/binarization - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// OCR 专用二值化算法 - 7
// Sauvola 阈值、Niblack 阈值、OCR 自适应阈值 - 8
- 9
///| - 10
/// 辅助函数:限制字节范围 - 11
fn _clamp_byte_ocr(v : Int32) -> Byte { - 12○
if v < 0 { - 13○
b'\x00' - 14○
} else if v > 255 { - 15○
b'\xFF' - 16
} else { - 17○
v.to_byte() - 18
} - 19
} - 20
- 21
///| - 22
/// 辅助函数:计算局部窗口的均值和标准差 - 23
fn local_mean_std( - 24
data : Array[Int32], - 25
width : Int32, - 26
height : Int32, - 27
x : Int32, - 28
y : Int32, - 29
radius : Int32, - 30
) -> (Double, Double) { - 31●
let mut sum = 0.0 - 32
let mut sum_sq = 0.0 - 33
let mut count = 0 - 34
for dy in -radius..<(radius + 1) { - 35●
for dx in -radius..<(radius + 1) { - 36●
let nx = x + dx - 37
let ny = y + dy - 38
if nx >= 0 && nx < width && ny >= 0 && ny < height { - 39●
let v = data[ny * width + nx].to_double() - 40
sum += v - 41
sum_sq += v * v - 42
count += 1 - 43
} - 44
} - 45
} - 46
if count == 0 { - 47○
return (0.0, 0.0) - 48
} - 49●
let mean = sum / count.to_double() - 50●
let variance = sum_sq / count.to_double() - mean * mean - 51◐
let std = if variance < 0.0 { 0.0 } else { variance.sqrt() } - 52
(mean, std) - 53
} - 54
- 55
///| - 56
/// Niblack 局部阈值二值化 - 57
/// 自适应阈值 T(x,y) = mean(x,y) + k * std(x,y) - 58
/// k: 修正系数,通常 -0.2 - 59
/// window_size: 局部窗口大小(奇数) - 60
pub fn niblack_threshold( - 61
img : @types.Image, - 62
window_size~ : Int32 = 15, - 63
k~ : Double = -0.2, - 64
) -> @types.Image raise @types.LoadError { - 65
// 输入验证 - 66●
if img.width <= 0 || img.height <= 0 { - 67●
raise @types.LoadError::DecodeFailed( - 68
"niblack_threshold: 图像尺寸必须大于0", - 69
) - 70
} - 71
if img.channels != 1 { - 72●
raise @types.LoadError::DecodeFailed( - 73
"niblack_threshold: 仅支持单通道灰度图", - 74
) - 75
} - 76
let expected_size = img.width * img.height - 77●
if img.data.length() != expected_size { - 78○
raise @types.LoadError::DecodeFailed( - 79
"niblack_threshold: 数据大小不匹配,期望 " + - 80○
expected_size.to_string() + - 81
",实际 " + - 82○
img.data.length().to_string(), - 83
) - 84
} - 85
if window_size <= 0 || window_size % 2 == 0 { - 86●
raise @types.LoadError::DecodeFailed( - 87
"niblack_threshold: window_size必须为正奇数", - 88
) - 89
} - 90
- 91
let w = img.width - 92
let h = img.height - 93
let radius = window_size / 2 - 94
// 转换为 Int 数组 - 95●
let data : Array[Int32] = Array::make(w * h, 0) - 96
for i in 0..<(w * h) { - 97●
data[i] = img.data[i].to_int() - 98
} - 99●
let out : Array[Byte] = Array::make(w * h, b'\x00') - 100
for y in 0..<h { - 101●
for x in 0..<w { - 102●
let (mean, std) = local_mean_std(data, w, h, x, y, radius) - 103
let threshold = mean + k * std - 104●
let v = data[y * w + x].to_double() - 105●
out[y * w + x] = if v > threshold { b'\xFF' } else { b'\x00' } - 106
} - 107
} - 108
@types.Image::{ - 109
width: w, - 110
height: h, - 111
channels: 1, - 112●
data: Bytes::from_array(out), - 113
} - 114
} - 115
- 116
///| - 117
/// Sauvola 局部阈值二值化(Niblack 的改进版,适合文档图像) - 118
/// 自适应阈值 T(x,y) = mean(x,y) * (1 + k * (std(x,y)/R - 1)) - 119
/// k: 修正系数,通常 0.5 - 120
/// R: 标准差的动态范围,通常 128 - 121
/// window_size: 局部窗口大小(奇数) - 122
pub fn sauvola_threshold( - 123
img : @types.Image, - 124
window_size~ : Int32 = 15, - 125
k~ : Double = 0.5, - 126
r~ : Double = 128.0, - 127
) -> @types.Image raise @types.LoadError { - 128
// 输入验证 - 129●
if img.width <= 0 || img.height <= 0 { - 130●
raise @types.LoadError::DecodeFailed( - 131
"sauvola_threshold: 图像尺寸必须大于0", - 132
) - 133
} - 134
if img.channels != 1 { - 135●
raise @types.LoadError::DecodeFailed( - 136
"sauvola_threshold: 仅支持单通道灰度图", - 137
) - 138
} - 139
let expected_size = img.width * img.height - 140●
if img.data.length() != expected_size { - 141○
raise @types.LoadError::DecodeFailed( - 142
"sauvola_threshold: 数据大小不匹配,期望 " + - 143○
expected_size.to_string() + - 144
",实际 " + - 145○
img.data.length().to_string(), - 146
) - 147
} - 148
if window_size <= 0 || window_size % 2 == 0 { - 149●
raise @types.LoadError::DecodeFailed( - 150
"sauvola_threshold: window_size必须为正奇数", - 151
) - 152
} - 153
if r <= 0.0 { - 154●
raise @types.LoadError::DecodeFailed("sauvola_threshold: r必须大于0") - 155
} - 156
- 157
let w = img.width - 158
let h = img.height - 159
let radius = window_size / 2 - 160
// 转换为 Int 数组 - 161●
let data : Array[Int32] = Array::make(w * h, 0) - 162
for i in 0..<(w * h) { - 163●
data[i] = img.data[i].to_int() - 164
} - 165●
let out : Array[Byte] = Array::make(w * h, b'\x00') - 166
for y in 0..<h { - 167●
for x in 0..<w { - 168●
let (mean, std) = local_mean_std(data, w, h, x, y, radius) - 169
let threshold = mean * (1.0 + k * (std / r - 1.0)) - 170●
let v = data[y * w + x].to_double() - 171●
out[y * w + x] = if v > threshold { b'\xFF' } else { b'\x00' } - 172
} - 173
} - 174
@types.Image::{ - 175
width: w, - 176
height: h, - 177
channels: 1, - 178●
data: Bytes::from_array(out), - 179
} - 180
} - 181
- 182
///| - 183
/// OCR 专用自适应阈值(结合全局阈值和局部阈值) - 184
/// 适用于光照不均匀的文档图像 - 185
pub fn adaptive_threshold_ocr( - 186
img : @types.Image, - 187
block_size~ : Int32 = 15, - 188
c~ : Int32 = 10, - 189
) -> @types.Image raise @types.LoadError { - 190
// 输入验证 - 191●
if img.width <= 0 || img.height <= 0 { - 192●
raise @types.LoadError::DecodeFailed( - 193
"adaptive_threshold_ocr: 图像尺寸必须大于0", - 194
) - 195
} - 196
if img.channels != 1 { - 197●
raise @types.LoadError::DecodeFailed( - 198
"adaptive_threshold_ocr: 仅支持单通道灰度图", - 199
) - 200
} - 201
let expected_size = img.width * img.height - 202●
if img.data.length() != expected_size { - 203○
raise @types.LoadError::DecodeFailed( - 204
"adaptive_threshold_ocr: 数据大小不匹配,期望 " + - 205○
expected_size.to_string() + - 206
",实际 " + - 207○
img.data.length().to_string(), - 208
) - 209
} - 210
if block_size <= 0 || block_size % 2 == 0 { - 211●
raise @types.LoadError::DecodeFailed( - 212
"adaptive_threshold_ocr: block_size必须为正奇数", - 213
) - 214
} - 215
- 216
let w = img.width - 217
let h = img.height - 218
let radius = block_size / 2 - 219
// 转换为 Int 数组 - 220●
let data : Array[Int32] = Array::make(w * h, 0) - 221
for i in 0..<(w * h) { - 222●
data[i] = img.data[i].to_int() - 223
} - 224●
let out : Array[Byte] = Array::make(w * h, b'\x00') - 225
for y in 0..<h { - 226●
for x in 0..<w { - 227
// 计算局部均值 - 228●
let (mean, _) = local_mean_std(data, w, h, x, y, radius) - 229●
let threshold = mean - c.to_double() - 230●
let v = data[y * w + x].to_double() - 231●
out[y * w + x] = if v > threshold { b'\xFF' } else { b'\x00' } - 232
} - 233
} - 234
@types.Image::{ - 235
width: w, - 236
height: h, - 237
channels: 1, - 238●
data: Bytes::from_array(out), - 239
} - 240
} - 241
- 242
///| - 243
/// Otsu 全局阈值二值化(自动计算最佳阈值) - 244
pub fn otsu_threshold( - 245
img : @types.Image, - 246
) -> @types.Image raise @types.LoadError { - 247
// 输入验证 - 248●
if img.width <= 0 || img.height <= 0 { - 249●
raise @types.LoadError::DecodeFailed( - 250
"otsu_threshold: 图像尺寸必须大于0", - 251
) - 252
} - 253
if img.channels != 1 { - 254●
raise @types.LoadError::DecodeFailed( - 255
"otsu_threshold: 仅支持单通道灰度图", - 256
) - 257
} - 258
let expected_size = img.width * img.height - 259●
if img.data.length() != expected_size { - 260○
raise @types.LoadError::DecodeFailed( - 261
"otsu_threshold: 数据大小不匹配,期望 " + - 262○
expected_size.to_string() + - 263
",实际 " + - 264○
img.data.length().to_string(), - 265
) - 266
} - 267
- 268
let w = img.width - 269
let h = img.height - 270
let n = w * h - 271
// 计算直方图 - 272●
let histogram : Array[Int32] = Array::make(256, 0) - 273
for i in 0..<n { - 274●
histogram[img.data[i].to_int()] += 1 - 275
} - 276
// 计算 Otsu 最佳阈值 - 277
let mut sum = 0 - 278
for t in 0..<256 { - 279●
sum += t * histogram[t] - 280
} - 281
let mut sum_b = 0 - 282
let mut w_b = 0 - 283
let mut max_variance = 0.0 - 284
let mut threshold = 128 - 285
for t in 0..<256 { - 286●
w_b += histogram[t] - 287
if w_b == 0 { - 288●
continue - 289
} - 290
let w_f = n - w_b - 291
if w_f == 0 { - 292●
break - 293
} - 294
sum_b += t * histogram[t] - 295●
let m_b = sum_b.to_double() / w_b.to_double() - 296●
let m_f = (sum - sum_b).to_double() / w_f.to_double() - 297●
let variance = w_b.to_double() * w_f.to_double() * (m_b - m_f) * (m_b - m_f) - 298
if variance > max_variance { - 299●
max_variance = variance - 300
threshold = t - 301
} - 302
} - 303
// 应用阈值 - 304●
let out : Array[Byte] = Array::make(n, b'\x00') - 305
for i in 0..<n { - 306●
out[i] = if img.data[i].to_int() > threshold { b'\xFF' } else { b'\x00' } - 307
} - 308
@types.Image::{ - 309
width: w, - 310
height: h, - 311
channels: 1, - 312●
data: Bytes::from_array(out), - 313
} - 314
}