image.process\enhance\dehaze.mbt
87% · 141 / 162 points · 513 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 去雾 dehaze - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 图像去雾(v11.6.0) - 7
// 暗通道先验算法 + 大气光估计 + 透射率优化 + 去雾复原 - 8
- 9
///| - 10
/// 去雾配置 - 11
pub(all) struct DehazeConfig { - 12
patch_size : Int32 // 暗通道 patch 大小 - 13
omega : Double // 透射率保留系数(0-1,越大去雾越强) - 14
t_min : Double // 最小透射率 - 15
guided_radius : Int32 // 引导滤波半径 - 16
guided_eps : Double // 引导滤波正则化参数 - 17
max_atmospheric : Int32 // 大气光候选像素比例(前 0.1%) - 18
} - 19
- 20
///| - 21
/// 创建默认去雾配置 - 22
pub fn dehaze_default_config() -> DehazeConfig { - 23●
DehazeConfig::{ - 24
patch_size: 15, - 25
omega: 0.95, - 26
t_min: 0.1, - 27
guided_radius: 20, - 28
guided_eps: 0.0001, - 29
max_atmospheric: 1000, - 30
} - 31
} - 32
- 33
///| - 34
/// 计算暗通道 - 35
/// 暗通道:在局部 patch 内,取每个像素 RGB 三通道的最小值,再取 patch 内最小值 - 36
pub fn dark_channel( - 37
img : @types.Image, - 38
patch_size~ : Int32 = 15, - 39
) -> @types.Image raise @types.LoadError { - 40
// 输入验证 - 41●
if img.width <= 0 || img.height <= 0 { - 42●
raise @types.LoadError::DecodeFailed( - 43
"dark_channel: 图像尺寸必须大于0", - 44
) - 45
} - 46
if img.channels < 1 || img.channels > 4 { - 47●
raise @types.LoadError::DecodeFailed( - 48
"dark_channel: 通道数必须在1-4之间", - 49
) - 50
} - 51
let expected_size = img.width * img.height * img.channels - 52●
if img.data.length() != expected_size { - 53●
raise @types.LoadError::DecodeFailed( - 54
"dark_channel: 数据大小不匹配,期望 " + - 55●
expected_size.to_string() + - 56
",实际 " + - 57●
img.data.length().to_string(), - 58
) - 59
} - 60
if patch_size <= 0 || patch_size % 2 == 0 { - 61●
raise @types.LoadError::DecodeFailed( - 62
"dark_channel: patch_size必须是正奇数", - 63
) - 64
} - 65
- 66
let ps = patch_size - 67
let w = img.width - 68
let h = img.height - 69
let channels = img.channels - 70
let data = img.data - 71
// 1. 计算每个像素的最小通道值 - 72
let min_channel : Array[Byte] = [] - 73
for i in 0..<(w * h) { - 74●
let idx = i * channels - 75●
let r = data[idx].to_int() - 76●
let g = if channels >= 2 { data[idx + 1].to_int() } else { r } - 77●
let b = if channels >= 3 { data[idx + 2].to_int() } else { r } - 78●
let min_val = r.min(g).min(b) - 79●
min_channel.push(min_val.to_byte()) - 80
} - 81
// 2. 计算局部最小值(最小值滤波) - 82●
let out_data : Array[Byte] = Array::make(w * h, b'\x00') - 83
let half = ps / 2 - 84
for y in 0..<h { - 85●
for x in 0..<w { - 86●
let mut min_val = 255 - 87
for dy in -half..<(half + 1) { - 88●
for dx in -half..<(half + 1) { - 89●
let nx = x + dx - 90
let ny = y + dy - 91
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 92●
let val = min_channel[ny * w + nx].to_int() - 93
if val < min_val { - 94●
min_val = val - 95
} - 96
} - 97
} - 98
} - 99●
out_data[y * w + x] = min_val.to_byte() - 100
} - 101
} - 102
@types.Image::{ - 103
width: w, - 104
height: h, - 105
channels: 1, - 106●
data: Bytes::from_array(out_data), - 107
} - 108
} - 109
- 110
///| - 111
/// 估计大气光 - 112
/// 取暗通道中最亮的 0.1% 像素,在原图中取这些像素的最大值作为大气光 - 113
pub fn estimate_atmospheric_light( - 114
img : @types.Image, - 115
dark : @types.Image, - 116
top_percent~ : Int32 = 1000, - 117
) -> Array[Int32] raise @types.LoadError { - 118
// 输入验证 - 119●
if img.width <= 0 || img.height <= 0 { - 120○
raise @types.LoadError::DecodeFailed( - 121
"estimate_atmospheric_light: 图像尺寸必须大于0", - 122
) - 123
} - 124
if img.channels < 1 || img.channels > 4 { - 125●
raise @types.LoadError::DecodeFailed( - 126
"estimate_atmospheric_light: 通道数必须在1-4之间", - 127
) - 128
} - 129
if dark.width != img.width || dark.height != img.height { - 130○
raise @types.LoadError::DecodeFailed( - 131
"estimate_atmospheric_light: 暗通道图像尺寸必须与原图一致", - 132
) - 133
} - 134
if dark.channels != 1 { - 135●
raise @types.LoadError::DecodeFailed( - 136
"estimate_atmospheric_light: 暗通道图像必须是单通道", - 137
) - 138
} - 139
if top_percent <= 0 { - 140○
raise @types.LoadError::DecodeFailed( - 141
"estimate_atmospheric_light: top_percent必须大于0", - 142
) - 143
} - 144
- 145
let top = top_percent - 146
let w = img.width - 147
let h = img.height - 148
let channels = img.channels - 149
let img_data = img.data - 150
let dark_data = dark.data - 151
// 收集所有像素的暗通道值和坐标 - 152
let pixels : Array[(Int32, Int32)] = [] - 153
for y in 0..<h { - 154●
for x in 0..<w { - 155●
pixels.push((dark_data[y * w + x].to_int(), y * w + x)) - 156
} - 157
} - 158
// 按暗通道值降序排序(选择排序,取前 top 个) - 159●
let count = if top < pixels.length() { top } else { pixels.length() } - 160
let selected_indices : Array[Int32] = [] - 161●
let used : Array[Bool] = Array::make(pixels.length(), false) - 162
for _ in 0..<count { - 163●
let mut max_val = -1 - 164
let mut max_idx = 0 - 165●
for i in 0..<pixels.length() { - 166●
if !used[i] && pixels[i].0 > max_val { - 167●
max_val = pixels[i].0 - 168
max_idx = i - 169
} - 170
} - 171
used[max_idx] = true - 172●
selected_indices.push(pixels[max_idx].1) - 173
} - 174
// 在这些像素中取每个通道的最大值 - 175
let atmospheric : Array[Int32] = [] - 176
for c in 0..<channels { - 177●
let mut max_val = 0 - 178
for idx in selected_indices { - 179●
let val = img_data[idx * channels + c].to_int() - 180
if val > max_val { - 181●
max_val = val - 182
} - 183
} - 184●
atmospheric.push(max_val) - 185
} - 186
atmospheric - 187
} - 188
- 189
///| - 190
/// 计算透射率 - 191
/// 透射率 t(x) = 1 - omega * dark_channel(I/A) - 192
pub fn estimate_transmission( - 193
img : @types.Image, - 194
atmospheric : Array[Int32], - 195
omega~ : Double = 0.95, - 196
patch_size~ : Int32 = 15, - 197
) -> @types.Image raise @types.LoadError { - 198
// 输入验证 - 199●
if img.width <= 0 || img.height <= 0 { - 200○
raise @types.LoadError::DecodeFailed( - 201
"estimate_transmission: 图像尺寸必须大于0", - 202
) - 203
} - 204
if img.channels < 1 || img.channels > 4 { - 205○
raise @types.LoadError::DecodeFailed( - 206
"estimate_transmission: 通道数必须在1-4之间", - 207
) - 208
} - 209●
if atmospheric.length() == 0 { - 210●
raise @types.LoadError::DecodeFailed( - 211
"estimate_transmission: atmospheric数组不能为空", - 212
) - 213
} - 214
if omega < 0.0 || omega > 1.0 { - 215●
raise @types.LoadError::DecodeFailed( - 216
"estimate_transmission: omega必须在0-1之间", - 217
) - 218
} - 219
if patch_size <= 0 || patch_size % 2 == 0 { - 220○
raise @types.LoadError::DecodeFailed( - 221
"estimate_transmission: patch_size必须是正奇数", - 222
) - 223
} - 224
- 225
let om = omega - 226
let ps = patch_size - 227
let w = img.width - 228
let h = img.height - 229
let channels = img.channels - 230
let data = img.data - 231
// 1. 归一化图像(除以大气光) - 232
let normalized : Array[Byte] = [] - 233
for i in 0..<(w * h) { - 234●
for c in 0..<channels { - 235●
let idx = i * channels + c - 236●
let val = data[idx].to_double() - 237●
let a = if c < atmospheric.length() { - 238●
atmospheric[c].to_double() - 239
} else { - 240○
255.0 - 241
} - 242
let norm = if a > 0.0 { - 243●
(val / a * 255.0).min(255.0).max(0.0) - 244
} else { - 245○
0.0 - 246
} - 247●
normalized.push(norm.to_int().to_byte()) - 248
} - 249
} - 250
let norm_img = @types.Image::{ - 251
width: w, - 252
height: h, - 253
channels, - 254●
data: Bytes::from_array(normalized), - 255
} - 256
// 2. 计算归一化图像的暗通道 - 257●
let dark = dark_channel(norm_img, patch_size=ps) - 258
// 3. 计算透射率 - 259
let dark_data = dark.data - 260●
let out_data : Array[Byte] = Array::make(w * h, b'\x00') - 261
for i in 0..<(w * h) { - 262●
let t = 1.0 - om * dark_data[i].to_double() / 255.0 - 263●
let t_clamped = t.max(0.0).min(1.0) - 264●
out_data[i] = (t_clamped * 255.0).to_int().to_byte() - 265
} - 266
@types.Image::{ - 267
width: w, - 268
height: h, - 269
channels: 1, - 270●
data: Bytes::from_array(out_data), - 271
} - 272
} - 273
- 274
///| - 275
/// 引导滤波(用于透射率优化) - 276
/// 简化版:使用均值滤波近似引导滤波 - 277
pub fn guided_filter_image( - 278
input : @types.Image, - 279
guide : @types.Image, - 280
radius~ : Int32 = 20, - 281
eps~ : Double = 0.0001, - 282
) -> @types.Image raise @types.LoadError { - 283
// 输入验证 - 284●
if input.width <= 0 || input.height <= 0 { - 285○
raise @types.LoadError::DecodeFailed( - 286
"guided_filter_image: 输入图像尺寸必须大于0", - 287
) - 288
} - 289
if guide.width <= 0 || guide.height <= 0 { - 290○
raise @types.LoadError::DecodeFailed( - 291
"guided_filter_image: 引导图像尺寸必须大于0", - 292
) - 293
} - 294
if input.width != guide.width || input.height != guide.height { - 295●
raise @types.LoadError::DecodeFailed( - 296
"guided_filter_image: 输入图像和引导图像尺寸必须一致", - 297
) - 298
} - 299
if radius <= 0 { - 300●
raise @types.LoadError::DecodeFailed( - 301
"guided_filter_image: radius必须大于0", - 302
) - 303
} - 304
if eps < 0.0 { - 305○
raise @types.LoadError::DecodeFailed( - 306
"guided_filter_image: eps必须大于等于0", - 307
) - 308
} - 309
- 310
let r = radius - 311
let e = eps - 312
let w = input.width - 313
let h = input.height - 314
let input_data = input.data - 315
let guide_data = guide.data - 316
// 简化版引导滤波:使用均值滤波 - 317
// 完整引导滤波需要计算均值、方差、协方差等 - 318
// 这里用双边滤波近似 - 319●
let out_data : Array[Byte] = Array::make(w * h, b'\x00') - 320
let half = r / 2 - 321
for y in 0..<h { - 322●
for x in 0..<w { - 323●
let mut sum = 0.0 - 324
let mut weight_sum = 0.0 - 325●
let center_guide = guide_data[y * w + x].to_double() - 326
for dy in -half..<(half + 1) { - 327●
for dx in -half..<(half + 1) { - 328●
let nx = x + dx - 329
let ny = y + dy - 330
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 331●
let spatial_dist = (dx * dx + dy * dy).to_double().sqrt() - 332●
let guide_diff = (guide_data[ny * w + nx].to_double() - center_guide).abs() - 333
// 双边权重:空间距离 + 引导图差异 - 334●
let spatial_weight = (1.0 - spatial_dist / half.to_double()).max( - 335
0.0, - 336
) - 337●
let range_weight = (1.0 - guide_diff / 255.0).max(0.0) - 338
let weight = spatial_weight * range_weight + e - 339●
sum += input_data[ny * w + nx].to_double() * weight - 340
weight_sum += weight - 341
} - 342
} - 343
} - 344
let result = if weight_sum > 0.0 { - 345●
sum / weight_sum - 346
} else { - 347○
input_data[y * w + x].to_double() - 348
} - 349●
out_data[y * w + x] = result.min(255.0).max(0.0).to_int().to_byte() - 350
} - 351
} - 352
@types.Image::{ - 353
width: w, - 354
height: h, - 355
channels: 1, - 356●
data: Bytes::from_array(out_data), - 357
} - 358
} - 359
- 360
///| - 361
/// 去雾复原 - 362
/// 去雾复原 J(x) = (I(x) - A) / max(t(x), t0) + A - 363
pub fn dehaze_recover( - 364
img : @types.Image, - 365
transmission : @types.Image, - 366
atmospheric : Array[Int32], - 367
t_min~ : Double = 0.1, - 368
) -> @types.Image raise @types.LoadError { - 369
// 输入验证 - 370●
if img.width <= 0 || img.height <= 0 { - 371○
raise @types.LoadError::DecodeFailed( - 372
"dehaze_recover: 图像尺寸必须大于0", - 373
) - 374
} - 375
if img.channels < 1 || img.channels > 4 { - 376○
raise @types.LoadError::DecodeFailed( - 377
"dehaze_recover: 通道数必须在1-4之间", - 378
) - 379
} - 380
if transmission.width != img.width || transmission.height != img.height { - 381●
raise @types.LoadError::DecodeFailed( - 382
"dehaze_recover: 透射率图像尺寸必须与原图一致", - 383
) - 384
} - 385
if transmission.channels != 1 { - 386●
raise @types.LoadError::DecodeFailed( - 387
"dehaze_recover: 透射率图像必须是单通道", - 388
) - 389
} - 390●
if atmospheric.length() == 0 { - 391●
raise @types.LoadError::DecodeFailed( - 392
"dehaze_recover: atmospheric数组不能为空", - 393
) - 394
} - 395
if t_min < 0.0 || t_min > 1.0 { - 396○
raise @types.LoadError::DecodeFailed( - 397
"dehaze_recover: t_min必须在0-1之间", - 398
) - 399
} - 400
- 401
let t0 = t_min - 402
let w = img.width - 403
let h = img.height - 404
let channels = img.channels - 405
let img_data = img.data - 406
let trans_data = transmission.data - 407●
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00') - 408
for i in 0..<(w * h) { - 409●
let t = trans_data[i].to_double() / 255.0 - 410●
let t_clamped = t.max(t0) - 411
for c in 0..<channels { - 412●
let idx = i * channels + c - 413●
let val = img_data[idx].to_double() - 414●
let a = if c < atmospheric.length() { - 415●
atmospheric[c].to_double() - 416
} else { - 417○
255.0 - 418
} - 419
let recovered = (val - a) / t_clamped + a - 420●
out_data[idx] = recovered.min(255.0).max(0.0).to_int().to_byte() - 421
} - 422
} - 423
@types.Image::{ - 424
width: w, - 425
height: h, - 426
channels, - 427●
data: Bytes::from_array(out_data), - 428
} - 429
} - 430
- 431
///| - 432
/// 完整去雾流水线 - 433
pub fn dehaze_advanced( - 434
img : @types.Image, - 435
config? : DehazeConfig, - 436
) -> @types.Image raise @types.LoadError { - 437
// 输入验证 - 438●
if img.width <= 0 || img.height <= 0 { - 439●
raise @types.LoadError::DecodeFailed( - 440
"dehaze_advanced: 图像尺寸必须大于0", - 441
) - 442
} - 443
if img.channels < 1 || img.channels > 4 { - 444●
raise @types.LoadError::DecodeFailed( - 445
"dehaze_advanced: 通道数必须在1-4之间", - 446
) - 447
} - 448
let expected_size = img.width * img.height * img.channels - 449●
if img.data.length() != expected_size { - 450●
raise @types.LoadError::DecodeFailed( - 451
"dehaze_advanced: 数据大小不匹配,期望 " + - 452●
expected_size.to_string() + - 453
",实际 " + - 454●
img.data.length().to_string(), - 455
) - 456
} - 457
- 458●
let cfg = match config { - 459●
Some(c) => c - 460●
None => dehaze_default_config() - 461
} - 462
- 463
// 参数验证 - 464
if cfg.patch_size <= 0 || cfg.patch_size % 2 == 0 { - 465●
raise @types.LoadError::DecodeFailed( - 466
"dehaze_advanced: patch_size必须是正奇数", - 467
) - 468
} - 469
if cfg.omega < 0.0 || cfg.omega > 1.0 { - 470○
raise @types.LoadError::DecodeFailed( - 471
"dehaze_advanced: omega必须在0-1之间", - 472
) - 473
} - 474
if cfg.t_min < 0.0 || cfg.t_min > 1.0 { - 475○
raise @types.LoadError::DecodeFailed( - 476
"dehaze_advanced: t_min必须在0-1之间", - 477
) - 478
} - 479
if cfg.guided_radius <= 0 { - 480○
raise @types.LoadError::DecodeFailed( - 481
"dehaze_advanced: guided_radius必须大于0", - 482
) - 483
} - 484
if cfg.max_atmospheric <= 0 { - 485○
raise @types.LoadError::DecodeFailed( - 486
"dehaze_advanced: max_atmospheric必须大于0", - 487
) - 488
} - 489
// 1. 计算暗通道 - 490●
let dark = dark_channel(img, patch_size=cfg.patch_size) - 491
// 2. 估计大气光 - 492●
let atmospheric = estimate_atmospheric_light( - 493
img, - 494
dark, - 495
top_percent=cfg.max_atmospheric, - 496
) - 497
// 3. 估计透射率 - 498●
let transmission_raw = estimate_transmission( - 499
img, - 500
atmospheric, - 501
omega=cfg.omega, - 502
patch_size=cfg.patch_size, - 503
) - 504
// 4. 引导滤波优化透射率 - 505●
let transmission = guided_filter_image( - 506
transmission_raw, - 507
img, - 508
radius=cfg.guided_radius, - 509
eps=cfg.guided_eps, - 510
) - 511
// 5. 去雾复原 - 512●
dehaze_recover(img, transmission, atmospheric, t_min=cfg.t_min) - 513
}