image.photography\inpainting\inpainting_enhancement.mbt
86.2% · 137 / 159 points · 633 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 修复增强 inpainting/inpainting_enhancement - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 图像修复增强(v25.0.0) - 7
// 图像修复、图像去雾、图像去雨、图像去噪 - 8
- 9
///| - 10
/// 修复算法类型 - 11
/// `#non_exhaustive` 表示未来可能添加新构造器 - 12
#derive(Eq, @debug.Debug) - 13
#non_exhaustive - 14
pub(all) enum InpaintingAlgorithm { - 15
Telea - 16
NavierStokes - 17
FastMarching - 18
PatchMatch - 19
DeepLearning - 20
Unknown - 21
} - 22
- 23
///| - 24
pub extend InpaintingAlgorithm with @debug.Debug::{to_repr} - 25
- 26
///| - 27
pub extend InpaintingAlgorithm with Eq::{not_equal, equal} - 28
- 29
///| - 30
/// 修复结果 - 31
#derive(Eq, @debug.Debug) - 32
pub(all) struct InpaintingResult { - 33
image : @types.Image - 34
mask : @types.Image - 35
algorithm : InpaintingAlgorithm - 36
inpainted_pixels : Int32 - 37
total_mask_pixels : Int32 - 38
processing_time_ms : Double - 39
iterations : Int32 - 40
} - 41
- 42
///| - 43
pub extend InpaintingResult with Eq::{not_equal, equal} - 44
- 45
///| - 46
pub extend InpaintingResult with @debug.Debug::{to_repr} - 47
- 48
///| - 49
/// 修复配置 - 50
#derive(Eq, @debug.Debug) - 51
pub(all) struct InpaintingConfig { - 52
algorithm : InpaintingAlgorithm - 53
radius : Int32 - 54
iterations : Int32 - 55
use_edge_preservation : Bool - 56
tolerance : Double - 57
} - 58
- 59
///| - 60
pub extend InpaintingConfig with Eq::{not_equal, equal} - 61
- 62
///| - 63
pub extend InpaintingConfig with @debug.Debug::{to_repr} - 64
- 65
///| - 66
/// 去雾算法类型 - 67
/// `#non_exhaustive` 表示未来可能添加新构造器 - 68
#derive(Eq, @debug.Debug) - 69
#non_exhaustive - 70
pub(all) enum DehazeAlgorithm { - 71
DarkChannelPrior - 72
ColorAttenuation - 73
HomomorphicFiltering - 74
Retinex - 75
CLAHE - 76
Unknown - 77
} - 78
- 79
///| - 80
pub extend DehazeAlgorithm with @debug.Debug::{to_repr} - 81
- 82
///| - 83
pub extend DehazeAlgorithm with Eq::{not_equal, equal} - 84
- 85
///| - 86
/// 去雾结果 - 87
#derive(Eq, @debug.Debug) - 88
pub(all) struct DehazeResult { - 89
image : @types.Image - 90
algorithm : DehazeAlgorithm - 91
atmospheric_light : Double - 92
transmission_map : Array[Double] - 93
processing_time_ms : Double - 94
} - 95
- 96
///| - 97
pub extend DehazeResult with Eq::{not_equal, equal} - 98
- 99
///| - 100
pub extend DehazeResult with @debug.Debug::{to_repr} - 101
- 102
///| - 103
/// 去雨算法类型 - 104
/// `#non_exhaustive` 表示未来可能添加新构造器 - 105
#derive(Eq, @debug.Debug) - 106
#non_exhaustive - 107
pub(all) enum DerainAlgorithm { - 108
DictionaryLearning - 109
ConvolutionalNeuralNetwork - 110
GAN - 111
Morphological - 112
Unknown - 113
} - 114
- 115
///| - 116
pub extend DerainAlgorithm with @debug.Debug::{to_repr} - 117
- 118
///| - 119
pub extend DerainAlgorithm with Eq::{not_equal, equal} - 120
- 121
///| - 122
/// 去雨结果 - 123
#derive(Eq, @debug.Debug) - 124
pub(all) struct DerainResult { - 125
image : @types.Image - 126
algorithm : DerainAlgorithm - 127
rain_streaks_removed : Int32 - 128
processing_time_ms : Double - 129
} - 130
- 131
///| - 132
pub extend DerainResult with Eq::{not_equal, equal} - 133
- 134
///| - 135
pub extend DerainResult with @debug.Debug::{to_repr} - 136
- 137
///| - 138
/// 创建默认修复配置 - 139
pub fn create_default_inpainting_config() -> InpaintingConfig { - 140●
InpaintingConfig::{ - 141
algorithm: InpaintingAlgorithm::Telea, - 142
radius: 3, - 143
iterations: 10, - 144
use_edge_preservation: true, - 145
tolerance: 0.1, - 146
} - 147
} - 148
- 149
///| - 150
/// 创建修复掩膜(矩形区域) - 151
pub fn create_rect_mask( - 152
width : Int32, - 153
height : Int32, - 154
x : Int32, - 155
y : Int32, - 156
rect_width : Int32, - 157
rect_height : Int32, - 158
) -> @types.Image { - 159●
let total = width * height - 160
let mask_data : Array[Byte] = [] - 161
for _ in 0..<total { - 162●
mask_data.push(b'\x00') - 163
} - 164
- 165
for my in 0..<rect_height { - 166●
for mx in 0..<rect_width { - 167●
let px = x + mx - 168
let py = y + my - 169
if px >= 0 && px < width && py >= 0 && py < height { - 170●
mask_data[py * width + px] = b'\xFF' - 171
} - 172
} - 173
} - 174
- 175
@types.Image::{ - 176
width, - 177
height, - 178
channels: 1, - 179●
data: Bytes::from_array(mask_data), - 180
} - 181
} - 182
- 183
///| - 184
/// 简单修复(邻域平均法) - 185
pub fn inpaint_simple( - 186
img : @types.Image, - 187
mask : @types.Image, - 188
radius : Int32, - 189
) -> @types.Image { - 190●
let w = img.width - 191
let h = img.height - 192
let total = w * h - 193
let out : Array[Byte] = [] - 194
- 195
for i in 0..<total { - 196●
out.push(img.data[i]) - 197
} - 198
- 199
// 迭代修复 - 200
for _ in 0..<5 { - 201●
for y in 0..<h { - 202●
for x in 0..<w { - 203●
let idx = y * w + x - 204
if mask.data[idx] == b'\xFF' { - 205
// 计算邻域平均值 - 206●
let mut sum = 0.0 - 207
let mut count = 0 - 208
for dy in -radius..<(radius + 1) { - 209●
for dx in -radius..<(radius + 1) { - 210●
if dx == 0 && dy == 0 { - 211●
continue - 212
} - 213
let nx = x + dx - 214
let ny = y + dy - 215
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 216●
let nidx = ny * w + nx - 217
if mask.data[nidx] == b'\x00' || out[nidx] != img.data[nidx] { - 218●
sum += out[nidx].to_double() - 219
count += 1 - 220
} - 221
} - 222
} - 223
} - 224
if count > 0 { - 225●
let avg = sum / count.to_double() - 226
let clamped = if avg < 0.0 { - 227○
0.0 - 228●
} else if avg > 255.0 { - 229○
255.0 - 230
} else { - 231●
avg - 232
} - 233●
out[idx] = clamped.to_int().to_byte() - 234
} - 235
} - 236
} - 237
} - 238
} - 239
- 240
@types.Image::{ - 241
width: w, - 242
height: h, - 243
channels: img.channels, - 244●
data: Bytes::from_array(out), - 245
} - 246
} - 247
- 248
///| - 249
/// 基于扩散的修复(简化版 Telea) - 250
pub fn inpaint_diffusion( - 251
img : @types.Image, - 252
mask : @types.Image, - 253
radius : Int32, - 254
) -> @types.Image { - 255●
let w = img.width - 256
let h = img.height - 257
let total = w * h - 258
let out : Array[Byte] = [] - 259
- 260
for i in 0..<total { - 261●
out.push(img.data[i]) - 262
} - 263
- 264
// 从边界向内部扩散 - 265
for _ in 0..<10 { - 266●
for y in 0..<h { - 267●
for x in 0..<w { - 268●
let idx = y * w + x - 269
if mask.data[idx] == b'\xFF' { - 270
// 检查是否有已修复的邻域 - 271●
let mut has_known = false - 272
for dy in -1..<2 { - 273●
for dx in -1..<2 { - 274●
if dx == 0 && dy == 0 { - 275●
continue - 276
} - 277
let nx = x + dx - 278
let ny = y + dy - 279
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 280●
let nidx = ny * w + nx - 281
if mask.data[nidx] == b'\x00' || out[nidx] != img.data[nidx] { - 282●
has_known = true - 283
} - 284
} - 285
} - 286
} - 287
- 288
if has_known { - 289
// 加权平均(距离加权) - 290●
let mut sum = 0.0 - 291
let mut weight_sum = 0.0 - 292
for dy in -radius..<(radius + 1) { - 293●
for dx in -radius..<(radius + 1) { - 294●
if dx == 0 && dy == 0 { - 295●
continue - 296
} - 297
let nx = x + dx - 298
let ny = y + dy - 299
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 300●
let nidx = ny * w + nx - 301
if mask.data[nidx] == b'\x00' || out[nidx] != img.data[nidx] { - 302●
let dist = (dx * dx + dy * dy).to_double().sqrt() - 303
let weight = 1.0 / (1.0 + dist) - 304●
sum += out[nidx].to_double() * weight - 305
weight_sum += weight - 306
} - 307
} - 308
} - 309
} - 310
if weight_sum > 0.0 { - 311●
let avg = sum / weight_sum - 312
let clamped = if avg < 0.0 { - 313○
0.0 - 314●
} else if avg > 255.0 { - 315○
255.0 - 316
} else { - 317●
avg - 318
} - 319●
out[idx] = clamped.to_int().to_byte() - 320
} - 321
} - 322
} - 323
} - 324
} - 325
} - 326
- 327
@types.Image::{ - 328
width: w, - 329
height: h, - 330
channels: img.channels, - 331●
data: Bytes::from_array(out), - 332
} - 333
} - 334
- 335
///| - 336
/// 图像修复 - 337
pub fn inpaint( - 338
img : @types.Image, - 339
mask : @types.Image, - 340
config : InpaintingConfig, - 341
) -> InpaintingResult { - 342●
let result = if config.algorithm == InpaintingAlgorithm::Telea { - 343●
inpaint_diffusion(img, mask, config.radius) - 344
} else { - 345●
inpaint_simple(img, mask, config.radius) - 346
} - 347
- 348
// 统计掩膜像素数 - 349
let total = mask.width * mask.height - 350
let mut mask_pixels = 0 - 351
for i in 0..<total { - 352●
if mask.data[i] == b'\xFF' { - 353●
mask_pixels += 1 - 354
} - 355
} - 356
- 357
InpaintingResult::{ - 358
image: result, - 359
mask, - 360
algorithm: config.algorithm, - 361
inpainted_pixels: mask_pixels, - 362
total_mask_pixels: mask_pixels, - 363
processing_time_ms: 0.0, - 364
iterations: config.iterations, - 365
} - 366
} - 367
- 368
///| - 369
/// 暗通道先验去雾(简化版) - 370
pub fn dehaze_dark_channel( - 371
img : @types.Image, - 372
omega : Double, - 373
t0 : Double, - 374
) -> DehazeResult { - 375●
let w = img.width - 376
let h = img.height - 377
let total = w * h - 378
let patch_size = 15 - 379
- 380
// 计算暗通道 - 381
let dark_channel : Array[Double] = [] - 382
for _ in 0..<total { - 383●
dark_channel.push(255.0) - 384
} - 385
- 386
for y in 0..<h { - 387●
for x in 0..<w { - 388●
let mut min_val = 255.0 - 389
for dy in 0..<patch_size { - 390●
for dx in 0..<patch_size { - 391●
let nx = x + dx - patch_size / 2 - 392
let ny = y + dy - patch_size / 2 - 393
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 394●
let val = img.data[ny * w + nx].to_double() - 395
if val < min_val { - 396●
min_val = val - 397
} - 398
} - 399
} - 400
} - 401
dark_channel[y * w + x] = min_val - 402
} - 403
} - 404
- 405
// 估计大气光(暗通道最亮的 0.1% 像素的平均值) - 406
let mut atmospheric_light = 0.0 - 407
let mut max_dark = 0.0 - 408
for i in 0..<total { - 409●
if dark_channel[i] > max_dark { - 410○
max_dark = dark_channel[i] - 411
} - 412
} - 413
let threshold = max_dark * 0.999 - 414
let mut count = 0 - 415
for i in 0..<total { - 416●
if dark_channel[i] >= threshold { - 417●
atmospheric_light += img.data[i].to_double() - 418
count += 1 - 419
} - 420
} - 421
if count > 0 { - 422●
atmospheric_light /= count.to_double() - 423
} - 424
- 425
// 计算透射率 - 426
let transmission : Array[Double] = [] - 427
for i in 0..<total { - 428●
let t = 1.0 - omega * (dark_channel[i] / atmospheric_light) - 429◐
let clamped = if t < t0 { t0 } else if t > 1.0 { 1.0 } else { t } - 430●
transmission.push(clamped) - 431
} - 432
- 433
// 去雾 - 434
let out : Array[Byte] = [] - 435
for i in 0..<total { - 436●
let t = transmission[i] - 437●
let pixel = img.data[i].to_double() - 438
let dehazed = (pixel - atmospheric_light) / t + atmospheric_light - 439
let clamped = if dehazed < 0.0 { - 440○
0.0 - 441●
} else if dehazed > 255.0 { - 442○
255.0 - 443
} else { - 444●
dehazed - 445
} - 446●
out.push(clamped.to_int().to_byte()) - 447
} - 448
- 449
DehazeResult::{ - 450
image: @types.Image::{ - 451
width: w, - 452
height: h, - 453
channels: img.channels, - 454●
data: Bytes::from_array(out), - 455
}, - 456
algorithm: DehazeAlgorithm::DarkChannelPrior, - 457
atmospheric_light, - 458
transmission_map: transmission, - 459
processing_time_ms: 0.0, - 460
} - 461
} - 462
- 463
///| - 464
/// CLAHE 去雾(对比度受限自适应直方图均衡化) - 465
pub fn dehaze_clahe( - 466
img : @types.Image, - 467
clip_limit : Double, - 468
tile_size : Int32, - 469
) -> @types.Image { - 470●
ignore(tile_size) - 471●
ignore(clip_limit) - 472
let w = img.width - 473
let h = img.height - 474
let total = w * h - 475
- 476
// 简化:全局直方图均衡化 - 477
let histogram : Array[Int32] = [] - 478
for _ in 0..<256 { - 479●
histogram.push(0) - 480
} - 481
for i in 0..<total { - 482●
histogram[img.data[i].to_int()] += 1 - 483
} - 484
- 485
// 累积分布函数 - 486
let cdf : Array[Double] = [] - 487●
let mut cdf_min = total.to_double() - 488
let mut sum = 0.0 - 489
for i in 0..<256 { - 490●
sum += histogram[i].to_double() - 491●
cdf.push(sum) - 492
if histogram[i] > 0 && sum < cdf_min { - 493●
cdf_min = sum - 494
} - 495
} - 496
- 497
// 直方图均衡化 - 498
let out : Array[Byte] = [] - 499
for i in 0..<total { - 500●
let pixel = img.data[i].to_int() - 501
let equalized = if total > 1 { - 502●
((cdf[pixel] - cdf_min) / (total.to_double() - cdf_min) * 255.0).to_int() - 503
} else { - 504○
pixel - 505
} - 506
let clamped = if equalized < 0 { - 507○
0 - 508●
} else if equalized > 255 { - 509○
255 - 510
} else { - 511●
equalized - 512
} - 513●
out.push(clamped.to_byte()) - 514
} - 515
- 516
@types.Image::{ - 517
width: w, - 518
height: h, - 519
channels: img.channels, - 520●
data: Bytes::from_array(out), - 521
} - 522
} - 523
- 524
///| - 525
/// 去雾 - 526
pub fn dehaze(img : @types.Image, algorithm : DehazeAlgorithm) -> DehazeResult { - 527●
if algorithm == DehazeAlgorithm::DarkChannelPrior { - 528●
dehaze_dark_channel(img, 0.95, 0.1) - 529
} else { - 530●
let result = dehaze_clahe(img, 2.0, 8) - 531
DehazeResult::{ - 532
image: result, - 533
algorithm, - 534
atmospheric_light: 0.0, - 535
transmission_map: [], - 536
processing_time_ms: 0.0, - 537
} - 538
} - 539
} - 540
- 541
///| - 542
/// 形态学去雨(简化版) - 543
pub fn derain_morphological(img : @types.Image) -> DerainResult { - 544●
let w = img.width - 545
let h = img.height - 546
let _ = w * h - 547
- 548
// 简化:中值滤波去除雨线 - 549
let out : Array[Byte] = [] - 550
for y in 0..<h { - 551●
for x in 0..<w { - 552
// 3x3 中值滤波 - 553●
let neighbors : Array[Int32] = [] - 554
for dy in -1..<2 { - 555●
for dx in -1..<2 { - 556●
let nx = x + dx - 557
let ny = y + dy - 558
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 559●
neighbors.push(img.data[ny * w + nx].to_int()) - 560
} - 561
} - 562
} - 563
// 排序取中值 - 564●
for i in 0..<neighbors.length() { - 565●
for j in 0..<(neighbors.length() - i - 1) { - 566●
if neighbors[j] > neighbors[j + 1] { - 567○
let temp = neighbors[j] - 568
neighbors[j] = neighbors[j + 1] - 569
neighbors[j + 1] = temp - 570
} - 571
} - 572
} - 573●
let median = neighbors[neighbors.length() / 2] - 574●
out.push(median.to_byte()) - 575
} - 576
} - 577
- 578
DerainResult::{ - 579
image: @types.Image::{ - 580
width: w, - 581
height: h, - 582
channels: img.channels, - 583●
data: Bytes::from_array(out), - 584
}, - 585
algorithm: DerainAlgorithm::Morphological, - 586
rain_streaks_removed: 0, - 587
processing_time_ms: 0.0, - 588
} - 589
} - 590
- 591
///| - 592
/// 去雨 - 593
pub fn derain(img : @types.Image, algorithm : DerainAlgorithm) -> DerainResult { - 594●
ignore(algorithm) - 595●
derain_morphological(img) - 596
} - 597
- 598
///| - 599
/// 生成修复报告 - 600
pub fn inpainting_report_to_string(result : InpaintingResult) -> String { - 601●
let mut report = "## 图像修复报告\n\n" - 602
report = report + "### 处理信息\n\n" - 603
report = report + "| 指标 | 值 |\n|------|------|\n" - 604
let algo_str = if result.algorithm == InpaintingAlgorithm::Telea { - 605●
"Telea" - 606○
} else if result.algorithm == InpaintingAlgorithm::NavierStokes { - 607○
"Navier-Stokes" - 608○
} else if result.algorithm == InpaintingAlgorithm::FastMarching { - 609○
"快速行进" - 610○
} else if result.algorithm == InpaintingAlgorithm::PatchMatch { - 611○
"PatchMatch" - 612○
} else if result.algorithm == InpaintingAlgorithm::DeepLearning { - 613○
"深度学习" - 614
} else { - 615○
"未知" - 616
} - 617
report = report + "| 算法 | " + algo_str + " |\n" - 618
report = report + - 619
"| 修复像素数 | " + - 620●
result.inpainted_pixels.to_string() + - 621
" |\n" - 622
report = report + - 623
"| 掩膜像素总数 | " + - 624●
result.total_mask_pixels.to_string() + - 625
" |\n" - 626●
report = report + "| 迭代次数 | " + result.iterations.to_string() + " |\n" - 627
report = report + - 628
"| 处理耗时 | " + - 629●
result.processing_time_ms.to_string() + - 630
"ms |\n" - 631
- 632
report - 633
}