image.process\color\dehaze.mbt
92.4% · 121 / 131 points · 330 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
// 暗通道先验去雾(纯 MoonBit) - 7
- 8
///| - 9
/// 暗通道先验去雾(Dark Channel Prior Dehaze)。 - 10
/// 基于 He et al. (2009) 的暗通道先验方法。 - 11
/// 适用于 RGB/RGBA 图像(channels=3/4)。 - 12
/// patch_size 为暗通道计算窗口半径(默认 7),omega 为去雾强度(默认 0.95), - 13
/// t0 为透射率下限(默认 0.1)。 - 14
pub fn dehaze( - 15
img : @types.Image, - 16
patch_size~ : Int32 = 7, - 17
omega~ : Float = 0.95F, - 18
t0~ : Float = 0.1F, - 19
) -> @types.Image raise @types.LoadError { - 20
// 输入验证 - 21●
if img.width < 0 || img.height < 0 { - 22○
raise @types.LoadError::DecodeFailed("dehaze: 图像尺寸不能为负") - 23
} - 24
if img.channels != 3 && img.channels != 4 { - 25●
raise @types.LoadError::DecodeFailed("dehaze 需要 RGB/RGBA 图像") - 26
} - 27
let expected_size = img.width * img.height * img.channels - 28●
if img.data.length() != expected_size { - 29○
raise @types.LoadError::DecodeFailed( - 30
"dehaze: 数据大小不匹配,期望 " + - 31○
expected_size.to_string() + - 32
",实际 " + - 33○
img.data.length().to_string(), - 34
) - 35
} - 36
if patch_size < 1 || patch_size > 30 { - 37●
raise @types.LoadError::DecodeFailed("patch_size 必须在 1..30 之间") - 38
} - 39
if omega <= 0.0F || omega > 1.0F { - 40●
raise @types.LoadError::DecodeFailed("omega 必须在 (0, 1] 之间") - 41
} - 42
if t0 <= 0.0F || t0 > 1.0F { - 43●
raise @types.LoadError::DecodeFailed("t0 必须在 (0, 1] 之间") - 44
} - 45
let ch = img.channels - 46
let w = img.width - 47
let h = img.height - 48
let n = w * h - 49
if n == 0 { - 50●
return img - 51
} - 52
// 1. 计算暗通道 - 53●
let dark_channel = compute_dark_channel(img, w, h, ch, patch_size) - 54
// 2. 估计大气光 A(取暗通道中最亮的 0.1% 像素对应的原图最亮像素) - 55●
let a = estimate_atmospheric_light(img, dark_channel, w, h, ch) - 56●
let ar = Float::from_int(a[0]) - 57●
let ag = Float::from_int(a[1]) - 58●
let ab = Float::from_int(a[2]) - 59
// 3. 计算透射率 t = 1 - omega * dark(I/A) - 60●
let transmission : Array[Float] = Array::make(n, 0.0F) - 61
for i in 0..<n { - 62●
transmission[i] = 1.0F - omega * dark_channel[i] - 63
if transmission[i] < t0 { - 64●
transmission[i] = t0 - 65
} - 66
} - 67
// 4. 恢复无雾图像: J = (I - A) / t + A - 68●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 69
for i in 0..<n { - 70●
for c in 0..<3 { - 71●
let iv = Float::from_int(img.data[i * ch + c].to_int()) - 72●
let ac = if c == 0 { ar } else if c == 1 { ag } else { ab } - 73
let jv = (iv - ac) / transmission[i] + ac - 74●
out[i * ch + c] = jv.to_int().clamp(min=0, max=255).to_byte() - 75
} - 76
if ch == 4 { - 77●
out[i * ch + 3] = img.data[i * ch + 3] - 78
} - 79
} - 80
@types.Image::{ - 81
width: w, - 82
height: h, - 83
channels: ch, - 84●
data: Bytes::from_array(out), - 85
} - 86
} - 87
- 88
///| - 89
/// 计算暗通道 - 90
fn compute_dark_channel( - 91
img : @types.Image, - 92
w : Int32, - 93
h : Int32, - 94
ch : Int32, - 95
patch_size : Int32, - 96
) -> Array[Float] { - 97●
let n = w * h - 98
// 先计算每个像素的最小通道值 - 99●
let min_channel : Array[Int32] = Array::make(n, 255) - 100
for i in 0..<n { - 101●
let r = img.data[i * ch].to_int() - 102●
let g = img.data[i * ch + 1].to_int() - 103●
let b = img.data[i * ch + 2].to_int() - 104●
min_channel[i] = r.min(g).min(b) - 105
} - 106
// 最小值滤波,分离内部/边界区域 - 107●
let dark : Array[Float] = Array::make(n, 0.0F) - 108
// 内部区域:x in [patch_size, w-patch_size), y in [patch_size, h-patch_size) - 109
for y in patch_size..<(h - patch_size) { - 110●
for x in patch_size..<(w - patch_size) { - 111●
let mut min_val = 255 - 112
for py in (y - patch_size)..<=(y + patch_size) { - 113●
for px in (x - patch_size)..<=(x + patch_size) { - 114●
if min_channel[py * w + px] < min_val { - 115●
min_val = min_channel[py * w + px] - 116
} - 117
} - 118
} - 119●
dark[y * w + x] = Float::from_int(min_val) / 255.0F - 120
} - 121
} - 122
// 边界区域 - 123
for y in 0..<h { - 124●
let y_inner = y >= patch_size && y < h - patch_size - 125
for x in 0..<w { - 126●
if y_inner && x >= patch_size && x < w - patch_size { - 127●
continue - 128
} - 129
let mut min_val = 255 - 130●
let y_start = (y - patch_size).clamp(min=0, max=h - 1) - 131●
let y_end = (y + patch_size).clamp(min=0, max=h - 1) - 132●
let x_start = (x - patch_size).clamp(min=0, max=w - 1) - 133●
let x_end = (x + patch_size).clamp(min=0, max=w - 1) - 134
for py in y_start..<=y_end { - 135●
for px in x_start..<=x_end { - 136●
if min_channel[py * w + px] < min_val { - 137●
min_val = min_channel[py * w + px] - 138
} - 139
} - 140
} - 141●
dark[y * w + x] = Float::from_int(min_val) / 255.0F - 142
} - 143
} - 144
dark - 145
} - 146
- 147
///| - 148
/// 估计大气光 - 149
fn estimate_atmospheric_light( - 150
img : @types.Image, - 151
dark_channel : Array[Float], - 152
w : Int32, - 153
h : Int32, - 154
ch : Int32, - 155
) -> Array[Int32] { - 156●
let n = w * h - 157
// 取暗通道中最亮的 0.1% 像素 - 158●
let num_pixels = (n / 1000).max(1) - 159
// 收集暗通道值和索引 - 160
let pairs : Array[(Float, Int32)] = [] - 161
for i in 0..<n { - 162●
pairs.push((dark_channel[i], i)) - 163
} - 164
// 按暗通道值降序排序 - 165●
pairs.sort_by(fn(a, b) { b.0.compare(a.0) }) - 166
// 在这些像素中找原图最亮的像素 - 167
let mut max_r = 0 - 168
let mut max_g = 0 - 169
let mut max_b = 0 - 170
let mut max_sum = 0 - 171●
for i in 0..<num_pixels.min(pairs.length()) { - 172●
let idx = pairs[i].1 - 173●
let r = img.data[idx * ch].to_int() - 174●
let g = img.data[idx * ch + 1].to_int() - 175●
let b = img.data[idx * ch + 2].to_int() - 176
let sum = r + g + b - 177
if sum > max_sum { - 178●
max_sum = sum - 179
max_r = r - 180
max_g = g - 181
max_b = b - 182
} - 183
} - 184
[max_r, max_g, max_b] - 185
} - 186
- 187
///| - 188
/// 引导滤波(Guided Filter)用于优化透射率。 - 189
/// 适用于灰度图(channels=1)作为引导图。 - 190
pub fn guided_filter( - 191
img : @types.Image, - 192
input : Array[Float], - 193
radius~ : Int32 = 20, - 194
eps~ : Float = 0.001F, - 195
) -> Array[Float] raise @types.LoadError { - 196
// 输入验证 - 197●
if img.width < 0 || img.height < 0 { - 198○
raise @types.LoadError::DecodeFailed( - 199
"guided_filter: 图像尺寸不能为负", - 200
) - 201
} - 202
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 203●
raise @types.LoadError::DecodeFailed( - 204
"guided_filter 仅支持 channels=1/3/4", - 205
) - 206
} - 207
let expected_size = img.width * img.height * img.channels - 208●
if img.data.length() != expected_size { - 209○
raise @types.LoadError::DecodeFailed( - 210
"guided_filter: 数据大小不匹配,期望 " + - 211○
expected_size.to_string() + - 212
",实际 " + - 213○
img.data.length().to_string(), - 214
) - 215
} - 216
if radius < 1 { - 217●
raise @types.LoadError::DecodeFailed("guided_filter: radius必须>=1") - 218
} - 219
if eps <= 0.0F { - 220●
raise @types.LoadError::DecodeFailed("guided_filter: eps必须大于0") - 221
} - 222
let w = img.width - 223
let h = img.height - 224
let n = w * h - 225●
if n == 0 || input.length() != n { - 226●
return [] - 227
} - 228
// 转灰度作为引导图 - 229●
let guide : Array[Float] = Array::make(n, 0.0F) - 230
let ch = img.channels - 231
for i in 0..<n { - 232●
let v = if ch == 1 { - 233●
img.data[i].to_int() - 234
} else { - 235●
let r = img.data[i * ch].to_int() - 236●
let g = img.data[i * ch + 1].to_int() - 237●
let b = img.data[i * ch + 2].to_int() - 238
(r * 299 + g * 587 + b * 114) / 1000 - 239
} - 240●
guide[i] = Float::from_int(v) / 255.0F - 241
} - 242
// 计算均值 - 243●
let mean_i = box_filter(guide, w, h, radius) - 244●
let mean_p = box_filter(input, w, h, radius) - 245
// 计算 I*P 的均值 - 246●
let ip : Array[Float] = Array::make(n, 0.0F) - 247
for i in 0..<n { - 248●
ip[i] = guide[i] * input[i] - 249
} - 250●
let mean_ip = box_filter(ip, w, h, radius) - 251
// 协方差 - 252●
let cov_ip : Array[Float] = Array::make(n, 0.0F) - 253
for i in 0..<n { - 254●
cov_ip[i] = mean_ip[i] - mean_i[i] * mean_p[i] - 255
} - 256
// I² 的均值 - 257●
let i_sq : Array[Float] = Array::make(n, 0.0F) - 258
for i in 0..<n { - 259●
i_sq[i] = guide[i] * guide[i] - 260
} - 261●
let mean_i_sq = box_filter(i_sq, w, h, radius) - 262
// 方差 - 263●
let var_i : Array[Float] = Array::make(n, 0.0F) - 264
for i in 0..<n { - 265●
var_i[i] = mean_i_sq[i] - mean_i[i] * mean_i[i] - 266
} - 267
// a = cov_ip / (var_i + eps), b = mean_p - a * mean_i - 268●
let a : Array[Float] = Array::make(n, 0.0F) - 269●
let b : Array[Float] = Array::make(n, 0.0F) - 270
for i in 0..<n { - 271●
a[i] = cov_ip[i] / (var_i[i] + eps) - 272
b[i] = mean_p[i] - a[i] * mean_i[i] - 273
} - 274●
let mean_a = box_filter(a, w, h, radius) - 275●
let mean_b = box_filter(b, w, h, radius) - 276
// 输出 = mean_a * guide + mean_b - 277●
let out : Array[Float] = Array::make(n, 0.0F) - 278
for i in 0..<n { - 279●
out[i] = mean_a[i] * guide[i] + mean_b[i] - 280
} - 281
out - 282
} - 283
- 284
///| - 285
/// 盒式滤波(均值滤波),分离内部/边界区域以消除热点 clamp 调用 - 286
fn box_filter( - 287
data : Array[Float], - 288
w : Int32, - 289
h : Int32, - 290
radius : Int32, - 291
) -> Array[Float] { - 292●
let n = w * h - 293●
let out : Array[Float] = Array::make(n, 0.0F) - 294
// 内部区域 count 恒为 (2*radius+1)² - 295●
let inner_count = Float::from_int((2 * radius + 1) * (2 * radius + 1)) - 296
for y in radius..<(h - radius) { - 297●
for x in radius..<(w - radius) { - 298●
let mut sum = 0.0F - 299
for py in (y - radius)..<=(y + radius) { - 300●
for px in (x - radius)..<=(x + radius) { - 301●
sum += data[py * w + px] - 302
} - 303
} - 304
out[y * w + x] = sum / inner_count - 305
} - 306
} - 307
// 边界区域 - 308
for y in 0..<h { - 309●
let y_inner = y >= radius && y < h - radius - 310
for x in 0..<w { - 311●
if y_inner && x >= radius && x < w - radius { - 312●
continue - 313
} - 314
let mut sum = 0.0F - 315
let mut count = 0 - 316●
let y_start = (y - radius).clamp(min=0, max=h - 1) - 317●
let y_end = (y + radius).clamp(min=0, max=h - 1) - 318●
let x_start = (x - radius).clamp(min=0, max=w - 1) - 319●
let x_end = (x + radius).clamp(min=0, max=w - 1) - 320
for py in y_start..<=y_end { - 321●
for px in x_start..<=x_end { - 322●
sum += data[py * w + px] - 323
count += 1 - 324
} - 325
} - 326●
out[y * w + x] = sum / Float::from_int(count) - 327
} - 328
} - 329
out - 330
}