image.process\filter\nlm_denoise.mbt
85.5% · 124 / 145 points · 416 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 非局部均值降噪 nlm denoise - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 非局部均值去噪 NLM(纯 MoonBit) - 7
- 8
///| - 9
/// 非局部均值去噪(Non-Local Means)。 - 10
/// 通过搜索窗口内相似像素块的加权平均来去噪。 - 11
/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4)。 - 12
/// patch_size 为相似性比较的块半径(默认 3,即 7x7 块)。 - 13
/// search_size 为搜索窗口半径(默认 7,即 15x15 窗口)。 - 14
/// h 为滤波强度参数(默认 10),越大平滑越强。 - 15
pub fn nlm_denoise( - 16
img : @types.Image, - 17
patch_size~ : Int32 = 3, - 18
search_size~ : Int32 = 7, - 19
h~ : Int32 = 10, - 20
) -> @types.Image raise @types.LoadError { - 21
// 输入验证 - 22●
if img.width <= 0 || img.height <= 0 { - 23●
raise @types.LoadError::DecodeFailed( - 24
"nlm_denoise: 图像尺寸必须大于0", - 25
) - 26
} - 27
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 28●
raise @types.LoadError::DecodeFailed( - 29
"nlm_denoise: 仅支持 channels=1/3/4", - 30
) - 31
} - 32
let expected_size = img.width * img.height * img.channels - 33●
if img.data.length() != expected_size { - 34○
raise @types.LoadError::DecodeFailed( - 35
"nlm_denoise: 数据大小不匹配,期望 " + - 36○
expected_size.to_string() + - 37
",实际 " + - 38○
img.data.length().to_string(), - 39
) - 40
} - 41
if patch_size < 1 || patch_size > 10 { - 42●
raise @types.LoadError::DecodeFailed("patch_size 必须在 1..10 之间") - 43
} - 44
if search_size < 1 || search_size > 20 { - 45●
raise @types.LoadError::DecodeFailed("search_size 必须在 1..20 之间") - 46
} - 47
if h < 1 { - 48●
raise @types.LoadError::DecodeFailed("h 必须 >= 1") - 49
} - 50
let ch = img.channels - 51
let w = img.width - 52
let h_img = img.height - 53
let n = w * h_img - 54
if n == 0 { - 55○
return img - 56
} - 57●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 58
let h_sq = h * h - 59
let patch_d = patch_size - 60
let search_d = search_size - 61
// 预计算常数 - 62
let patch_area = (2 * patch_d + 1) * (2 * patch_d + 1) - 63
// 预计算像素 Int 数组,避免重复 .to_int() 转换 - 64●
let pixels : Array[Int32] = Array::make(n * ch, 0) - 65
for i in 0..<(n * ch) { - 66●
pixels[i] = img.data[i].to_int() - 67
} - 68●
let denom = Float::from_int(h_sq * patch_area) - 69●
let early_term = (denom * 6.907755278982137F).to_int() - 70
let inner_start = search_d + patch_d - 71
// 内部区域:搜索窗口和块都完全在图像内 - 72
for y in inner_start..<(h_img - inner_start) { - 73●
for x in inner_start..<(w - inner_start) { - 74●
for c in 0..<ch { - 75●
if ch == 4 && c == 3 { - 76●
out[(y * w + x) * ch + 3] = img.data[(y * w + x) * ch + 3] - 77
continue - 78
} - 79
let mut weight_sum = 0.0F - 80
let mut value_sum = 0.0F - 81
for sy in (y - search_d)..<=(y + search_d) { - 82●
let row_sy = sy * w - 83
for sx in (x - search_d)..<=(x + search_d) { - 84●
let dist = patch_distance_inner( - 85
pixels, x, y, sx, sy, w, ch, c, patch_d, early_term, - 86
) - 87
if dist >= early_term { - 88○
continue - 89
} - 90●
let weight = @math.expf(Float::from_int(-dist) / denom) - 91
weight_sum += weight - 92
value_sum += weight * - 93●
Float::from_int(pixels[(row_sy + sx) * ch + c]) - 94
} - 95
} - 96
let val = if weight_sum > 0.0F { - 97●
(value_sum / weight_sum).to_int() - 98
} else { - 99○
pixels[(y * w + x) * ch + c] - 100
} - 101●
out[(y * w + x) * ch + c] = val.clamp(min=0, max=255).to_byte() - 102
} - 103
} - 104
} - 105
// 边界区域 - 106
for y in 0..<h_img { - 107●
let y_inner = y >= inner_start && y < h_img - inner_start - 108
for x in 0..<w { - 109●
if y_inner && x >= inner_start && x < w - inner_start { - 110●
continue - 111
} - 112
for c in 0..<ch { - 113●
if ch == 4 && c == 3 { - 114●
out[(y * w + x) * ch + 3] = img.data[(y * w + x) * ch + 3] - 115
continue - 116
} - 117
let mut weight_sum = 0.0F - 118
let mut value_sum = 0.0F - 119●
let sy_start = if y - search_d < 0 { 0 } else { y - search_d } - 120
let sy_end = if y + search_d > h_img - 1 { - 121●
h_img - 1 - 122
} else { - 123●
y + search_d - 124
} - 125●
let sx_start = if x - search_d < 0 { 0 } else { x - search_d } - 126●
let sx_end = if x + search_d > w - 1 { w - 1 } else { x + search_d } - 127
for sy in sy_start..<=sy_end { - 128●
for sx in sx_start..<=sx_end { - 129●
let dist = patch_distance_fast( - 130
pixels, x, y, sx, sy, w, h_img, ch, c, patch_d, early_term, - 131
) - 132
if dist >= early_term { - 133●
continue - 134
} - 135●
let weight = @math.expf(Float::from_int(-dist) / denom) - 136
weight_sum += weight - 137
value_sum += weight * - 138●
Float::from_int(pixels[(sy * w + sx) * ch + c]) - 139
} - 140
} - 141
let val = if weight_sum > 0.0F { - 142●
(value_sum / weight_sum).to_int() - 143
} else { - 144○
pixels[(y * w + x) * ch + c] - 145
} - 146●
out[(y * w + x) * ch + c] = val.clamp(min=0, max=255).to_byte() - 147
} - 148
} - 149
} - 150
@types.Image::{ - 151
width: w, - 152
height: h_img, - 153
channels: ch, - 154●
data: Bytes::from_array(out), - 155
} - 156
} - 157
- 158
///| - 159
/// 计算两个像素块之间的平方距离(内部区域,无边界检查) - 160
fn patch_distance_inner( - 161
pixels : Array[Int32], - 162
x1 : Int32, - 163
y1 : Int32, - 164
x2 : Int32, - 165
y2 : Int32, - 166
w : Int32, - 167
ch : Int32, - 168
c : Int32, - 169
patch_d : Int32, - 170
early_term : Int32, - 171
) -> Int32 { - 172●
let mut dist = 0 - 173
for dy in -patch_d..<=patch_d { - 174●
let row1 = (y1 + dy) * w - 175
let row2 = (y2 + dy) * w - 176
for dx in -patch_d..<=patch_d { - 177●
let v1 = pixels[(row1 + x1 + dx) * ch + c] - 178
let v2 = pixels[(row2 + x2 + dx) * ch + c] - 179
let diff = v1 - v2 - 180
dist += diff * diff - 181
if dist >= early_term { - 182○
return dist - 183
} - 184
} - 185
} - 186
dist - 187
} - 188
- 189
///| - 190
/// 计算两个像素块之间的平方距离(使用预计算 Int 数组,内联边界检查) - 191
fn patch_distance_fast( - 192
pixels : Array[Int32], - 193
x1 : Int32, - 194
y1 : Int32, - 195
x2 : Int32, - 196
y2 : Int32, - 197
w : Int32, - 198
h : Int32, - 199
ch : Int32, - 200
c : Int32, - 201
patch_d : Int32, - 202
early_term : Int32, - 203
) -> Int32 { - 204●
let mut dist = 0 - 205
let w_max = w - 1 - 206
let h_max = h - 1 - 207
for dy in -patch_d..<=patch_d { - 208●
for dx in -patch_d..<=patch_d { - 209
// 内联边界检查,避免 clamp 方法调用 - 210●
let raw1x = x1 + dx - 211
let raw1y = y1 + dy - 212
let raw2x = x2 + dx - 213
let raw2y = y2 + dy - 214
let px1 = if raw1x < 0 { - 215●
0 - 216●
} else if raw1x > w_max { - 217●
w_max - 218
} else { - 219●
raw1x - 220
} - 221
let py1 = if raw1y < 0 { - 222●
0 - 223●
} else if raw1y > h_max { - 224●
h_max - 225
} else { - 226●
raw1y - 227
} - 228
let px2 = if raw2x < 0 { - 229●
0 - 230●
} else if raw2x > w_max { - 231●
w_max - 232
} else { - 233●
raw2x - 234
} - 235
let py2 = if raw2y < 0 { - 236●
0 - 237●
} else if raw2y > h_max { - 238●
h_max - 239
} else { - 240●
raw2y - 241
} - 242
let v1 = pixels[(py1 * w + px1) * ch + c] - 243
let v2 = pixels[(py2 * w + px2) * ch + c] - 244
let diff = v1 - v2 - 245
dist += diff * diff - 246
if dist >= early_term { - 247●
return dist - 248
} - 249
} - 250
} - 251
dist - 252
} - 253
- 254
///| - 255
- 256
///| - 257
/// 快速 NLM 去噪(降采样搜索)。 - 258
/// 使用积分图加速块距离计算,搜索步长 > 1 以加速。 - 259
/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4)。 - 260
pub fn nlm_denoise_fast( - 261
img : @types.Image, - 262
patch_size~ : Int32 = 3, - 263
search_size~ : Int32 = 7, - 264
h~ : Int32 = 10, - 265
step~ : Int32 = 2, - 266
) -> @types.Image raise @types.LoadError { - 267
// 输入验证 - 268●
if img.width <= 0 || img.height <= 0 { - 269●
raise @types.LoadError::DecodeFailed( - 270
"nlm_denoise_fast: 图像尺寸必须大于0", - 271
) - 272
} - 273
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 274●
raise @types.LoadError::DecodeFailed( - 275
"nlm_denoise_fast: 仅支持 channels=1/3/4", - 276
) - 277
} - 278
let expected_size = img.width * img.height * img.channels - 279●
if img.data.length() != expected_size { - 280○
raise @types.LoadError::DecodeFailed( - 281
"nlm_denoise_fast: 数据大小不匹配,期望 " + - 282○
expected_size.to_string() + - 283
",实际 " + - 284○
img.data.length().to_string(), - 285
) - 286
} - 287
if patch_size < 1 || patch_size > 10 { - 288○
raise @types.LoadError::DecodeFailed("patch_size 必须在 1..10 之间") - 289
} - 290
if search_size < 1 || search_size > 20 { - 291○
raise @types.LoadError::DecodeFailed("search_size 必须在 1..20 之间") - 292
} - 293
if h < 1 { - 294○
raise @types.LoadError::DecodeFailed("h 必须 >= 1") - 295
} - 296
if step < 1 || step > 5 { - 297●
raise @types.LoadError::DecodeFailed("step 必须在 1..5 之间") - 298
} - 299
let ch = img.channels - 300
let w = img.width - 301
let h_img = img.height - 302
let n = w * h_img - 303
if n == 0 { - 304○
return img - 305
} - 306●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 307
let h_sq = h * h - 308
let patch_d = patch_size - 309
let search_d = search_size - 310
let patch_area = (2 * patch_d + 1) * (2 * patch_d + 1) - 311
// 预计算像素 Int 数组 - 312●
let pixels : Array[Int32] = Array::make(n * ch, 0) - 313
for i in 0..<(n * ch) { - 314●
pixels[i] = img.data[i].to_int() - 315
} - 316●
let denom = Float::from_int(h_sq * patch_area) - 317●
let early_term = (denom * 6.907755278982137F).to_int() - 318
let inner_start = search_d + patch_d - 319
// 内部区域:搜索窗口和块都完全在图像内 - 320
for y in inner_start..<(h_img - inner_start) { - 321●
for x in inner_start..<(w - inner_start) { - 322●
for c in 0..<ch { - 323●
if ch == 4 && c == 3 { - 324○
out[(y * w + x) * ch + 3] = img.data[(y * w + x) * ch + 3] - 325
continue - 326
} - 327
let mut weight_sum = 0.0F - 328
let mut value_sum = 0.0F - 329
let mut sy = y - search_d - 330
let sy_end = y + search_d - 331
while sy <= sy_end { - 332●
let row_sy = sy * w - 333
let mut sx = x - search_d - 334
let sx_end = x + search_d - 335
while sx <= sx_end { - 336●
let dist = patch_distance_inner( - 337
pixels, x, y, sx, sy, w, ch, c, patch_d, early_term, - 338
) - 339
if dist >= early_term { - 340○
sx += step - 341
continue - 342
} - 343●
let weight = @math.expf(Float::from_int(-dist) / denom) - 344
weight_sum += weight - 345
value_sum += weight * - 346●
Float::from_int(pixels[(row_sy + sx) * ch + c]) - 347
sx += step - 348
} - 349
sy += step - 350
} - 351
let val = if weight_sum > 0.0F { - 352●
(value_sum / weight_sum).to_int() - 353
} else { - 354○
pixels[(y * w + x) * ch + c] - 355
} - 356●
out[(y * w + x) * ch + c] = val.clamp(min=0, max=255).to_byte() - 357
} - 358
} - 359
} - 360
// 边界区域 - 361
for y in 0..<h_img { - 362●
let y_inner = y >= inner_start && y < h_img - inner_start - 363
for x in 0..<w { - 364●
if y_inner && x >= inner_start && x < w - inner_start { - 365●
continue - 366
} - 367
for c in 0..<ch { - 368●
if ch == 4 && c == 3 { - 369○
out[(y * w + x) * ch + 3] = img.data[(y * w + x) * ch + 3] - 370
continue - 371
} - 372
let mut weight_sum = 0.0F - 373
let mut value_sum = 0.0F - 374●
let sy_start = if y - search_d < 0 { 0 } else { y - search_d } - 375
let sy_end = if y + search_d > h_img - 1 { - 376●
h_img - 1 - 377
} else { - 378●
y + search_d - 379
} - 380●
let sx_start = if x - search_d < 0 { 0 } else { x - search_d } - 381●
let sx_end = if x + search_d > w - 1 { w - 1 } else { x + search_d } - 382
let mut sy = sy_start - 383
while sy <= sy_end { - 384●
let mut sx = sx_start - 385
while sx <= sx_end { - 386●
let dist = patch_distance_fast( - 387
pixels, x, y, sx, sy, w, h_img, ch, c, patch_d, early_term, - 388
) - 389
if dist >= early_term { - 390●
sx += step - 391
continue - 392
} - 393●
let weight = @math.expf(Float::from_int(-dist) / denom) - 394
weight_sum += weight - 395
value_sum += weight * - 396●
Float::from_int(pixels[(sy * w + sx) * ch + c]) - 397
sx += step - 398
} - 399
sy += step - 400
} - 401
let val = if weight_sum > 0.0F { - 402●
(value_sum / weight_sum).to_int() - 403
} else { - 404●
pixels[(y * w + x) * ch + c] - 405
} - 406●
out[(y * w + x) * ch + c] = val.clamp(min=0, max=255).to_byte() - 407
} - 408
} - 409
} - 410
@types.Image::{ - 411
width: w, - 412
height: h_img, - 413
channels: ch, - 414●
data: Bytes::from_array(out), - 415
} - 416
}