image.vision\ocr\document_enhance.mbt
85.6% · 101 / 118 points · 344 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/document_enhance - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 文档图像增强 - 7
// 文档增强、阴影去除、背景归一化 - 8
- 9
///| - 10
/// 文档图像增强(去噪+对比度+锐化) - 11
/// 适用于扫描文档和照片文档的预处理 - 12
pub fn document_enhance( - 13
img : @types.Image, - 14
) -> @types.Image raise @types.LoadError { - 15
// 输入验证 - 16●
if img.width <= 0 || img.height <= 0 { - 17●
raise @types.LoadError::DecodeFailed( - 18
"document_enhance: 图像尺寸必须大于0", - 19
) - 20
} - 21
if img.channels < 1 || img.channels > 4 { - 22●
raise @types.LoadError::DecodeFailed( - 23
"document_enhance: 通道数必须在1-4之间", - 24
) - 25
} - 26
let expected_size = img.width * img.height * img.channels - 27●
if img.data.length() != expected_size { - 28○
raise @types.LoadError::DecodeFailed( - 29
"document_enhance: 数据大小不匹配,期望 " + - 30○
expected_size.to_string() + - 31
",实际 " + - 32○
img.data.length().to_string(), - 33
) - 34
} - 35
let mut result = img - 36
// 1. 转灰度(如果是彩色图像) - 37
if result.channels != 1 { - 38●
result = to_grayscale_doc(result) - 39
} - 40
// 2. 对比度增强(拉伸直方图) - 41●
result = contrast_stretch_doc(result) - 42
// 3. 轻度去噪(中值滤波) - 43●
result = median_denoise_doc(result, 3) - 44
// 4. 锐化(增强文字边缘) - 45●
result = sharpen_doc(result, 0.5) - 46
result - 47
} - 48
- 49
///| - 50
/// 转灰度(文档专用,BT.601) - 51
fn to_grayscale_doc(img : @types.Image) -> @types.Image { - 52●
if img.channels == 1 { - 53○
return img - 54
} - 55
let n = img.width * img.height - 56
let ch = img.channels - 57●
let out : Array[Byte] = Array::make(n, b'\x00') - 58
let data = img.data - 59
for i in 0..<n { - 60●
let base = i * ch - 61●
let r = data[base].to_int() - 62●
let g = data[base + 1].to_int() - 63●
let b = data[base + 2].to_int() - 64
let gray = (r * 299 + g * 587 + b * 114) / 1000 - 65●
out[i] = gray.to_byte() - 66
} - 67
@types.Image::{ - 68
width: img.width, - 69
height: img.height, - 70
channels: 1, - 71●
data: Bytes::from_array(out), - 72
} - 73
} - 74
- 75
///| - 76
/// 对比度拉伸(文档专用) - 77
fn contrast_stretch_doc(img : @types.Image) -> @types.Image { - 78●
let n = img.width * img.height - 79
let data = img.data - 80
// 计算最小和最大值(忽略 1% 极值) - 81●
let histogram : Array[Int32] = Array::make(256, 0) - 82
for i in 0..<n { - 83●
histogram[data[i].to_int()] += 1 - 84
} - 85
let cutoff = n / 100 - 86
let mut count = 0 - 87
let mut min_val = 0 - 88
for i in 0..<256 { - 89●
count += histogram[i] - 90
if count >= cutoff { - 91●
min_val = i - 92
break - 93
} - 94
} - 95
count = 0 - 96
let mut max_val = 255 - 97
for i in 0..<256 { - 98●
count += histogram[255 - i] - 99
if count >= cutoff { - 100●
max_val = 255 - i - 101
break - 102
} - 103
} - 104
// 应用拉伸 - 105●
let range = if max_val > min_val { max_val - min_val } else { 1 } - 106●
let out : Array[Byte] = Array::make(n, b'\x00') - 107
for i in 0..<n { - 108●
let v = data[i].to_int() - 109
let stretched = (v - min_val) * 255 / range - 110●
out[i] = @types.clamp_byte(stretched) - 111
} - 112
@types.Image::{ - 113
width: img.width, - 114
height: img.height, - 115
channels: 1, - 116●
data: Bytes::from_array(out), - 117
} - 118
} - 119
- 120
///| - 121
/// 中值去噪(文档专用,3x3 窗口) - 122
fn median_denoise_doc(img : @types.Image, size : Int32) -> @types.Image { - 123●
let w = img.width - 124
let h = img.height - 125
let radius = size / 2 - 126
let data = img.data - 127●
let out : Array[Byte] = Array::make(w * h, b'\x00') - 128
for y in 0..<h { - 129●
for x in 0..<w { - 130●
let values : Array[Int32] = [] - 131
for dy in -radius..<(radius + 1) { - 132●
for dx in -radius..<(radius + 1) { - 133●
let nx = x + dx - 134
let ny = y + dy - 135
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 136●
values.push(data[ny * w + nx].to_int()) - 137
} - 138
} - 139
} - 140
// 简单排序取中值 - 141●
for i in 0..<values.length() { - 142●
for j in (i + 1)..<values.length() { - 143●
if values[j] < values[i] { - 144●
let tmp = values[i] - 145
values[i] = values[j] - 146
values[j] = tmp - 147
} - 148
} - 149
} - 150●
let median = values[values.length() / 2] - 151●
out[y * w + x] = median.to_byte() - 152
} - 153
} - 154
@types.Image::{ - 155
width: w, - 156
height: h, - 157
channels: 1, - 158●
data: Bytes::from_array(out), - 159
} - 160
} - 161
- 162
///| - 163
/// 锐化(文档专用,USM 简化版) - 164
fn sharpen_doc(img : @types.Image, amount : Double) -> @types.Image { - 165●
let w = img.width - 166
let h = img.height - 167
let data = img.data - 168●
let out : Array[Byte] = Array::make(w * h, b'\x00') - 169
for y in 0..<h { - 170●
for x in 0..<w { - 171●
let center = data[y * w + x].to_int().to_double() - 172
// 计算邻域均值 - 173
let mut sum = 0.0 - 174
let mut count = 0 - 175
for dy in -1..<2 { - 176●
for dx in -1..<2 { - 177●
let nx = x + dx - 178
let ny = y + dy - 179
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 180●
sum += data[ny * w + nx].to_int().to_double() - 181
count += 1 - 182
} - 183
} - 184
} - 185●
let mean = sum / count.to_double() - 186
// USM: result = center + amount * (center - mean) - 187
let sharpened = center + amount * (center - mean) - 188●
out[y * w + x] = @types.clamp_byte(sharpened.to_int()) - 189
} - 190
} - 191
@types.Image::{ - 192
width: w, - 193
height: h, - 194
channels: 1, - 195●
data: Bytes::from_array(out), - 196
} - 197
} - 198
- 199
///| - 200
/// 阴影去除(基于大核高斯模糊的背景估计) - 201
/// 通过估计背景光照并去除,实现阴影去除 - 202
pub fn shadow_removal( - 203
img : @types.Image, - 204
kernel_size~ : Int32 = 51, - 205
) -> @types.Image raise @types.LoadError { - 206
// 输入验证 - 207●
if img.width <= 0 || img.height <= 0 { - 208●
raise @types.LoadError::DecodeFailed( - 209
"shadow_removal: 图像尺寸必须大于0", - 210
) - 211
} - 212
if img.channels != 1 { - 213○
raise @types.LoadError::DecodeFailed( - 214
"shadow_removal: 仅支持单通道灰度图", - 215
) - 216
} - 217
let expected_size = img.width * img.height - 218●
if img.data.length() != expected_size { - 219○
raise @types.LoadError::DecodeFailed( - 220
"shadow_removal: 数据大小不匹配,期望 " + - 221○
expected_size.to_string() + - 222
",实际 " + - 223○
img.data.length().to_string(), - 224
) - 225
} - 226
if kernel_size <= 0 || kernel_size % 2 == 0 { - 227○
raise @types.LoadError::DecodeFailed( - 228
"shadow_removal: 核大小必须为正奇数", - 229
) - 230
} - 231
let w = img.width - 232
let h = img.height - 233
let data = img.data - 234
// 估计背景(大核均值模糊) - 235
let radius = kernel_size / 2 - 236●
let background : Array[Int32] = Array::make(w * h, 0) - 237
for y in 0..<h { - 238●
for x in 0..<w { - 239●
let mut sum = 0 - 240
let mut count = 0 - 241
for dy in -radius..<(radius + 1) { - 242●
for dx in -radius..<(radius + 1) { - 243●
let nx = x + dx - 244
let ny = y + dy - 245
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 246●
sum += data[ny * w + nx].to_int() - 247
count += 1 - 248
} - 249
} - 250
} - 251
background[y * w + x] = sum / count - 252
} - 253
} - 254
// 去除阴影:result = (original / background) * 255 - 255●
let out : Array[Byte] = Array::make(w * h, b'\x00') - 256
for i in 0..<(w * h) { - 257●
let orig = data[i].to_int() - 258
let bg = background[i] - 259◐
let result = if bg > 0 { orig * 255 / bg } else { 255 } - 260●
out[i] = @types.clamp_byte(result) - 261
} - 262
@types.Image::{ - 263
width: w, - 264
height: h, - 265
channels: 1, - 266●
data: Bytes::from_array(out), - 267
} - 268
} - 269
- 270
///| - 271
/// 背景归一化(将背景统一为白色) - 272
/// 适用于背景不均匀的文档图像 - 273
pub fn background_normalization( - 274
img : @types.Image, - 275
kernel_size~ : Int32 = 31, - 276
) -> @types.Image raise @types.LoadError { - 277
// 输入验证 - 278●
if img.width <= 0 || img.height <= 0 { - 279●
raise @types.LoadError::DecodeFailed( - 280
"background_normalization: 图像尺寸必须大于0", - 281
) - 282
} - 283
if img.channels != 1 { - 284●
raise @types.LoadError::DecodeFailed( - 285
"background_normalization: 仅支持单通道灰度图", - 286
) - 287
} - 288
let expected_size = img.width * img.height - 289●
if img.data.length() != expected_size { - 290○
raise @types.LoadError::DecodeFailed( - 291
"background_normalization: 数据大小不匹配,期望 " + - 292○
expected_size.to_string() + - 293
",实际 " + - 294○
img.data.length().to_string(), - 295
) - 296
} - 297
if kernel_size <= 0 || kernel_size % 2 == 0 { - 298○
raise @types.LoadError::DecodeFailed( - 299
"background_normalization: 核大小必须为正奇数", - 300
) - 301
} - 302
let w = img.width - 303
let h = img.height - 304
let data = img.data - 305
// 估计背景(大核中值模糊) - 306
let radius = kernel_size / 2 - 307●
let background : Array[Int32] = Array::make(w * h, 0) - 308
for y in 0..<h { - 309●
for x in 0..<w { - 310●
let values : Array[Int32] = [] - 311
for dy in -radius..<(radius + 1) { - 312●
for dx in -radius..<(radius + 1) { - 313●
let nx = x + dx - 314
let ny = y + dy - 315
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 316●
values.push(data[ny * w + nx].to_int()) - 317
} - 318
} - 319
} - 320
// 取最大值作为背景估计(文字是暗的,背景是亮的) - 321
let mut max_val = 0 - 322
for v in values { - 323●
if v > max_val { - 324●
max_val = v - 325
} - 326
} - 327
background[y * w + x] = max_val - 328
} - 329
} - 330
// 归一化:result = original * (255 / background) - 331●
let out : Array[Byte] = Array::make(w * h, b'\x00') - 332
for i in 0..<(w * h) { - 333●
let orig = data[i].to_int() - 334
let bg = background[i] - 335●
let result = if bg > 0 { orig * 255 / bg } else { 255 } - 336●
out[i] = @types.clamp_byte(result) - 337
} - 338
@types.Image::{ - 339
width: w, - 340
height: h, - 341
channels: 1, - 342●
data: Bytes::from_array(out), - 343
} - 344
}