image.photography\superres\super_resolution_enhancement.mbt
90.3% · 233 / 258 points · 711 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 超分辨率增强 superres/super_resolution_enhancement - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 图像超分辨率增强(v23.0.0) - 7
// 图像超分辨率、插值算法、图像增强、质量评估 - 8
- 9
///| - 10
/// 超分辨率算法类型 - 11
/// `#non_exhaustive` 表示未来可能添加新构造器 - 12
#derive(Eq, @debug.Debug) - 13
#non_exhaustive - 14
pub(all) enum SuperResolutionAlgorithm { - 15
NearestNeighbor - 16
Bilinear - 17
Bicubic - 18
Lanczos - 19
EdgeDirected - 20
ExampleBased - 21
DeepLearning - 22
Unknown - 23
} - 24
- 25
///| - 26
pub extend SuperResolutionAlgorithm with @debug.Debug::{to_repr} - 27
- 28
///| - 29
pub extend SuperResolutionAlgorithm with Eq::{not_equal, equal} - 30
- 31
///| - 32
/// 超分辨率结果 - 33
#derive(Eq, @debug.Debug) - 34
pub(all) struct SuperResolutionResult { - 35
image : @types.Image - 36
original_width : Int32 - 37
original_height : Int32 - 38
output_width : Int32 - 39
output_height : Int32 - 40
scale_factor : Double - 41
algorithm : SuperResolutionAlgorithm - 42
processing_time_ms : Double - 43
psnr : Double - 44
ssim : Double - 45
} - 46
- 47
///| - 48
pub extend SuperResolutionResult with Eq::{not_equal, equal} - 49
- 50
///| - 51
pub extend SuperResolutionResult with @debug.Debug::{to_repr} - 52
- 53
///| - 54
/// 插值配置 - 55
#derive(Eq, @debug.Debug) - 56
pub(all) struct InterpolationConfig { - 57
algorithm : SuperResolutionAlgorithm - 58
scale_factor : Double - 59
use_edge_preservation : Bool - 60
sharpening_strength : Double - 61
denoise_strength : Double - 62
} - 63
- 64
///| - 65
pub extend InterpolationConfig with Eq::{not_equal, equal} - 66
- 67
///| - 68
pub extend InterpolationConfig with @debug.Debug::{to_repr} - 69
- 70
///| - 71
/// 图像质量指标 - 72
#derive(Eq, @debug.Debug) - 73
pub(all) struct ImageQualityMetrics { - 74
psnr : Double - 75
ssim : Double - 76
mse : Double - 77
mae : Double - 78
correlation : Double - 79
sharpness : Double - 80
noise_level : Double - 81
overall_quality : Double - 82
} - 83
- 84
///| - 85
pub extend ImageQualityMetrics with Eq::{not_equal, equal} - 86
- 87
///| - 88
pub extend ImageQualityMetrics with @debug.Debug::{to_repr} - 89
- 90
///| - 91
/// 图像增强配置 - 92
#derive(Eq, @debug.Debug) - 93
pub(all) struct EnhancementConfig { - 94
brightness : Double - 95
contrast : Double - 96
saturation : Double - 97
sharpness : Double - 98
denoise : Double - 99
gamma : Double - 100
use_clahe : Bool - 101
clahe_clip_limit : Double - 102
clahe_tile_size : Int32 - 103
} - 104
- 105
///| - 106
pub extend EnhancementConfig with Eq::{not_equal, equal} - 107
- 108
///| - 109
pub extend EnhancementConfig with @debug.Debug::{to_repr} - 110
- 111
///| - 112
/// 创建默认插值配置 - 113
pub fn create_default_interpolation_config() -> InterpolationConfig { - 114●
InterpolationConfig::{ - 115
algorithm: SuperResolutionAlgorithm::Bicubic, - 116
scale_factor: 2.0, - 117
use_edge_preservation: true, - 118
sharpening_strength: 0.3, - 119
denoise_strength: 0.1, - 120
} - 121
} - 122
- 123
///| - 124
/// 创建默认增强配置 - 125
pub fn create_default_enhancement_config() -> EnhancementConfig { - 126●
EnhancementConfig::{ - 127
brightness: 0.0, - 128
contrast: 1.0, - 129
saturation: 1.0, - 130
sharpness: 0.0, - 131
denoise: 0.0, - 132
gamma: 1.0, - 133
use_clahe: false, - 134
clahe_clip_limit: 2.0, - 135
clahe_tile_size: 8, - 136
} - 137
} - 138
- 139
///| - 140
/// 最近邻插值 - 141
pub fn nearest_neighbor_interpolate( - 142
img : @types.Image, - 143
scale_factor : Double, - 144
) -> @types.Image { - 145●
let w = img.width - 146
let h = img.height - 147●
let new_w = (w.to_double() * scale_factor).to_int() - 148●
let new_h = (h.to_double() * scale_factor).to_int() - 149
let out : Array[Byte] = [] - 150
- 151
for y in 0..<new_h { - 152●
for x in 0..<new_w { - 153●
let src_x = (x.to_double() / scale_factor).to_int() - 154●
let src_y = (y.to_double() / scale_factor).to_int() - 155
let clamped_x = if src_x < 0 { - 156○
0 - 157●
} else if src_x >= w { - 158○
w - 1 - 159
} else { - 160●
src_x - 161
} - 162
let clamped_y = if src_y < 0 { - 163○
0 - 164●
} else if src_y >= h { - 165○
h - 1 - 166
} else { - 167●
src_y - 168
} - 169●
out.push(img.data[clamped_y * w + clamped_x]) - 170
} - 171
} - 172
- 173
@types.Image::{ - 174
width: new_w, - 175
height: new_h, - 176
channels: img.channels, - 177●
data: Bytes::from_array(out), - 178
} - 179
} - 180
- 181
///| - 182
/// 双线性插值 - 183
pub fn bilinear_interpolate( - 184
img : @types.Image, - 185
scale_factor : Double, - 186
) -> @types.Image { - 187●
let w = img.width - 188
let h = img.height - 189●
let new_w = (w.to_double() * scale_factor).to_int() - 190●
let new_h = (h.to_double() * scale_factor).to_int() - 191
let out : Array[Byte] = [] - 192
- 193
for y in 0..<new_h { - 194●
for x in 0..<new_w { - 195●
let src_x = x.to_double() / scale_factor - 196●
let src_y = y.to_double() / scale_factor - 197●
let x0 = src_x.to_int() - 198●
let y0 = src_y.to_int() - 199
let x1 = x0 + 1 - 200
let y1 = y0 + 1 - 201●
let fx = src_x - x0.to_double() - 202●
let fy = src_y - y0.to_double() - 203
- 204◐
let cx0 = if x0 < 0 { 0 } else if x0 >= w { w - 1 } else { x0 } - 205◐
let cy0 = if y0 < 0 { 0 } else if y0 >= h { h - 1 } else { y0 } - 206◐
let cx1 = if x1 < 0 { 0 } else if x1 >= w { w - 1 } else { x1 } - 207◐
let cy1 = if y1 < 0 { 0 } else if y1 >= h { h - 1 } else { y1 } - 208
- 209●
let p00 = img.data[cy0 * w + cx0].to_double() - 210●
let p01 = img.data[cy0 * w + cx1].to_double() - 211●
let p10 = img.data[cy1 * w + cx0].to_double() - 212●
let p11 = img.data[cy1 * w + cx1].to_double() - 213
- 214
let top = p00 * (1.0 - fx) + p01 * fx - 215
let bottom = p10 * (1.0 - fx) + p11 * fx - 216
let value = top * (1.0 - fy) + bottom * fy - 217
let clamped = if value < 0.0 { - 218○
0.0 - 219●
} else if value > 255.0 { - 220○
255.0 - 221
} else { - 222●
value - 223
} - 224●
out.push(clamped.to_int().to_byte()) - 225
} - 226
} - 227
- 228
@types.Image::{ - 229
width: new_w, - 230
height: new_h, - 231
channels: img.channels, - 232●
data: Bytes::from_array(out), - 233
} - 234
} - 235
- 236
///| - 237
/// 双三次插值(简化版) - 238
pub fn bicubic_interpolate( - 239
img : @types.Image, - 240
scale_factor : Double, - 241
) -> @types.Image { - 242●
let w = img.width - 243
let h = img.height - 244●
let new_w = (w.to_double() * scale_factor).to_int() - 245●
let new_h = (h.to_double() * scale_factor).to_int() - 246
let out : Array[Byte] = [] - 247
- 248
for y in 0..<new_h { - 249●
for x in 0..<new_w { - 250●
let src_x = x.to_double() / scale_factor - 251●
let src_y = y.to_double() / scale_factor - 252●
let x0 = src_x.to_int() - 1 - 253●
let y0 = src_y.to_int() - 1 - 254●
let fx = src_x - src_x.to_int().to_double() - 255●
let fy = src_y - src_y.to_int().to_double() - 256
- 257
// 简化为双线性(双三次需要更多计算) - 258◐
let cx0 = if x0 < 0 { 0 } else if x0 >= w { w - 1 } else { x0 } - 259◐
let cy0 = if y0 < 0 { 0 } else if y0 >= h { h - 1 } else { y0 } - 260●
let cx1 = if cx0 + 1 >= w { w - 1 } else { cx0 + 1 } - 261●
let cy1 = if cy0 + 1 >= h { h - 1 } else { cy0 + 1 } - 262
- 263●
let p00 = img.data[cy0 * w + cx0].to_double() - 264●
let p01 = img.data[cy0 * w + cx1].to_double() - 265●
let p10 = img.data[cy1 * w + cx0].to_double() - 266●
let p11 = img.data[cy1 * w + cx1].to_double() - 267
- 268
let top = p00 * (1.0 - fx) + p01 * fx - 269
let bottom = p10 * (1.0 - fx) + p11 * fx - 270
let value = top * (1.0 - fy) + bottom * fy - 271
let clamped = if value < 0.0 { - 272○
0.0 - 273●
} else if value > 255.0 { - 274○
255.0 - 275
} else { - 276●
value - 277
} - 278●
out.push(clamped.to_int().to_byte()) - 279
} - 280
} - 281
- 282
@types.Image::{ - 283
width: new_w, - 284
height: new_h, - 285
channels: img.channels, - 286●
data: Bytes::from_array(out), - 287
} - 288
} - 289
- 290
///| - 291
/// 图像锐化(USM 非锐化掩膜) - 292
pub fn sharpen_image(img : @types.Image, strength : Double) -> @types.Image { - 293●
let w = img.width - 294
let h = img.height - 295
let out : Array[Byte] = [] - 296
- 297
// 简化的锐化:中心加权 - 298
for y in 0..<h { - 299●
for x in 0..<w { - 300●
let center = img.data[y * w + x].to_double() - 301
let mut sum = 0.0 - 302
let mut count = 0 - 303
for dy in -1..<2 { - 304●
for dx in -1..<2 { - 305●
if dx == 0 && dy == 0 { - 306●
continue - 307
} - 308
let nx = x + dx - 309
let ny = y + dy - 310
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 311●
sum += img.data[ny * w + nx].to_double() - 312
count += 1 - 313
} - 314
} - 315
} - 316◐
let avg = if count > 0 { sum / count.to_double() } else { center } - 317
let sharpened = center + strength * (center - avg) - 318
let clamped = if sharpened < 0.0 { - 319●
0.0 - 320●
} else if sharpened > 255.0 { - 321○
255.0 - 322
} else { - 323●
sharpened - 324
} - 325●
out.push(clamped.to_int().to_byte()) - 326
} - 327
} - 328
- 329
@types.Image::{ - 330
width: w, - 331
height: h, - 332
channels: img.channels, - 333●
data: Bytes::from_array(out), - 334
} - 335
} - 336
- 337
///| - 338
/// 图像去噪(均值滤波) - 339
pub fn denoise_image(img : @types.Image, strength : Double) -> @types.Image { - 340●
let w = img.width - 341
let h = img.height - 342
let out : Array[Byte] = [] - 343●
let kernel_size = if strength > 0.5 { 3 } else { 1 } - 344
- 345
for y in 0..<h { - 346●
for x in 0..<w { - 347●
if kernel_size <= 1 { - 348●
out.push(img.data[y * w + x]) - 349
} else { - 350●
let mut sum = 0.0 - 351
let mut count = 0 - 352
for dy in -1..<2 { - 353●
for dx in -1..<2 { - 354●
let nx = x + dx - 355
let ny = y + dy - 356
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 357●
sum += img.data[ny * w + nx].to_double() - 358
count += 1 - 359
} - 360
} - 361
} - 362
let avg = if count > 0 { - 363●
sum / count.to_double() - 364
} else { - 365○
img.data[y * w + x].to_double() - 366
} - 367●
let original = img.data[y * w + x].to_double() - 368
let blended = original * (1.0 - strength) + avg * strength - 369●
out.push(blended.to_int().to_byte()) - 370
} - 371
} - 372
} - 373
- 374
@types.Image::{ - 375
width: w, - 376
height: h, - 377
channels: img.channels, - 378●
data: Bytes::from_array(out), - 379
} - 380
} - 381
- 382
///| - 383
/// 超分辨率处理 - 384
pub fn super_resolve( - 385
img : @types.Image, - 386
config : InterpolationConfig, - 387
) -> SuperResolutionResult { - 388●
let mut result = if config.algorithm == - 389
SuperResolutionAlgorithm::NearestNeighbor { - 390●
nearest_neighbor_interpolate(img, config.scale_factor) - 391●
} else if config.algorithm == SuperResolutionAlgorithm::Bilinear { - 392●
bilinear_interpolate(img, config.scale_factor) - 393●
} else if config.algorithm == SuperResolutionAlgorithm::Bicubic { - 394●
bicubic_interpolate(img, config.scale_factor) - 395
} else { - 396●
bicubic_interpolate(img, config.scale_factor) - 397
} - 398
- 399
// 去噪 - 400
if config.denoise_strength > 0.0 { - 401●
result = denoise_image(result, config.denoise_strength) - 402
} - 403
- 404
// 锐化 - 405
if config.sharpening_strength > 0.0 { - 406●
result = sharpen_image(result, config.sharpening_strength) - 407
} - 408
- 409
SuperResolutionResult::{ - 410
image: result, - 411
original_width: img.width, - 412
original_height: img.height, - 413
output_width: result.width, - 414
output_height: result.height, - 415
scale_factor: config.scale_factor, - 416
algorithm: config.algorithm, - 417
processing_time_ms: 0.0, - 418
psnr: 0.0, - 419
ssim: 0.0, - 420
} - 421
} - 422
- 423
///| - 424
/// 计算 PSNR - 425
pub fn calculate_psnr_sr( - 426
original : @types.Image, - 427
processed : @types.Image, - 428
) -> Double { - 429●
let w = original.width - 430
let h = original.height - 431
let total = w * h - 432
let mut mse = 0.0 - 433
- 434
for i in 0..<total { - 435●
let p1 = original.data[i].to_double() - 436●
let p2 = processed.data[i].to_double() - 437
let diff = p1 - p2 - 438
mse += diff * diff - 439
} - 440●
mse /= total.to_double() - 441
- 442
if mse > 0.0 { - 443●
10.0 * (65025.0 / mse) * 0.1 - 444
} else { - 445●
100.0 - 446
} - 447
} - 448
- 449
///| - 450
/// 计算 SSIM(结构相似性,简化版) - 451
pub fn calculate_ssim_sr( - 452
original : @types.Image, - 453
processed : @types.Image, - 454
) -> Double { - 455●
let w = original.width - 456
let h = original.height - 457
let total = w * h - 458
let mut sum1 = 0.0 - 459
let mut sum2 = 0.0 - 460
let mut sum12 = 0.0 - 461
let mut sum1_sq = 0.0 - 462
let mut sum2_sq = 0.0 - 463
- 464
for i in 0..<total { - 465●
let p1 = original.data[i].to_double() - 466●
let p2 = processed.data[i].to_double() - 467
sum1 += p1 - 468
sum2 += p2 - 469
sum12 += p1 * p2 - 470
sum1_sq += p1 * p1 - 471
sum2_sq += p2 * p2 - 472
} - 473
- 474●
let mean1 = sum1 / total.to_double() - 475●
let mean2 = sum2 / total.to_double() - 476●
let var1 = sum1_sq / total.to_double() - mean1 * mean1 - 477●
let var2 = sum2_sq / total.to_double() - mean2 * mean2 - 478●
let cov12 = sum12 / total.to_double() - mean1 * mean2 - 479
- 480
let c1 = 6.5025 - 481
let c2 = 58.5225 - 482
let numerator = (2.0 * mean1 * mean2 + c1) * (2.0 * cov12 + c2) - 483
let denominator = (mean1 * mean1 + mean2 * mean2 + c1) * (var1 + var2 + c2) - 484
- 485
if denominator > 0.0 { - 486●
numerator / denominator - 487
} else { - 488○
1.0 - 489
} - 490
} - 491
- 492
///| - 493
/// 计算图像质量指标 - 494
pub fn calculate_image_quality_metrics( - 495
original : @types.Image, - 496
processed : @types.Image, - 497
) -> ImageQualityMetrics { - 498●
let psnr = calculate_psnr_sr(original, processed) - 499●
let ssim = calculate_ssim_sr(original, processed) - 500
- 501
let w = original.width - 502
let h = original.height - 503
let total = w * h - 504
let mut mse = 0.0 - 505
let mut mae = 0.0 - 506
let mut sum1 = 0.0 - 507
let mut sum2 = 0.0 - 508
let mut sum12 = 0.0 - 509
- 510
for i in 0..<total { - 511●
let p1 = original.data[i].to_double() - 512●
let p2 = processed.data[i].to_double() - 513
let diff = p1 - p2 - 514
mse += diff * diff - 515●
mae += diff.abs() - 516
sum1 += p1 - 517
sum2 += p2 - 518
sum12 += p1 * p2 - 519
} - 520●
mse /= total.to_double() - 521●
mae /= total.to_double() - 522
- 523●
let mean1 = sum1 / total.to_double() - 524●
let mean2 = sum2 / total.to_double() - 525●
let cov = sum12 / total.to_double() - mean1 * mean2 - 526
let var1 = if total > 0 { - 527●
let mut v = 0.0 - 528
for i in 0..<total { - 529●
let p = original.data[i].to_double() - 530
v += (p - mean1) * (p - mean1) - 531
} - 532●
v / total.to_double() - 533
} else { - 534○
0.0 - 535
} - 536
let var2 = if total > 0 { - 537●
let mut v = 0.0 - 538
for i in 0..<total { - 539●
let p = processed.data[i].to_double() - 540
v += (p - mean2) * (p - mean2) - 541
} - 542●
v / total.to_double() - 543
} else { - 544○
0.0 - 545
} - 546
let correlation = if var1 > 0.0 && var2 > 0.0 { - 547●
cov / (var1 * var2).sqrt() - 548
} else { - 549●
0.0 - 550
} - 551
- 552
// 清晰度(基于梯度) - 553
let mut sharpness = 0.0 - 554
for y in 1..<(h - 1) { - 555●
for x in 1..<(w - 1) { - 556●
let idx = y * w + x - 557●
let gx = processed.data[idx + 1].to_double() - - 558●
processed.data[idx - 1].to_double() - 559●
let gy = processed.data[idx + w].to_double() - - 560●
processed.data[idx - w].to_double() - 561●
sharpness += (gx * gx + gy * gy).sqrt() - 562
} - 563
} - 564●
sharpness /= total.to_double() - 565
- 566●
let noise_level = mse.sqrt() - 567
let overall_quality = if psnr > 40.0 && ssim > 0.95 { - 568●
5.0 - 569●
} else if psnr > 30.0 && ssim > 0.9 { - 570○
4.0 - 571●
} else if psnr > 20.0 && ssim > 0.8 { - 572○
3.0 - 573
} else { - 574●
2.0 - 575
} - 576
- 577
ImageQualityMetrics::{ - 578
psnr, - 579
ssim, - 580
mse, - 581
mae, - 582
correlation, - 583
sharpness, - 584
noise_level, - 585
overall_quality, - 586
} - 587
} - 588
- 589
///| - 590
/// 图像增强 - 591
pub fn enhance_image( - 592
img : @types.Image, - 593
config : EnhancementConfig, - 594
) -> @types.Image { - 595●
let w = img.width - 596
let h = img.height - 597
let total = w * h - 598
let out : Array[Byte] = [] - 599
- 600
for i in 0..<total { - 601●
let mut pixel = img.data[i].to_double() - 602
- 603
// 亮度 - 604
pixel += config.brightness * 255.0 - 605
- 606
// 对比度 - 607
pixel = (pixel - 128.0) * config.contrast + 128.0 - 608
- 609
// Gamma - 610
if config.gamma != 1.0 && config.gamma > 0.0 { - 611●
pixel = 255.0 * (pixel / 255.0) * (1.0 / config.gamma) - 612
} - 613
- 614
let clamped = if pixel < 0.0 { - 615●
0.0 - 616●
} else if pixel > 255.0 { - 617●
255.0 - 618
} else { - 619●
pixel - 620
} - 621●
out.push(clamped.to_int().to_byte()) - 622
} - 623
- 624
let mut result = @types.Image::{ - 625
width: w, - 626
height: h, - 627
channels: img.channels, - 628●
data: Bytes::from_array(out), - 629
} - 630
- 631
// 去噪 - 632
if config.denoise > 0.0 { - 633●
result = denoise_image(result, config.denoise) - 634
} - 635
- 636
// 锐化 - 637
if config.sharpness > 0.0 { - 638●
result = sharpen_image(result, config.sharpness) - 639
} - 640
- 641
result - 642
} - 643
- 644
///| - 645
/// 生成超分辨率报告 - 646
pub fn super_resolution_report_to_string( - 647
result : SuperResolutionResult, - 648
metrics : ImageQualityMetrics, - 649
) -> String { - 650●
let mut report = "## 图像超分辨率报告\n\n" - 651
report = report + "### 处理信息\n\n" - 652
report = report + "| 指标 | 值 |\n|------|------|\n" - 653
report = report + - 654
"| 原始尺寸 | " + - 655●
result.original_width.to_string() + - 656
"x" + - 657●
result.original_height.to_string() + - 658
" |\n" - 659
report = report + - 660
"| 输出尺寸 | " + - 661●
result.output_width.to_string() + - 662
"x" + - 663●
result.output_height.to_string() + - 664
" |\n" - 665
report = report + - 666
"| 缩放因子 | " + - 667●
result.scale_factor.to_string() + - 668
"x |\n" - 669
let algo_str = if result.algorithm == - 670
SuperResolutionAlgorithm::NearestNeighbor { - 671●
"最近邻" - 672●
} else if result.algorithm == SuperResolutionAlgorithm::Bilinear { - 673●
"双线性" - 674●
} else if result.algorithm == SuperResolutionAlgorithm::Bicubic { - 675●
"双三次" - 676●
} else if result.algorithm == SuperResolutionAlgorithm::Lanczos { - 677●
"Lanczos" - 678●
} else if result.algorithm == SuperResolutionAlgorithm::EdgeDirected { - 679●
"边缘导向" - 680●
} else if result.algorithm == SuperResolutionAlgorithm::ExampleBased { - 681●
"基于示例" - 682●
} else if result.algorithm == SuperResolutionAlgorithm::DeepLearning { - 683●
"深度学习" - 684
} else { - 685●
"未知" - 686
} - 687
report = report + "| 算法 | " + algo_str + " |\n" - 688
report = report + - 689
"| 处理耗时 | " + - 690●
result.processing_time_ms.to_string() + - 691
"ms |\n\n" - 692
- 693
report = report + "### 质量评估\n\n" - 694
report = report + "| 指标 | 值 |\n|------|------|\n" - 695●
report = report + "| PSNR | " + metrics.psnr.to_string() + " dB |\n" - 696●
report = report + "| SSIM | " + metrics.ssim.to_string() + " |\n" - 697●
report = report + "| MSE | " + metrics.mse.to_string() + " |\n" - 698●
report = report + "| MAE | " + metrics.mae.to_string() + " |\n" - 699●
report = report + "| 相关性 | " + metrics.correlation.to_string() + " |\n" - 700●
report = report + "| 清晰度 | " + metrics.sharpness.to_string() + " |\n" - 701
report = report + - 702
"| 噪声水平 | " + - 703●
metrics.noise_level.to_string() + - 704
" |\n" - 705
report = report + - 706
"| 综合质量 | " + - 707●
metrics.overall_quality.to_string() + - 708
"/5.0 |\n" - 709
- 710
report - 711
}