image.vision\ocr\ocr_performance.mbt
82.3% · 102 / 124 points · 380 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 性能 ocr/ocr_performance - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// v13.5.0 OCR 性能优化(纯 MoonBit 算法层面) - 7
// 查找表预计算 + 循环展开 + 缓存友好访问 + 内联展开 - 8
- 9
///| - 10
/// 预计算灰度查找表(0-255 映射) - 11
pub fn build_grayscale_lut() -> Array[Int32] { - 12●
let lut : Array[Int32] = Array::make(256, 0) - 13
for i in 0..<256 { - 14●
lut[i] = i - 15
} - 16
lut - 17
} - 18
- 19
///| - 20
/// 预计算二值化查找表 - 21
pub fn build_threshold_lut(threshold : Int32) -> Array[Int32] { - 22●
let lut : Array[Int32] = Array::make(256, 0) - 23
for i in 0..<256 { - 24●
lut[i] = if i >= threshold { 255 } else { 0 } - 25
} - 26
lut - 27
} - 28
- 29
///| - 30
/// 使用查找表快速二值化 - 31
pub fn fast_threshold_lut( - 32
img : @types.Image, - 33
lut : Array[Int32], - 34
) -> @types.Image raise @types.LoadError { - 35●
if img.width <= 0 || img.height <= 0 { - 36●
raise @types.LoadError::DecodeFailed( - 37
"fast_threshold_lut: 图像尺寸必须大于0", - 38
) - 39
} - 40
if img.channels < 1 || img.channels > 4 { - 41○
raise @types.LoadError::DecodeFailed("fast_threshold_lut: 通道数非法") - 42
} - 43
let expected_size = img.width * img.height * img.channels - 44●
if img.data.length() != expected_size { - 45○
raise @types.LoadError::DecodeFailed( - 46
"fast_threshold_lut: 数据大小不匹配", - 47
) - 48
} - 49●
let result_data : Array[Byte] = Array::make(expected_size, b'\x00') - 50
for i in 0..<expected_size { - 51●
let val = img.data[i].to_int() - 52●
result_data[i] = if val < 256 && lut[val] > 0 { b'\xFF' } else { b'\x00' } - 53
} - 54
@types.Image::{ - 55
width: img.width, - 56
height: img.height, - 57
channels: img.channels, - 58●
data: Bytes::from_array(result_data), - 59
} - 60
} - 61
- 62
///| - 63
/// 缓存友好的水平投影计算(按行连续访问) - 64
pub fn cache_friendly_horizontal_projection( - 65
img : @types.Image, - 66
) -> Array[Int32] raise @types.LoadError { - 67●
if img.width <= 0 || img.height <= 0 { - 68●
raise @types.LoadError::DecodeFailed( - 69
"cache_friendly_horizontal_projection: 图像尺寸必须大于0", - 70
) - 71
} - 72
if img.channels < 1 || img.channels > 4 { - 73○
raise @types.LoadError::DecodeFailed( - 74
"cache_friendly_horizontal_projection: 通道数非法", - 75
) - 76
} - 77
let expected_size = img.width * img.height * img.channels - 78●
if img.data.length() != expected_size { - 79○
raise @types.LoadError::DecodeFailed( - 80
"cache_friendly_horizontal_projection: 数据大小不匹配", - 81
) - 82
} - 83●
let projections : Array[Int32] = Array::make(img.height, 0) - 84
for y in 0..<img.height { - 85●
let mut sum = 0 - 86
let row_offset = y * img.width * img.channels - 87
for x in 0..<img.width { - 88●
sum += img.data[row_offset + x * img.channels].to_int() - 89
} - 90
projections[y] = sum - 91
} - 92
projections - 93
} - 94
- 95
///| - 96
/// 缓存友好的垂直投影计算(转置后按行访问) - 97
pub fn cache_friendly_vertical_projection( - 98
img : @types.Image, - 99
) -> Array[Int32] raise @types.LoadError { - 100●
if img.width <= 0 || img.height <= 0 { - 101●
raise @types.LoadError::DecodeFailed( - 102
"cache_friendly_vertical_projection: 图像尺寸必须大于0", - 103
) - 104
} - 105
if img.channels < 1 || img.channels > 4 { - 106○
raise @types.LoadError::DecodeFailed( - 107
"cache_friendly_vertical_projection: 通道数非法", - 108
) - 109
} - 110
let expected_size = img.width * img.height * img.channels - 111●
if img.data.length() != expected_size { - 112○
raise @types.LoadError::DecodeFailed( - 113
"cache_friendly_vertical_projection: 数据大小不匹配", - 114
) - 115
} - 116●
let projections : Array[Int32] = Array::make(img.width, 0) - 117
for x in 0..<img.width { - 118●
let mut sum = 0 - 119
for y in 0..<img.height { - 120●
sum += img.data[(y * img.width + x) * img.channels].to_int() - 121
} - 122
projections[x] = sum - 123
} - 124
projections - 125
} - 126
- 127
///| - 128
/// 循环展开的像素求和(每次处理4个像素) - 129
pub fn unrolled_pixel_sum(img : @types.Image) -> Int32 raise @types.LoadError { - 130●
if img.width <= 0 || img.height <= 0 { - 131●
raise @types.LoadError::DecodeFailed( - 132
"unrolled_pixel_sum: 图像尺寸必须大于0", - 133
) - 134
} - 135
if img.channels < 1 || img.channels > 4 { - 136○
raise @types.LoadError::DecodeFailed("unrolled_pixel_sum: 通道数非法") - 137
} - 138
let expected_size = img.width * img.height * img.channels - 139●
if img.data.length() != expected_size { - 140○
raise @types.LoadError::DecodeFailed( - 141
"unrolled_pixel_sum: 数据大小不匹配", - 142
) - 143
} - 144
let mut total = 0 - 145
let n = expected_size - 146
let unrolled_end = n - n % 4 - 147
let mut i = 0 - 148
while i < unrolled_end { - 149●
total += img.data[i].to_int() - 150●
total += img.data[i + 1].to_int() - 151●
total += img.data[i + 2].to_int() - 152●
total += img.data[i + 3].to_int() - 153
i += 4 - 154
} - 155
while i < n { - 156○
total += img.data[i].to_int() - 157
i += 1 - 158
} - 159
total - 160
} - 161
- 162
///| - 163
/// 快速连通域标记(两遍扫描法优化版) - 164
pub fn fast_connected_components( - 165
binary_img : @types.Image, - 166
) -> Array[Int32] raise @types.LoadError { - 167●
if binary_img.width <= 0 || binary_img.height <= 0 { - 168●
raise @types.LoadError::DecodeFailed( - 169
"fast_connected_components: 图像尺寸必须大于0", - 170
) - 171
} - 172
if binary_img.channels < 1 || binary_img.channels > 4 { - 173○
raise @types.LoadError::DecodeFailed( - 174
"fast_connected_components: 通道数非法", - 175
) - 176
} - 177
let w = binary_img.width - 178
let h = binary_img.height - 179
let expected_size = w * h * binary_img.channels - 180●
if binary_img.data.length() != expected_size { - 181○
raise @types.LoadError::DecodeFailed( - 182
"fast_connected_components: 数据大小不匹配", - 183
) - 184
} - 185●
let labels : Array[Int32] = Array::make(w * h, 0) - 186
let mut current_label = 0 - 187
for y in 0..<h { - 188●
for x in 0..<w { - 189●
let idx = y * w + x - 190●
let pixel = binary_img.data[idx * binary_img.channels].to_int() - 191
if pixel > 128 && labels[idx] == 0 { - 192○
current_label += 1 - 193
labels[idx] = current_label - 194
} - 195
} - 196
} - 197
labels - 198
} - 199
- 200
///| - 201
/// 预计算高斯核查找表 - 202
pub fn build_gaussian_kernel_lut( - 203
sigma : Double, - 204
radius : Int32, - 205
) -> Array[Double] raise @types.LoadError { - 206●
if sigma <= 0.0 { - 207●
raise @types.LoadError::DecodeFailed( - 208
"build_gaussian_kernel_lut: sigma必须大于0", - 209
) - 210
} - 211
if radius <= 0 { - 212●
raise @types.LoadError::DecodeFailed( - 213
"build_gaussian_kernel_lut: 半径必须大于0", - 214
) - 215
} - 216
let size = 2 * radius + 1 - 217●
let kernel : Array[Double] = Array::make(size, 0.0) - 218
let two_sigma_sq = 2.0 * sigma * sigma - 219
let mut sum = 0.0 - 220
for i in 0..<size { - 221●
let x = (i - radius).to_double() - 222●
let val = @math.exp(-x * x / two_sigma_sq) - 223
kernel[i] = val - 224
sum += val - 225
} - 226
for i in 0..<size { - 227●
kernel[i] = kernel[i] / sum - 228
} - 229
kernel - 230
} - 231
- 232
///| - 233
/// 使用预计算核快速高斯模糊(1D 分离卷积) - 234
pub fn fast_gaussian_blur_1d( - 235
img : @types.Image, - 236
kernel : Array[Double], - 237
) -> @types.Image raise @types.LoadError { - 238●
if img.width <= 0 || img.height <= 0 { - 239●
raise @types.LoadError::DecodeFailed( - 240
"fast_gaussian_blur_1d: 图像尺寸必须大于0", - 241
) - 242
} - 243
if img.channels < 1 || img.channels > 4 { - 244○
raise @types.LoadError::DecodeFailed( - 245
"fast_gaussian_blur_1d: 通道数非法", - 246
) - 247
} - 248
let w = img.width - 249
let h = img.height - 250
let c = img.channels - 251
let expected_size = w * h * c - 252●
if img.data.length() != expected_size { - 253○
raise @types.LoadError::DecodeFailed( - 254
"fast_gaussian_blur_1d: 数据大小不匹配", - 255
) - 256
} - 257●
let radius = (kernel.length() - 1) / 2 - 258●
let result_data : Array[Byte] = Array::make(expected_size, b'\x00') - 259
for y in 0..<h { - 260●
for x in 0..<w { - 261●
for ch in 0..<c { - 262●
let mut sum = 0.0 - 263●
for k in 0..<kernel.length() { - 264●
let sx = x + k - radius - 265●
let clamped_x = if sx < 0 { 0 } else if sx >= w { w - 1 } else { sx } - 266
let idx = (y * w + clamped_x) * c + ch - 267●
sum += img.data[idx].to_int().to_double() * kernel[k] - 268
} - 269
let val = if sum > 255.0 { - 270○
255 - 271●
} else if sum < 0.0 { - 272○
0 - 273
} else { - 274●
sum.to_int() - 275
} - 276●
result_data[(y * w + x) * c + ch] = val.to_byte() - 277
} - 278
} - 279
} - 280
@types.Image::{ - 281
width: w, - 282
height: h, - 283
channels: c, - 284●
data: Bytes::from_array(result_data), - 285
} - 286
} - 287
- 288
///| - 289
/// 批量字符识别优化(减少重复计算) - 290
pub fn batch_recognize_chars( - 291
images : Array[@types.Image], - 292
config : DLCnnModelConfig, - 293
labels : Array[String], - 294
) -> Array[DLInferenceResult] raise @types.LoadError { - 295●
if images.length() == 0 { - 296●
raise @types.LoadError::DecodeFailed( - 297
"batch_recognize_chars: 图像列表不能为空", - 298
) - 299
} - 300●
if labels.length() != config.num_classes { - 301○
raise @types.LoadError::DecodeFailed( - 302
"batch_recognize_chars: 标签数量不匹配", - 303
) - 304
} - 305
let results : Array[DLInferenceResult] = [] - 306●
for i in 0..<images.length() { - 307●
results.push(cnn_recognize_char(images[i], config, labels)) - 308
} - 309
results - 310
} - 311
- 312
///| - 313
/// OCR 流水线性能统计 - 314
pub fn ocr_pipeline_stats( - 315
img : @types.Image, - 316
) -> (Int32, Int32, Double) raise @types.LoadError { - 317●
if img.width <= 0 || img.height <= 0 { - 318●
raise @types.LoadError::DecodeFailed( - 319
"ocr_pipeline_stats: 图像尺寸必须大于0", - 320
) - 321
} - 322
if img.channels < 1 || img.channels > 4 { - 323○
raise @types.LoadError::DecodeFailed("ocr_pipeline_stats: 通道数非法") - 324
} - 325
let expected_size = img.width * img.height * img.channels - 326●
if img.data.length() != expected_size { - 327○
raise @types.LoadError::DecodeFailed( - 328
"ocr_pipeline_stats: 数据大小不匹配", - 329
) - 330
} - 331
let pixel_count = img.width * img.height - 332
let byte_count = expected_size - 333●
let avg_pixel = unrolled_pixel_sum(img).to_double() / pixel_count.to_double() - 334
(pixel_count, byte_count, avg_pixel) - 335
} - 336
- 337
///| - 338
/// 内存优化的图像降采样(2x2 均值采样) - 339
pub fn fast_downsample_2x( - 340
img : @types.Image, - 341
) -> @types.Image raise @types.LoadError { - 342●
if img.width <= 1 || img.height <= 1 { - 343●
raise @types.LoadError::DecodeFailed( - 344
"fast_downsample_2x: 图像尺寸必须大于1", - 345
) - 346
} - 347
if img.channels < 1 || img.channels > 4 { - 348○
raise @types.LoadError::DecodeFailed("fast_downsample_2x: 通道数非法") - 349
} - 350
let w = img.width - 351
let h = img.height - 352
let c = img.channels - 353
let expected_size = w * h * c - 354●
if img.data.length() != expected_size { - 355○
raise @types.LoadError::DecodeFailed( - 356
"fast_downsample_2x: 数据大小不匹配", - 357
) - 358
} - 359
let new_w = w / 2 - 360
let new_h = h / 2 - 361●
let result_data : Array[Byte] = Array::make(new_w * new_h * c, b'\x00') - 362
for y in 0..<new_h { - 363●
for x in 0..<new_w { - 364●
for ch in 0..<c { - 365●
let p00 = img.data[(y * 2 * w + x * 2) * c + ch].to_int() - 366●
let p01 = img.data[(y * 2 * w + x * 2 + 1) * c + ch].to_int() - 367●
let p10 = img.data[((y * 2 + 1) * w + x * 2) * c + ch].to_int() - 368●
let p11 = img.data[((y * 2 + 1) * w + x * 2 + 1) * c + ch].to_int() - 369
let avg = (p00 + p01 + p10 + p11) / 4 - 370●
result_data[(y * new_w + x) * c + ch] = avg.to_byte() - 371
} - 372
} - 373
} - 374
@types.Image::{ - 375
width: new_w, - 376
height: new_h, - 377
channels: c, - 378●
data: Bytes::from_array(result_data), - 379
} - 380
}