image.vision\ocr\projection_analysis.mbt
95.2% · 178 / 187 points · 645 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/projection_analysis - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 投影分析与文本行分割 - 7
// 水平/垂直投影、文本行检测、字符分割 - 8
- 9
///| - 10
/// 投影结果结构体 - 11
#derive(Eq, @debug.Debug) - 12
pub(all) struct ProjectionResult { - 13
projection : Array[Int32] - 14
peaks : Array[Int32] - 15
valleys : Array[Int32] - 16
mean : Double - 17
stddev : Double - 18
} - 19
- 20
///| - 21
pub extend ProjectionResult with Eq::{not_equal, equal} - 22
- 23
///| - 24
pub extend ProjectionResult with @debug.Debug::{to_repr} - 25
- 26
///| - 27
/// 水平投影(按行统计前景像素数) - 28
pub fn horizontal_projection( - 29
img : @types.Image, - 30
) -> ProjectionResult raise @types.LoadError { - 31
// 输入验证 - 32●
if img.width <= 0 || img.height <= 0 { - 33●
raise @types.LoadError::DecodeFailed( - 34
"horizontal_projection: 图像尺寸必须大于0", - 35
) - 36
} - 37
if img.channels != 1 { - 38●
raise @types.LoadError::DecodeFailed( - 39
"horizontal_projection: 仅支持单通道二值图", - 40
) - 41
} - 42
let expected_size = img.width * img.height - 43●
if img.data.length() != expected_size { - 44●
raise @types.LoadError::DecodeFailed( - 45
"horizontal_projection: 数据大小不匹配,期望 " + - 46●
expected_size.to_string() + - 47
",实际 " + - 48●
img.data.length().to_string(), - 49
) - 50
} - 51
- 52
let w = img.width - 53
let h = img.height - 54
let data = img.data - 55●
let projection : Array[Int32] = Array::make(h, 0) - 56
for y in 0..<h { - 57●
for x in 0..<w { - 58●
if data[y * w + x].to_int() > 128 { - 59●
projection[y] += 1 - 60
} - 61
} - 62
} - 63●
analyze_projection(projection) - 64
} - 65
- 66
///| - 67
/// 垂直投影(按列统计前景像素数) - 68
pub fn vertical_projection( - 69
img : @types.Image, - 70
) -> ProjectionResult raise @types.LoadError { - 71
// 输入验证 - 72●
if img.width <= 0 || img.height <= 0 { - 73●
raise @types.LoadError::DecodeFailed( - 74
"vertical_projection: 图像尺寸必须大于0", - 75
) - 76
} - 77
if img.channels != 1 { - 78●
raise @types.LoadError::DecodeFailed( - 79
"vertical_projection: 仅支持单通道二值图", - 80
) - 81
} - 82
let expected_size = img.width * img.height - 83●
if img.data.length() != expected_size { - 84●
raise @types.LoadError::DecodeFailed( - 85
"vertical_projection: 数据大小不匹配,期望 " + - 86●
expected_size.to_string() + - 87
",实际 " + - 88●
img.data.length().to_string(), - 89
) - 90
} - 91
- 92
let w = img.width - 93
let h = img.height - 94
let data = img.data - 95●
let projection : Array[Int32] = Array::make(w, 0) - 96
for y in 0..<h { - 97●
for x in 0..<w { - 98●
if data[y * w + x].to_int() > 128 { - 99●
projection[x] += 1 - 100
} - 101
} - 102
} - 103●
analyze_projection(projection) - 104
} - 105
- 106
///| - 107
/// 分析投影(找峰值、谷值、均值、标准差) - 108
fn analyze_projection(projection : Array[Int32]) -> ProjectionResult { - 109●
let n = projection.length() - 110
// 计算均值和标准差 - 111
let mut sum = 0 - 112
let mut sum_sq = 0 - 113
for i in 0..<n { - 114●
sum += projection[i] - 115
sum_sq += projection[i] * projection[i] - 116
} - 117◐
let mean = if n > 0 { sum.to_double() / n.to_double() } else { 0.0 } - 118
let variance = if n > 0 { - 119●
sum_sq.to_double() / n.to_double() - mean * mean - 120
} else { - 121○
0.0 - 122
} - 123●
let stddev = if variance > 0.0 { variance.sqrt() } else { 0.0 } - 124
// 找峰值(局部最大值) - 125
let peaks : Array[Int32] = [] - 126
for i in 1..<(n - 1) { - 127●
if projection[i] > projection[i - 1] && - 128
projection[i] >= projection[i + 1] && - 129●
projection[i].to_double() > mean { - 130●
peaks.push(i) - 131
} - 132
} - 133
// 找谷值(局部最小值) - 134
let valleys : Array[Int32] = [] - 135
for i in 1..<(n - 1) { - 136●
if projection[i] < projection[i - 1] && - 137
projection[i] <= projection[i + 1] && - 138●
projection[i].to_double() < mean { - 139●
valleys.push(i) - 140
} - 141
} - 142
ProjectionResult::{ projection, peaks, valleys, mean, stddev, } - 143
} - 144
- 145
///| - 146
/// 文本行结构体 - 147
#derive(Eq, @debug.Debug) - 148
pub(all) struct TextLineInfo { - 149
y_start : Int32 - 150
y_end : Int32 - 151
height : Int32 - 152
x_start : Int32 - 153
x_end : Int32 - 154
width : Int32 - 155
pixel_count : Int32 - 156
baseline : Int32 - 157
} - 158
- 159
///| - 160
pub extend TextLineInfo with Eq::{not_equal, equal} - 161
- 162
///| - 163
pub extend TextLineInfo with @debug.Debug::{to_repr} - 164
- 165
///| - 166
/// 检测文本行(基于水平投影) - 167
pub fn detect_text_lines_projection( - 168
img : @types.Image, - 169
min_height~ : Int32 = 5, - 170
max_gap~ : Int32 = 3, - 171
) -> Array[TextLineInfo] raise @types.LoadError { - 172
// 输入验证 - 173●
if img.width <= 0 || img.height <= 0 { - 174●
raise @types.LoadError::DecodeFailed( - 175
"detect_text_lines_projection: 图像尺寸必须大于0", - 176
) - 177
} - 178
if img.channels != 1 { - 179●
raise @types.LoadError::DecodeFailed( - 180
"detect_text_lines_projection: 仅支持单通道二值图", - 181
) - 182
} - 183
let expected_size = img.width * img.height - 184●
if img.data.length() != expected_size { - 185●
raise @types.LoadError::DecodeFailed( - 186
"detect_text_lines_projection: 数据大小不匹配,期望 " + - 187●
expected_size.to_string() + - 188
",实际 " + - 189●
img.data.length().to_string(), - 190
) - 191
} - 192
if min_height <= 0 { - 193●
raise @types.LoadError::DecodeFailed( - 194
"detect_text_lines_projection: min_height必须大于0", - 195
) - 196
} - 197
if max_gap < 0 { - 198●
raise @types.LoadError::DecodeFailed( - 199
"detect_text_lines_projection: max_gap不能为负数", - 200
) - 201
} - 202
- 203
let w = img.width - 204
let h = img.height - 205
let data = img.data - 206
// 水平投影 - 207●
let projection : Array[Int32] = Array::make(h, 0) - 208
for y in 0..<h { - 209●
for x in 0..<w { - 210●
if data[y * w + x].to_int() > 128 { - 211●
projection[y] += 1 - 212
} - 213
} - 214
} - 215
// 找文本行(连续的非零行) - 216
let lines : Array[TextLineInfo] = [] - 217
let mut in_line = false - 218
let mut start_y = 0 - 219
let mut gap_count = 0 - 220
for y in 0..<h { - 221●
if projection[y] > 0 { - 222●
if !in_line { - 223●
in_line = true - 224
start_y = y - 225
} - 226
gap_count = 0 - 227●
} else if in_line { - 228●
gap_count += 1 - 229
if gap_count > max_gap { - 230
// 结束当前行 - 231●
let end_y = y - gap_count - 232
let line_height = end_y - start_y + 1 - 233
if line_height >= min_height { - 234
// 计算这一行的水平范围和像素数 - 235●
let mut min_x = w - 236
let mut max_x = 0 - 237
let mut pixel_count = 0 - 238
let mut baseline_y = start_y - 239
let mut max_proj = 0 - 240
for yy in start_y..<(end_y + 1) { - 241●
if projection[yy] > max_proj { - 242●
max_proj = projection[yy] - 243
baseline_y = yy - 244
} - 245
for x in 0..<w { - 246●
if data[yy * w + x].to_int() > 128 { - 247●
pixel_count += 1 - 248
if x < min_x { - 249●
min_x = x - 250
} - 251
if x > max_x { - 252●
max_x = x - 253
} - 254
} - 255
} - 256
} - 257
if max_x > min_x { - 258●
lines.push(TextLineInfo::{ - 259
y_start: start_y, - 260
y_end: end_y, - 261
height: line_height, - 262
x_start: min_x, - 263
x_end: max_x, - 264
width: max_x - min_x + 1, - 265
pixel_count, - 266
baseline: baseline_y, - 267
}) - 268
} - 269
} - 270
in_line = false - 271
gap_count = 0 - 272
} - 273
} - 274
} - 275
// 处理最后一行 - 276
if in_line { - 277●
let end_y = h - 1 - 278
let line_height = end_y - start_y + 1 - 279
if line_height >= min_height { - 280●
let mut min_x = w - 281
let mut max_x = 0 - 282
let mut pixel_count = 0 - 283
for yy in start_y..<(end_y + 1) { - 284●
for x in 0..<w { - 285●
if data[yy * w + x].to_int() > 128 { - 286●
pixel_count += 1 - 287
if x < min_x { - 288●
min_x = x - 289
} - 290
if x > max_x { - 291●
max_x = x - 292
} - 293
} - 294
} - 295
} - 296
if max_x > min_x { - 297●
lines.push(TextLineInfo::{ - 298
y_start: start_y, - 299
y_end: end_y, - 300
height: line_height, - 301
x_start: min_x, - 302
x_end: max_x, - 303
width: max_x - min_x + 1, - 304
pixel_count, - 305
baseline: start_y + line_height / 2, - 306
}) - 307
} - 308
} - 309
} - 310
lines - 311
} - 312
- 313
///| - 314
/// 字符分割结构体 - 315
#derive(Eq, @debug.Debug) - 316
pub(all) struct CharSegment { - 317
x_start : Int32 - 318
x_end : Int32 - 319
width : Int32 - 320
y_start : Int32 - 321
y_end : Int32 - 322
height : Int32 - 323
pixel_count : Int32 - 324
} - 325
- 326
///| - 327
pub extend CharSegment with Eq::{not_equal, equal} - 328
- 329
///| - 330
pub extend CharSegment with @debug.Debug::{to_repr} - 331
- 332
///| - 333
/// 文本行字符分割(基于垂直投影) - 334
pub fn segment_chars_projection( - 335
img : @types.Image, - 336
line : TextLineInfo, - 337
min_width~ : Int32 = 2, - 338
max_gap~ : Int32 = 2, - 339
) -> Array[CharSegment] raise @types.LoadError { - 340
// 输入验证 - 341●
if img.width <= 0 || img.height <= 0 { - 342●
raise @types.LoadError::DecodeFailed( - 343
"segment_chars_projection: 图像尺寸必须大于0", - 344
) - 345
} - 346
if img.channels != 1 { - 347●
raise @types.LoadError::DecodeFailed( - 348
"segment_chars_projection: 仅支持单通道二值图", - 349
) - 350
} - 351
let expected_size = img.width * img.height - 352●
if img.data.length() != expected_size { - 353●
raise @types.LoadError::DecodeFailed( - 354
"segment_chars_projection: 数据大小不匹配,期望 " + - 355●
expected_size.to_string() + - 356
",实际 " + - 357●
img.data.length().to_string(), - 358
) - 359
} - 360
if line.width <= 0 || line.height <= 0 { - 361●
raise @types.LoadError::DecodeFailed( - 362
"segment_chars_projection: 文本行尺寸必须大于0", - 363
) - 364
} - 365
if min_width <= 0 { - 366●
raise @types.LoadError::DecodeFailed( - 367
"segment_chars_projection: min_width必须大于0", - 368
) - 369
} - 370
if max_gap < 0 { - 371●
raise @types.LoadError::DecodeFailed( - 372
"segment_chars_projection: max_gap不能为负数", - 373
) - 374
} - 375
- 376
let w = img.width - 377
let data = img.data - 378
let y_start = line.y_start - 379
let y_end = line.y_end - 380
let x_start = line.x_start - 381
let x_end = line.x_end - 382
// 垂直投影(只在文本行范围内) - 383●
let projection : Array[Int32] = Array::make(w, 0) - 384
for x in x_start..<(x_end + 1) { - 385●
for y in y_start..<(y_end + 1) { - 386●
if data[y * w + x].to_int() > 128 { - 387●
projection[x] += 1 - 388
} - 389
} - 390
} - 391
// 找字符(连续的非零列) - 392
let chars : Array[CharSegment] = [] - 393
let mut in_char = false - 394
let mut start_x = 0 - 395
let mut gap_count = 0 - 396
for x in x_start..<(x_end + 1) { - 397●
if projection[x] > 0 { - 398●
if !in_char { - 399●
in_char = true - 400
start_x = x - 401
} - 402
gap_count = 0 - 403●
} else if in_char { - 404●
gap_count += 1 - 405
if gap_count > max_gap { - 406●
let end_x = x - gap_count - 407
let char_width = end_x - start_x + 1 - 408
if char_width >= min_width { - 409
// 计算字符的垂直范围和像素数 - 410●
let mut min_y = y_end - 411
let mut max_y = y_start - 412
let mut pixel_count = 0 - 413
for xx in start_x..<(end_x + 1) { - 414●
for yy in y_start..<(y_end + 1) { - 415●
if data[yy * w + xx].to_int() > 128 { - 416●
pixel_count += 1 - 417
if yy < min_y { - 418●
min_y = yy - 419
} - 420
if yy > max_y { - 421●
max_y = yy - 422
} - 423
} - 424
} - 425
} - 426●
chars.push(CharSegment::{ - 427
x_start: start_x, - 428
x_end: end_x, - 429
width: char_width, - 430
y_start: min_y, - 431
y_end: max_y, - 432
height: max_y - min_y + 1, - 433
pixel_count, - 434
}) - 435
} - 436
in_char = false - 437
gap_count = 0 - 438
} - 439
} - 440
} - 441
// 处理最后一个字符 - 442
if in_char { - 443●
let end_x = x_end - 444
let char_width = end_x - start_x + 1 - 445
if char_width >= min_width { - 446●
let mut min_y = y_end - 447
let mut max_y = y_start - 448
let mut pixel_count = 0 - 449
for xx in start_x..<(end_x + 1) { - 450●
for yy in y_start..<(y_end + 1) { - 451●
if data[yy * w + xx].to_int() > 128 { - 452●
pixel_count += 1 - 453
if yy < min_y { - 454●
min_y = yy - 455
} - 456
if yy > max_y { - 457●
max_y = yy - 458
} - 459
} - 460
} - 461
} - 462●
chars.push(CharSegment::{ - 463
x_start: start_x, - 464
x_end: end_x, - 465
width: char_width, - 466
y_start: min_y, - 467
y_end: max_y, - 468
height: max_y - min_y + 1, - 469
pixel_count, - 470
}) - 471
} - 472
} - 473
chars - 474
} - 475
- 476
///| - 477
/// 提取文本行图像 - 478
pub fn extract_text_line_image( - 479
img : @types.Image, - 480
line : TextLineInfo, - 481
padding~ : Int32 = 2, - 482
) -> @types.Image raise @types.LoadError { - 483
// 输入验证 - 484●
if img.width <= 0 || img.height <= 0 { - 485●
raise @types.LoadError::DecodeFailed( - 486
"extract_text_line_image: 图像尺寸必须大于0", - 487
) - 488
} - 489
if img.channels < 1 || img.channels > 4 { - 490●
raise @types.LoadError::DecodeFailed( - 491
"extract_text_line_image: 通道数必须在1-4之间", - 492
) - 493
} - 494
let expected_size = img.width * img.height * img.channels - 495●
if img.data.length() != expected_size { - 496●
raise @types.LoadError::DecodeFailed( - 497
"extract_text_line_image: 数据大小不匹配,期望 " + - 498●
expected_size.to_string() + - 499
",实际 " + - 500●
img.data.length().to_string(), - 501
) - 502
} - 503
if line.width <= 0 || line.height <= 0 { - 504●
raise @types.LoadError::DecodeFailed( - 505
"extract_text_line_image: 文本行尺寸必须大于0", - 506
) - 507
} - 508
if padding < 0 { - 509●
raise @types.LoadError::DecodeFailed( - 510
"extract_text_line_image: padding不能为负数", - 511
) - 512
} - 513
- 514
let w = img.width - 515
let h = img.height - 516
let ch = img.channels - 517●
let x_start = max(0, line.x_start - padding) - 518●
let x_end = min(w - 1, line.x_end + padding) - 519●
let y_start = max(0, line.y_start - padding) - 520●
let y_end = min(h - 1, line.y_end + padding) - 521
let new_w = x_end - x_start + 1 - 522
let new_h = y_end - y_start + 1 - 523●
let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00') - 524
let data = img.data - 525
for y in 0..<new_h { - 526●
for x in 0..<new_w { - 527●
for c in 0..<ch { - 528●
out[(y * new_w + x) * ch + c] = data[((y_start + y) * w + (x_start + x)) * - 529
ch + - 530
c] - 531
} - 532
} - 533
} - 534
@types.Image::{ - 535
width: new_w, - 536
height: new_h, - 537
channels: ch, - 538●
data: Bytes::from_array(out), - 539
} - 540
} - 541
- 542
///| - 543
/// 提取字符图像 - 544
pub fn extract_char_image( - 545
img : @types.Image, - 546
char : CharSegment, - 547
target_size~ : Int32 = 32, - 548
padding~ : Int32 = 2, - 549
) -> @types.Image raise @types.LoadError { - 550
// 输入验证 - 551●
if img.width <= 0 || img.height <= 0 { - 552●
raise @types.LoadError::DecodeFailed( - 553
"extract_char_image: 图像尺寸必须大于0", - 554
) - 555
} - 556
if img.channels < 1 || img.channels > 4 { - 557●
raise @types.LoadError::DecodeFailed( - 558
"extract_char_image: 通道数必须在1-4之间", - 559
) - 560
} - 561
let expected_size = img.width * img.height * img.channels - 562●
if img.data.length() != expected_size { - 563●
raise @types.LoadError::DecodeFailed( - 564
"extract_char_image: 数据大小不匹配,期望 " + - 565●
expected_size.to_string() + - 566
",实际 " + - 567●
img.data.length().to_string(), - 568
) - 569
} - 570
if char.width <= 0 || char.height <= 0 { - 571●
raise @types.LoadError::DecodeFailed( - 572
"extract_char_image: 字符尺寸必须大于0", - 573
) - 574
} - 575
if target_size <= 0 { - 576●
raise @types.LoadError::DecodeFailed( - 577
"extract_char_image: target_size必须大于0", - 578
) - 579
} - 580
if padding < 0 { - 581●
raise @types.LoadError::DecodeFailed( - 582
"extract_char_image: padding不能为负数", - 583
) - 584
} - 585
- 586
let w = img.width - 587
let h = img.height - 588
let ch = img.channels - 589●
let x_start = max(0, char.x_start - padding) - 590●
let x_end = min(w - 1, char.x_end + padding) - 591●
let y_start = max(0, char.y_start - padding) - 592●
let y_end = min(h - 1, char.y_end + padding) - 593
let src_w = x_end - x_start + 1 - 594
let src_h = y_end - y_start + 1 - 595
// 缩放到目标尺寸(保持宽高比,居中) - 596●
let scale_w = target_size.to_double() / src_w.to_double() - 597●
let scale_h = target_size.to_double() / src_h.to_double() - 598◐
let scale = if scale_w < scale_h { scale_w } else { scale_h } - 599●
let new_w = max(1, (src_w.to_double() * scale).to_int()) - 600●
let new_h = max(1, (src_h.to_double() * scale).to_int()) - 601
let offset_x = (target_size - new_w) / 2 - 602
let offset_y = (target_size - new_h) / 2 - 603●
let out : Array[Byte] = Array::make(target_size * target_size * ch, b'\x00') - 604
let data = img.data - 605
// 最近邻缩放 - 606
for y in 0..<new_h { - 607●
for x in 0..<new_w { - 608●
let src_x = x_start + x * src_w / new_w - 609
let src_y = y_start + y * src_h / new_h - 610
for c in 0..<ch { - 611●
out[((offset_y + y) * target_size + (offset_x + x)) * ch + c] = data[( - 612
src_y * w + src_x - 613
) * - 614
ch + - 615
c] - 616
} - 617
} - 618
} - 619
@types.Image::{ - 620
width: target_size, - 621
height: target_size, - 622
channels: ch, - 623●
data: Bytes::from_array(out), - 624
} - 625
} - 626
- 627
///| - 628
/// 辅助函数:最小值 - 629
fn _min_proj(a : Int32, b : Int32) -> Int32 { - 630○
if a < b { - 631○
a - 632
} else { - 633○
b - 634
} - 635
} - 636
- 637
///| - 638
/// 辅助函数:最大值 - 639
fn _max_proj(a : Int32, b : Int32) -> Int32 { - 640○
if a > b { - 641○
a - 642
} else { - 643○
b - 644
} - 645
}