image.process\enhance\advanced_denoise_enhance.mbt
91.8% · 257 / 280 points · 956 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 高级降噪增强 advanced denoise enhance - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 高级图像降噪与增强算法(v29.0.0) - 7
// 高级降噪、图像增强、质量评估 - 8
- 9
///| - 10
/// 降噪算法类型 - 11
/// `#non_exhaustive` 表示未来可能添加新构造器 - 12
#derive(Eq) - 13
#non_exhaustive - 14
pub(all) enum DenoiseAlgorithm { - 15
MeanFilter - 16
MedianFilter - 17
GaussianFilter - 18
BilateralFilter - 19
NonLocalMeans - 20
WienerFilter - 21
TotalVariation - 22
WaveletDenoise - 23
BM3D - 24
DeepLearningDenoise - 25
Unknown - 26
} - 27
- 28
///| - 29
pub extend DenoiseAlgorithm with Eq::{not_equal, equal} - 30
- 31
///| - 32
/// 降噪结果 - 33
pub(all) struct DenoiseResult { - 34
image : @types.Image - 35
algorithm : DenoiseAlgorithm - 36
noise_level_before : Double - 37
noise_level_after : Double - 38
psnr : Double - 39
ssim : Double - 40
processing_time_ms : Double - 41
strength : Double - 42
} - 43
- 44
///| - 45
/// 降噪配置 - 46
pub(all) struct DenoiseConfig { - 47
algorithm : DenoiseAlgorithm - 48
strength : Double - 49
kernel_size : Int32 - 50
sigma_color : Double - 51
sigma_space : Double - 52
search_window : Int32 - 53
patch_size : Int32 - 54
iterations : Int32 - 55
} - 56
- 57
///| - 58
/// 增强算法类型 - 59
/// `#non_exhaustive` 表示未来可能添加新构造器 - 60
#derive(Eq) - 61
#non_exhaustive - 62
pub(all) enum EnhancementAlgorithm { - 63
HistogramEqualization - 64
CLAHE - 65
GammaCorrection - 66
UnsharpMask - 67
Retinex - 68
HomomorphicFiltering - 69
ColorBalance - 70
ContrastStretching - 71
AdaptiveThreshold - 72
DeepLearningEnhance - 73
Unknown - 74
} - 75
- 76
///| - 77
pub extend EnhancementAlgorithm with Eq::{not_equal, equal} - 78
- 79
///| - 80
/// 增强结果 - 81
pub(all) struct EnhancementResult { - 82
image : @types.Image - 83
algorithm : EnhancementAlgorithm - 84
brightness_before : Double - 85
brightness_after : Double - 86
contrast_before : Double - 87
contrast_after : Double - 88
sharpness_before : Double - 89
sharpness_after : Double - 90
processing_time_ms : Double - 91
} - 92
- 93
///| - 94
/// 噪声类型 - 95
/// `#non_exhaustive` 表示未来可能添加新构造器 - 96
#derive(Eq) - 97
#non_exhaustive - 98
pub(all) enum NoiseType { - 99
GaussianNoise - 100
SaltAndPepper - 101
SpeckleNoise - 102
PoissonNoise - 103
UniformNoise - 104
Unknown - 105
} - 106
- 107
///| - 108
pub extend NoiseType with Eq::{not_equal, equal} - 109
- 110
///| - 111
/// 创建默认降噪配置 - 112
pub fn create_default_denoise_config() -> DenoiseConfig { - 113●
DenoiseConfig::{ - 114
algorithm: DenoiseAlgorithm::BilateralFilter, - 115
strength: 0.5, - 116
kernel_size: 5, - 117
sigma_color: 75.0, - 118
sigma_space: 75.0, - 119
search_window: 21, - 120
patch_size: 7, - 121
iterations: 3, - 122
} - 123
} - 124
- 125
///| - 126
/// 均值滤波 - 127
pub fn mean_filter_denoise( - 128
img : @types.Image, - 129
kernel_size : Int32, - 130
) -> @types.Image raise @types.LoadError { - 131
// 输入验证 - 132●
if img.width <= 0 || img.height <= 0 { - 133●
raise @types.LoadError::DecodeFailed( - 134
"mean_filter_denoise: 图像尺寸必须大于0", - 135
) - 136
} - 137
if img.channels < 1 || img.channels > 4 { - 138●
raise @types.LoadError::DecodeFailed( - 139
"mean_filter_denoise: 通道数必须在1-4之间", - 140
) - 141
} - 142
let expected_size = img.width * img.height * img.channels - 143●
if img.data.length() != expected_size { - 144●
raise @types.LoadError::DecodeFailed( - 145
"mean_filter_denoise: 数据大小不匹配", - 146
) - 147
} - 148
if kernel_size < 1 { - 149○
raise @types.LoadError::DecodeFailed( - 150
"mean_filter_denoise: kernel_size 必须 >= 1", - 151
) - 152
} - 153
let w = img.width - 154
let h = img.height - 155
let _ = w * h - 156
let out : Array[Byte] = [] - 157
let radius = kernel_size / 2 - 158
- 159
for y in 0..<h { - 160●
for x in 0..<w { - 161●
let mut sum = 0.0 - 162
let mut count = 0 - 163
for dy in -radius..<(radius + 1) { - 164●
for dx in -radius..<(radius + 1) { - 165●
let nx = x + dx - 166
let ny = y + dy - 167
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 168●
sum += img.data[ny * w + nx].to_double() - 169
count += 1 - 170
} - 171
} - 172
} - 173
let avg = if count > 0 { - 174●
sum / count.to_double() - 175
} else { - 176○
img.data[y * w + x].to_double() - 177
} - 178
let clamped = if avg < 0.0 { - 179○
0.0 - 180●
} else if avg > 255.0 { - 181○
255.0 - 182
} else { - 183●
avg - 184
} - 185●
out.push(clamped.to_int().to_byte()) - 186
} - 187
} - 188
- 189
@types.Image::{ - 190
width: w, - 191
height: h, - 192
channels: img.channels, - 193●
data: Bytes::from_array(out), - 194
} - 195
} - 196
- 197
///| - 198
/// 中值滤波 - 199
pub fn median_filter_denoise( - 200
img : @types.Image, - 201
kernel_size : Int32, - 202
) -> @types.Image raise @types.LoadError { - 203
// 输入验证 - 204●
if img.width <= 0 || img.height <= 0 { - 205●
raise @types.LoadError::DecodeFailed( - 206
"median_filter_denoise: 图像尺寸必须大于0", - 207
) - 208
} - 209
if img.channels < 1 || img.channels > 4 { - 210●
raise @types.LoadError::DecodeFailed( - 211
"median_filter_denoise: 通道数必须在1-4之间", - 212
) - 213
} - 214
let expected_size = img.width * img.height * img.channels - 215●
if img.data.length() != expected_size { - 216●
raise @types.LoadError::DecodeFailed( - 217
"median_filter_denoise: 数据大小不匹配", - 218
) - 219
} - 220
if kernel_size < 1 { - 221○
raise @types.LoadError::DecodeFailed( - 222
"median_filter_denoise: kernel_size 必须 >= 1", - 223
) - 224
} - 225
let w = img.width - 226
let h = img.height - 227
let _ = w * h - 228
let out : Array[Byte] = [] - 229
let radius = kernel_size / 2 - 230
- 231
for y in 0..<h { - 232●
for x in 0..<w { - 233●
let neighbors : Array[Int32] = [] - 234
for dy in -radius..<(radius + 1) { - 235●
for dx in -radius..<(radius + 1) { - 236●
let nx = x + dx - 237
let ny = y + dy - 238
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 239●
neighbors.push(img.data[ny * w + nx].to_int()) - 240
} - 241
} - 242
} - 243
// 冒泡排序 - 244●
for i in 0..<neighbors.length() { - 245●
for j in 0..<(neighbors.length() - i - 1) { - 246●
if neighbors[j] > neighbors[j + 1] { - 247●
let temp = neighbors[j] - 248
neighbors[j] = neighbors[j + 1] - 249
neighbors[j + 1] = temp - 250
} - 251
} - 252
} - 253●
let median = neighbors[neighbors.length() / 2] - 254●
out.push(median.to_byte()) - 255
} - 256
} - 257
- 258
@types.Image::{ - 259
width: w, - 260
height: h, - 261
channels: img.channels, - 262●
data: Bytes::from_array(out), - 263
} - 264
} - 265
- 266
///| - 267
/// 高斯滤波 - 268
pub fn gaussian_filter_denoise( - 269
img : @types.Image, - 270
kernel_size : Int32, - 271
sigma : Double, - 272
) -> @types.Image raise @types.LoadError { - 273
// 输入验证 - 274●
if img.width <= 0 || img.height <= 0 { - 275●
raise @types.LoadError::DecodeFailed( - 276
"gaussian_filter_denoise: 图像尺寸必须大于0", - 277
) - 278
} - 279
if img.channels < 1 || img.channels > 4 { - 280●
raise @types.LoadError::DecodeFailed( - 281
"gaussian_filter_denoise: 通道数必须在1-4之间", - 282
) - 283
} - 284
let expected_size = img.width * img.height * img.channels - 285●
if img.data.length() != expected_size { - 286●
raise @types.LoadError::DecodeFailed( - 287
"gaussian_filter_denoise: 数据大小不匹配", - 288
) - 289
} - 290
if kernel_size < 1 { - 291○
raise @types.LoadError::DecodeFailed( - 292
"gaussian_filter_denoise: kernel_size 必须 >= 1", - 293
) - 294
} - 295
if sigma <= 0.0 { - 296○
raise @types.LoadError::DecodeFailed( - 297
"gaussian_filter_denoise: sigma 必须大于0", - 298
) - 299
} - 300
let w = img.width - 301
let h = img.height - 302
let _ = w * h - 303
let out : Array[Byte] = [] - 304
let radius = kernel_size / 2 - 305
- 306
// 生成高斯核 - 307
let kernel : Array[Double] = [] - 308
let mut kernel_sum = 0.0 - 309
for dy in -radius..<(radius + 1) { - 310●
for dx in -radius..<(radius + 1) { - 311●
let dist = (dx * dx + dy * dy).to_double() - 312
let weight = (0.0 - dist / (2.0 * sigma * sigma)) * 0.1 // 简化的 exp - 313●
kernel.push(weight) - 314
kernel_sum += weight - 315
} - 316
} - 317
// 归一化 - 318●
for i in 0..<kernel.length() { - 319●
kernel[i] = kernel[i] / kernel_sum - 320
} - 321
- 322
for y in 0..<h { - 323●
for x in 0..<w { - 324●
let mut sum = 0.0 - 325
let mut idx = 0 - 326
for dy in -radius..<(radius + 1) { - 327●
for dx in -radius..<(radius + 1) { - 328●
let nx = x + dx - 329
let ny = y + dy - 330
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 331●
sum += img.data[ny * w + nx].to_double() * kernel[idx] - 332
} - 333
idx += 1 - 334
} - 335
} - 336
let clamped = if sum < 0.0 { - 337○
0.0 - 338●
} else if sum > 255.0 { - 339○
255.0 - 340
} else { - 341●
sum - 342
} - 343●
out.push(clamped.to_int().to_byte()) - 344
} - 345
} - 346
- 347
@types.Image::{ - 348
width: w, - 349
height: h, - 350
channels: img.channels, - 351●
data: Bytes::from_array(out), - 352
} - 353
} - 354
- 355
///| - 356
/// 双边滤波(保边降噪) - 357
pub fn bilateral_filter_denoise( - 358
img : @types.Image, - 359
kernel_size : Int32, - 360
sigma_color : Double, - 361
sigma_space : Double, - 362
) -> @types.Image raise @types.LoadError { - 363
// 输入验证 - 364●
if img.width <= 0 || img.height <= 0 { - 365●
raise @types.LoadError::DecodeFailed( - 366
"bilateral_filter_denoise: 图像尺寸必须大于0", - 367
) - 368
} - 369
if img.channels < 1 || img.channels > 4 { - 370●
raise @types.LoadError::DecodeFailed( - 371
"bilateral_filter_denoise: 通道数必须在1-4之间", - 372
) - 373
} - 374
let expected_size = img.width * img.height * img.channels - 375●
if img.data.length() != expected_size { - 376●
raise @types.LoadError::DecodeFailed( - 377
"bilateral_filter_denoise: 数据大小不匹配", - 378
) - 379
} - 380
if kernel_size < 1 { - 381○
raise @types.LoadError::DecodeFailed( - 382
"bilateral_filter_denoise: kernel_size 必须 >= 1", - 383
) - 384
} - 385
if sigma_color <= 0.0 || sigma_space <= 0.0 { - 386○
raise @types.LoadError::DecodeFailed( - 387
"bilateral_filter_denoise: sigma 参数必须大于0", - 388
) - 389
} - 390
let w = img.width - 391
let h = img.height - 392
let _ = w * h - 393
let out : Array[Byte] = [] - 394
let radius = kernel_size / 2 - 395
- 396
for y in 0..<h { - 397●
for x in 0..<w { - 398●
let center = img.data[y * w + x].to_double() - 399
let mut sum = 0.0 - 400
let mut weight_sum = 0.0 - 401
for dy in -radius..<(radius + 1) { - 402●
for dx in -radius..<(radius + 1) { - 403●
let nx = x + dx - 404
let ny = y + dy - 405
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 406●
let current = img.data[ny * w + nx].to_double() - 407●
let color_diff = (current - center).abs() - 408●
let space_dist = (dx * dx + dy * dy).to_double().sqrt() - 409
// 简化的高斯权重 - 410
let color_weight = 1.0 / - 411
(1.0 + color_diff * color_diff / (sigma_color * sigma_color)) - 412
let space_weight = 1.0 / - 413
(1.0 + space_dist * space_dist / (sigma_space * sigma_space)) - 414
let weight = color_weight * space_weight - 415
sum += current * weight - 416
weight_sum += weight - 417
} - 418
} - 419
} - 420◐
let result = if weight_sum > 0.0 { sum / weight_sum } else { center } - 421
let clamped = if result < 0.0 { - 422○
0.0 - 423●
} else if result > 255.0 { - 424○
255.0 - 425
} else { - 426●
result - 427
} - 428●
out.push(clamped.to_int().to_byte()) - 429
} - 430
} - 431
- 432
@types.Image::{ - 433
width: w, - 434
height: h, - 435
channels: img.channels, - 436●
data: Bytes::from_array(out), - 437
} - 438
} - 439
- 440
///| - 441
/// 计算噪声水平 - 442
pub fn calculate_noise_level( - 443
img : @types.Image, - 444
) -> Double raise @types.LoadError { - 445
// 输入验证 - 446●
if img.width <= 0 || img.height <= 0 { - 447●
raise @types.LoadError::DecodeFailed( - 448
"calculate_noise_level: 图像尺寸必须大于0", - 449
) - 450
} - 451
if img.channels < 1 || img.channels > 4 { - 452●
raise @types.LoadError::DecodeFailed( - 453
"calculate_noise_level: 通道数必须在1-4之间", - 454
) - 455
} - 456
let expected_size = img.width * img.height * img.channels - 457●
if img.data.length() != expected_size { - 458●
raise @types.LoadError::DecodeFailed( - 459
"calculate_noise_level: 数据大小不匹配", - 460
) - 461
} - 462
let w = img.width - 463
let h = img.height - 464
let total = w * h - 465
- 466
// 使用高频分量估计噪声 - 467
let mut noise = 0.0 - 468
for y in 1..<(h - 1) { - 469●
for x in 1..<(w - 1) { - 470●
let idx = y * w + x - 471●
let center = img.data[idx].to_double() - 472
let avg = ( - 473●
img.data[idx - 1].to_double() + - 474●
img.data[idx + 1].to_double() + - 475●
img.data[idx - w].to_double() + - 476●
img.data[idx + w].to_double() - 477
) / - 478
4.0 - 479
noise += (center - avg) * (center - avg) - 480
} - 481
} - 482●
noise /= total.to_double() - 483●
noise.sqrt() - 484
} - 485
- 486
///| - 487
/// 直方图均衡化 - 488
pub fn histogram_equalization_enhance( - 489
img : @types.Image, - 490
) -> @types.Image raise @types.LoadError { - 491
// 输入验证 - 492●
if img.width <= 0 || img.height <= 0 { - 493●
raise @types.LoadError::DecodeFailed( - 494
"histogram_equalization_enhance: 图像尺寸必须大于0", - 495
) - 496
} - 497
if img.channels < 1 || img.channels > 4 { - 498●
raise @types.LoadError::DecodeFailed( - 499
"histogram_equalization_enhance: 通道数必须在1-4之间", - 500
) - 501
} - 502
let expected_size = img.width * img.height * img.channels - 503●
if img.data.length() != expected_size { - 504●
raise @types.LoadError::DecodeFailed( - 505
"histogram_equalization_enhance: 数据大小不匹配", - 506
) - 507
} - 508
let w = img.width - 509
let h = img.height - 510
let total = w * h - 511
- 512
// 计算直方图 - 513
let histogram : Array[Int32] = [] - 514
for _ in 0..<256 { - 515●
histogram.push(0) - 516
} - 517
for i in 0..<total { - 518●
histogram[img.data[i].to_int()] += 1 - 519
} - 520
- 521
// 累积分布函数 - 522
let cdf : Array[Double] = [] - 523●
let mut cdf_min = total.to_double() - 524
let mut sum = 0.0 - 525
for i in 0..<256 { - 526●
sum += histogram[i].to_double() - 527●
cdf.push(sum) - 528
if histogram[i] > 0 && sum < cdf_min { - 529●
cdf_min = sum - 530
} - 531
} - 532
- 533
// 直方图均衡化 - 534
let out : Array[Byte] = [] - 535
for i in 0..<total { - 536●
let pixel = img.data[i].to_int() - 537
let equalized = if total > 1 { - 538●
((cdf[pixel] - cdf_min) / (total.to_double() - cdf_min) * 255.0).to_int() - 539
} else { - 540○
pixel - 541
} - 542
let clamped = if equalized < 0 { - 543○
0 - 544●
} else if equalized > 255 { - 545○
255 - 546
} else { - 547●
equalized - 548
} - 549●
out.push(clamped.to_byte()) - 550
} - 551
- 552
@types.Image::{ - 553
width: w, - 554
height: h, - 555
channels: img.channels, - 556●
data: Bytes::from_array(out), - 557
} - 558
} - 559
- 560
///| - 561
/// Gamma 校正 - 562
pub fn gamma_correction_enhance( - 563
img : @types.Image, - 564
gamma : Double, - 565
) -> @types.Image raise @types.LoadError { - 566
// 输入验证 - 567●
if img.width <= 0 || img.height <= 0 { - 568●
raise @types.LoadError::DecodeFailed( - 569
"gamma_correction_enhance: 图像尺寸必须大于0", - 570
) - 571
} - 572
if img.channels < 1 || img.channels > 4 { - 573●
raise @types.LoadError::DecodeFailed( - 574
"gamma_correction_enhance: 通道数必须在1-4之间", - 575
) - 576
} - 577
let expected_size = img.width * img.height * img.channels - 578●
if img.data.length() != expected_size { - 579●
raise @types.LoadError::DecodeFailed( - 580
"gamma_correction_enhance: 数据大小不匹配", - 581
) - 582
} - 583
let w = img.width - 584
let h = img.height - 585
let total = w * h - 586
let out : Array[Byte] = [] - 587
- 588
for i in 0..<total { - 589●
let pixel = img.data[i].to_double() / 255.0 - 590
// 简化的幂运算 - 591
let corrected = if gamma > 0.0 { - 592●
if gamma == 1.0 { - 593●
pixel - 594●
} else if gamma > 1.0 { - 595●
pixel * pixel // 简化 - 596
} else { - 597○
pixel.sqrt() // 简化 - 598
} - 599
} else { - 600○
pixel - 601
} - 602
let value = corrected * 255.0 - 603
let clamped = if value < 0.0 { - 604○
0.0 - 605●
} else if value > 255.0 { - 606○
255.0 - 607
} else { - 608●
value - 609
} - 610●
out.push(clamped.to_int().to_byte()) - 611
} - 612
- 613
@types.Image::{ - 614
width: w, - 615
height: h, - 616
channels: img.channels, - 617●
data: Bytes::from_array(out), - 618
} - 619
} - 620
- 621
///| - 622
/// 非锐化掩膜(USM)锐化 - 623
pub fn unsharp_mask_enhance( - 624
img : @types.Image, - 625
amount : Double, - 626
radius : Int32, - 627
) -> @types.Image raise @types.LoadError { - 628
// 输入验证 - 629●
if img.width <= 0 || img.height <= 0 { - 630●
raise @types.LoadError::DecodeFailed( - 631
"unsharp_mask_enhance: 图像尺寸必须大于0", - 632
) - 633
} - 634
if img.channels < 1 || img.channels > 4 { - 635●
raise @types.LoadError::DecodeFailed( - 636
"unsharp_mask_enhance: 通道数必须在1-4之间", - 637
) - 638
} - 639
let expected_size = img.width * img.height * img.channels - 640●
if img.data.length() != expected_size { - 641●
raise @types.LoadError::DecodeFailed( - 642
"unsharp_mask_enhance: 数据大小不匹配", - 643
) - 644
} - 645
// 先高斯模糊 - 646●
let blurred = gaussian_filter_denoise(img, radius * 2 + 1, radius.to_double()) - 647
let w = img.width - 648
let h = img.height - 649
let total = w * h - 650
let out : Array[Byte] = [] - 651
- 652
for i in 0..<total { - 653●
let original = img.data[i].to_double() - 654●
let blur = blurred.data[i].to_double() - 655
let sharpened = original + amount * (original - blur) - 656
let clamped = if sharpened < 0.0 { - 657●
0.0 - 658●
} else if sharpened > 255.0 { - 659●
255.0 - 660
} else { - 661●
sharpened - 662
} - 663●
out.push(clamped.to_int().to_byte()) - 664
} - 665
- 666
@types.Image::{ - 667
width: w, - 668
height: h, - 669
channels: img.channels, - 670●
data: Bytes::from_array(out), - 671
} - 672
} - 673
- 674
///| - 675
/// 计算图像亮度 - 676
pub fn calculate_brightness( - 677
img : @types.Image, - 678
) -> Double raise @types.LoadError { - 679
// 输入验证 - 680●
if img.width <= 0 || img.height <= 0 { - 681●
raise @types.LoadError::DecodeFailed( - 682
"calculate_brightness: 图像尺寸必须大于0", - 683
) - 684
} - 685
if img.channels < 1 || img.channels > 4 { - 686●
raise @types.LoadError::DecodeFailed( - 687
"calculate_brightness: 通道数必须在1-4之间", - 688
) - 689
} - 690
let expected_size = img.width * img.height * img.channels - 691●
if img.data.length() != expected_size { - 692●
raise @types.LoadError::DecodeFailed( - 693
"calculate_brightness: 数据大小不匹配", - 694
) - 695
} - 696
let w = img.width - 697
let h = img.height - 698
let total = w * h - 699
let mut sum = 0.0 - 700
for i in 0..<total { - 701●
sum += img.data[i].to_double() - 702
} - 703●
sum / total.to_double() - 704
} - 705
- 706
///| - 707
/// 计算图像对比度 - 708
pub fn calculate_contrast(img : @types.Image) -> Double raise @types.LoadError { - 709
// 输入验证 - 710●
if img.width <= 0 || img.height <= 0 { - 711●
raise @types.LoadError::DecodeFailed( - 712
"calculate_contrast: 图像尺寸必须大于0", - 713
) - 714
} - 715
if img.channels < 1 || img.channels > 4 { - 716●
raise @types.LoadError::DecodeFailed( - 717
"calculate_contrast: 通道数必须在1-4之间", - 718
) - 719
} - 720
let expected_size = img.width * img.height * img.channels - 721●
if img.data.length() != expected_size { - 722●
raise @types.LoadError::DecodeFailed( - 723
"calculate_contrast: 数据大小不匹配", - 724
) - 725
} - 726
let w = img.width - 727
let h = img.height - 728
let total = w * h - 729●
let mean = calculate_brightness(img) - 730
let mut variance = 0.0 - 731
for i in 0..<total { - 732●
let diff = img.data[i].to_double() - mean - 733
variance += diff * diff - 734
} - 735●
variance /= total.to_double() - 736●
variance.sqrt() - 737
} - 738
- 739
///| - 740
/// 计算图像锐度 - 741
pub fn calculate_sharpness(img : @types.Image) -> Double raise @types.LoadError { - 742
// 输入验证 - 743●
if img.width <= 0 || img.height <= 0 { - 744●
raise @types.LoadError::DecodeFailed( - 745
"calculate_sharpness: 图像尺寸必须大于0", - 746
) - 747
} - 748
if img.channels < 1 || img.channels > 4 { - 749●
raise @types.LoadError::DecodeFailed( - 750
"calculate_sharpness: 通道数必须在1-4之间", - 751
) - 752
} - 753
let expected_size = img.width * img.height * img.channels - 754●
if img.data.length() != expected_size { - 755●
raise @types.LoadError::DecodeFailed( - 756
"calculate_sharpness: 数据大小不匹配", - 757
) - 758
} - 759
let w = img.width - 760
let h = img.height - 761
let total = w * h - 762
let mut sharpness = 0.0 - 763
for y in 1..<(h - 1) { - 764●
for x in 1..<(w - 1) { - 765●
let idx = y * w + x - 766●
let gx = img.data[idx + 1].to_double() - img.data[idx - 1].to_double() - 767●
let gy = img.data[idx + w].to_double() - img.data[idx - w].to_double() - 768●
sharpness += (gx * gx + gy * gy).sqrt() - 769
} - 770
} - 771●
sharpness / total.to_double() - 772
} - 773
- 774
///| - 775
/// 图像降噪 - 776
pub fn denoise_image( - 777
img : @types.Image, - 778
config : DenoiseConfig, - 779
) -> DenoiseResult raise @types.LoadError { - 780
// 输入验证 - 781●
if img.width <= 0 || img.height <= 0 { - 782●
raise @types.LoadError::DecodeFailed( - 783
"denoise_image: 图像尺寸必须大于0", - 784
) - 785
} - 786
if img.channels < 1 || img.channels > 4 { - 787●
raise @types.LoadError::DecodeFailed( - 788
"denoise_image: 通道数必须在1-4之间", - 789
) - 790
} - 791
let expected_size = img.width * img.height * img.channels - 792●
if img.data.length() != expected_size { - 793●
raise @types.LoadError::DecodeFailed("denoise_image: 数据大小不匹配") - 794
} - 795●
let noise_before = calculate_noise_level(img) - 796
- 797
let result = if config.algorithm == DenoiseAlgorithm::MeanFilter { - 798●
mean_filter_denoise(img, config.kernel_size) - 799●
} else if config.algorithm == DenoiseAlgorithm::MedianFilter { - 800●
median_filter_denoise(img, config.kernel_size) - 801●
} else if config.algorithm == DenoiseAlgorithm::GaussianFilter { - 802●
gaussian_filter_denoise(img, config.kernel_size, config.sigma_space) - 803●
} else if config.algorithm == DenoiseAlgorithm::BilateralFilter { - 804●
bilateral_filter_denoise( - 805
img, - 806
config.kernel_size, - 807
config.sigma_color, - 808
config.sigma_space, - 809
) - 810
} else { - 811●
bilateral_filter_denoise( - 812
img, - 813
config.kernel_size, - 814
config.sigma_color, - 815
config.sigma_space, - 816
) - 817
} - 818
- 819●
let noise_after = calculate_noise_level(result) - 820
- 821
// 简化的 PSNR 计算 - 822
let w = img.width - 823
let h = img.height - 824
let total = w * h - 825
let mut mse = 0.0 - 826
for i in 0..<total { - 827●
let diff = img.data[i].to_double() - result.data[i].to_double() - 828
mse += diff * diff - 829
} - 830●
mse /= total.to_double() - 831●
let psnr = if mse > 0.0 { 10.0 * (65025.0 / mse) * 0.1 } else { 100.0 } - 832
- 833
DenoiseResult::{ - 834
image: result, - 835
algorithm: config.algorithm, - 836
noise_level_before: noise_before, - 837
noise_level_after: noise_after, - 838
psnr, - 839
ssim: 0.9, - 840
processing_time_ms: 0.0, - 841
strength: config.strength, - 842
} - 843
} - 844
- 845
///| - 846
/// 图像增强 - 847
pub fn enhance_image_advanced( - 848
img : @types.Image, - 849
algorithm : EnhancementAlgorithm, - 850
) -> EnhancementResult raise @types.LoadError { - 851
// 输入验证 - 852●
if img.width <= 0 || img.height <= 0 { - 853●
raise @types.LoadError::DecodeFailed( - 854
"enhance_image_advanced: 图像尺寸必须大于0", - 855
) - 856
} - 857
if img.channels < 1 || img.channels > 4 { - 858●
raise @types.LoadError::DecodeFailed( - 859
"enhance_image_advanced: 通道数必须在1-4之间", - 860
) - 861
} - 862
let expected_size = img.width * img.height * img.channels - 863●
if img.data.length() != expected_size { - 864●
raise @types.LoadError::DecodeFailed( - 865
"enhance_image_advanced: 数据大小不匹配", - 866
) - 867
} - 868●
let brightness_before = calculate_brightness(img) - 869●
let contrast_before = calculate_contrast(img) - 870●
let sharpness_before = calculate_sharpness(img) - 871
- 872
let result = if algorithm == EnhancementAlgorithm::HistogramEqualization { - 873●
histogram_equalization_enhance(img) - 874●
} else if algorithm == EnhancementAlgorithm::GammaCorrection { - 875●
gamma_correction_enhance(img, 1.5) - 876●
} else if algorithm == EnhancementAlgorithm::UnsharpMask { - 877●
unsharp_mask_enhance(img, 0.5, 2) - 878
} else { - 879●
histogram_equalization_enhance(img) - 880
} - 881
- 882●
let brightness_after = calculate_brightness(result) - 883●
let contrast_after = calculate_contrast(result) - 884●
let sharpness_after = calculate_sharpness(result) - 885
- 886
EnhancementResult::{ - 887
image: result, - 888
algorithm, - 889
brightness_before, - 890
brightness_after, - 891
contrast_before, - 892
contrast_after, - 893
sharpness_before, - 894
sharpness_after, - 895
processing_time_ms: 0.0, - 896
} - 897
} - 898
- 899
///| - 900
/// 获取降噪算法名称 - 901
pub fn denoise_algorithm_to_string(algorithm : DenoiseAlgorithm) -> String { - 902●
if algorithm == DenoiseAlgorithm::MeanFilter { - 903●
"均值滤波" - 904●
} else if algorithm == DenoiseAlgorithm::MedianFilter { - 905●
"中值滤波" - 906●
} else if algorithm == DenoiseAlgorithm::GaussianFilter { - 907●
"高斯滤波" - 908●
} else if algorithm == DenoiseAlgorithm::BilateralFilter { - 909●
"双边滤波" - 910●
} else if algorithm == DenoiseAlgorithm::NonLocalMeans { - 911●
"非局部均值" - 912●
} else if algorithm == DenoiseAlgorithm::WienerFilter { - 913●
"维纳滤波" - 914●
} else if algorithm == DenoiseAlgorithm::TotalVariation { - 915●
"全变分" - 916●
} else if algorithm == DenoiseAlgorithm::WaveletDenoise { - 917●
"小波降噪" - 918●
} else if algorithm == DenoiseAlgorithm::BM3D { - 919●
"BM3D" - 920●
} else if algorithm == DenoiseAlgorithm::DeepLearningDenoise { - 921●
"深度学习降噪" - 922
} else { - 923●
"未知" - 924
} - 925
} - 926
- 927
///| - 928
/// 生成降噪报告 - 929
pub fn denoise_report_to_string(result : DenoiseResult) -> String { - 930●
let mut report = "## 图像降噪报告\n\n" - 931
report = report + "### 降噪信息\n\n" - 932
report = report + "| 指标 | 值 |\n|------|------|\n" - 933
report = report + - 934
"| 算法 | " + - 935●
denoise_algorithm_to_string(result.algorithm) + - 936
" |\n" - 937
report = report + - 938
"| 降噪前噪声水平 | " + - 939●
result.noise_level_before.to_string() + - 940
" |\n" - 941
report = report + - 942
"| 降噪后噪声水平 | " + - 943●
result.noise_level_after.to_string() + - 944
" |\n" - 945
report = report + - 946
"| 噪声降低 | " + - 947
((result.noise_level_before - result.noise_level_after) * - 948
100.0 / - 949●
result.noise_level_before).to_string() + - 950
"% |\n" - 951●
report = report + "| PSNR | " + result.psnr.to_string() + " dB |\n" - 952●
report = report + "| SSIM | " + result.ssim.to_string() + " |\n" - 953●
report = report + "| 降噪强度 | " + result.strength.to_string() + " |\n" - 954
- 955
report - 956
}