image.process\enhance\denoise.mbt
83% · 88 / 106 points · 353 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 降噪 denoise - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 图像去噪(v12.1.0) - 7
// 非局部均值(NLM)+ 双边滤波增强 + 中值滤波增强 - 8
- 9
///| - 10
/// 非局部均值去噪配置 - 11
pub(all) struct NlmConfig { - 12
search_window : Int32 // 搜索窗口大小 - 13
patch_size : Int32 // 补丁大小 - 14
h : Double // 滤波强度参数 - 15
sigma : Double // 噪声标准差 - 16
} - 17
- 18
///| - 19
/// 创建默认 NLM 配置 - 20
pub fn nlm_default_config() -> NlmConfig { - 21●
NlmConfig::{ search_window: 21, patch_size: 7, h: 10.0, sigma: 10.0, } - 22
} - 23
- 24
///| - 25
/// 非局部均值去噪(简化版) - 26
/// 基于图像块相似度的加权平均去噪 - 27
pub fn denoise_nlm( - 28
img : @types.Image, - 29
config? : NlmConfig, - 30
) -> @types.Image raise @types.LoadError { - 31
// 输入验证 - 32●
if img.width <= 0 || img.height <= 0 { - 33○
raise @types.LoadError::DecodeFailed( - 34
"denoise_nlm: 图像尺寸必须大于0", - 35
) - 36
} - 37
if img.channels < 1 || img.channels > 4 { - 38●
raise @types.LoadError::DecodeFailed( - 39
"denoise_nlm: 通道数必须在1-4之间", - 40
) - 41
} - 42
let expected_size = img.width * img.height * img.channels - 43●
if img.data.length() != expected_size { - 44●
raise @types.LoadError::DecodeFailed( - 45
"denoise_nlm: 数据大小不匹配,期望 " + - 46●
expected_size.to_string() + - 47
",实际 " + - 48●
img.data.length().to_string(), - 49
) - 50
} - 51
- 52●
let cfg = match config { - 53●
Some(c) => c - 54●
None => nlm_default_config() - 55
} - 56
- 57
// 参数验证 - 58
if cfg.search_window <= 0 { - 59○
raise @types.LoadError::DecodeFailed( - 60
"denoise_nlm: search_window必须大于0", - 61
) - 62
} - 63
if cfg.patch_size <= 0 { - 64○
raise @types.LoadError::DecodeFailed("denoise_nlm: patch_size必须大于0") - 65
} - 66
if cfg.h <= 0.0 { - 67●
raise @types.LoadError::DecodeFailed("denoise_nlm: h必须大于0") - 68
} - 69
- 70
let w = img.width - 71
let h = img.height - 72
let channels = img.channels - 73
let data = img.data - 74●
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00') - 75
let half_search = cfg.search_window / 2 - 76
let half_patch = cfg.patch_size / 2 - 77
let h2 = cfg.h * cfg.h - 78
for y in 0..<h { - 79●
for x in 0..<w { - 80●
for c in 0..<channels { - 81●
let mut weighted_sum = 0.0 - 82
let mut weight_sum = 0.0 - 83
// 在搜索窗口内寻找相似块 - 84
for dy in -half_search..<(half_search + 1) { - 85●
for dx in -half_search..<(half_search + 1) { - 86●
let nx = x + dx - 87
let ny = y + dy - 88
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 89
// 计算两个块的相似度(欧氏距离) - 90●
let mut dist = 0.0 - 91
let mut count = 0 - 92
for py in -half_patch..<(half_patch + 1) { - 93●
for px in -half_patch..<(half_patch + 1) { - 94●
let p1x = x + px - 95
let p1y = y + py - 96
let p2x = nx + px - 97
let p2y = ny + py - 98
if p1x >= 0 && - 99
p1x < w && - 100
p1y >= 0 && - 101
p1y < h && - 102
p2x >= 0 && - 103
p2x < w && - 104
p2y >= 0 && - 105
p2y < h { - 106●
let v1 = data[(p1y * w + p1x) * channels + c].to_double() - 107●
let v2 = data[(p2y * w + p2x) * channels + c].to_double() - 108
let diff = v1 - v2 - 109
dist += diff * diff - 110
count += 1 - 111
} - 112
} - 113
} - 114
if count > 0 { - 115●
dist = dist / count.to_double() - 116
// 高斯权重 - 117●
let weight = @math.exp(-dist / h2) - 118●
weighted_sum += data[(ny * w + nx) * channels + c].to_double() * - 119
weight - 120
weight_sum += weight - 121
} - 122
} - 123
} - 124
} - 125
if weight_sum > 0.0 { - 126●
out_data[(y * w + x) * channels + c] = (weighted_sum / weight_sum) - 127●
.min(255.0) - 128●
.max(0.0) - 129●
.to_int() - 130●
.to_byte() - 131
} else { - 132○
out_data[(y * w + x) * channels + c] = data[(y * w + x) * channels + c] - 133
} - 134
} - 135
} - 136
} - 137
@types.Image::{ - 138
width: w, - 139
height: h, - 140
channels, - 141●
data: Bytes::from_array(out_data), - 142
} - 143
} - 144
- 145
///| - 146
/// 双边滤波增强版 - 147
/// 同时考虑空间距离和像素值差异 - 148
pub fn denoise_bilateral( - 149
img : @types.Image, - 150
diameter~ : Int32 = 9, - 151
sigma_color~ : Double = 75.0, - 152
sigma_space~ : Double = 75.0, - 153
) -> @types.Image raise @types.LoadError { - 154
// 输入验证 - 155●
if img.width <= 0 || img.height <= 0 { - 156○
raise @types.LoadError::DecodeFailed( - 157
"denoise_bilateral: 图像尺寸必须大于0", - 158
) - 159
} - 160
if img.channels < 1 || img.channels > 4 { - 161●
raise @types.LoadError::DecodeFailed( - 162
"denoise_bilateral: 通道数必须在1-4之间", - 163
) - 164
} - 165
let expected_size = img.width * img.height * img.channels - 166●
if img.data.length() != expected_size { - 167●
raise @types.LoadError::DecodeFailed( - 168
"denoise_bilateral: 数据大小不匹配,期望 " + - 169●
expected_size.to_string() + - 170
",实际 " + - 171●
img.data.length().to_string(), - 172
) - 173
} - 174
if diameter <= 0 { - 175●
raise @types.LoadError::DecodeFailed( - 176
"denoise_bilateral: diameter必须大于0", - 177
) - 178
} - 179
if sigma_color <= 0.0 { - 180●
raise @types.LoadError::DecodeFailed( - 181
"denoise_bilateral: sigma_color必须大于0", - 182
) - 183
} - 184
if sigma_space <= 0.0 { - 185○
raise @types.LoadError::DecodeFailed( - 186
"denoise_bilateral: sigma_space必须大于0", - 187
) - 188
} - 189
- 190
let d = diameter - 191
let sc = sigma_color - 192
let ss = sigma_space - 193
let w = img.width - 194
let h = img.height - 195
let channels = img.channels - 196
let data = img.data - 197●
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00') - 198
let half = d / 2 - 199
for y in 0..<h { - 200●
for x in 0..<w { - 201●
for c in 0..<channels { - 202●
let center_val = data[(y * w + x) * channels + c].to_double() - 203
let mut weighted_sum = 0.0 - 204
let mut weight_sum = 0.0 - 205
for dy in -half..<(half + 1) { - 206●
for dx in -half..<(half + 1) { - 207●
let nx = x + dx - 208
let ny = y + dy - 209
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 210●
let neighbor_val = data[(ny * w + nx) * channels + c].to_double() - 211
// 空间距离权重 - 212●
let spatial_dist = (dx * dx + dy * dy).to_double().sqrt() - 213●
let spatial_weight = @math.exp( - 214
-(spatial_dist * spatial_dist) / (2.0 * ss * ss), - 215
) - 216
// 像素值差异权重 - 217●
let color_diff = (center_val - neighbor_val).abs() - 218●
let color_weight = @math.exp( - 219
-(color_diff * color_diff) / (2.0 * sc * sc), - 220
) - 221
// 组合权重 - 222
let weight = spatial_weight * color_weight - 223
weighted_sum += neighbor_val * weight - 224
weight_sum += weight - 225
} - 226
} - 227
} - 228
if weight_sum > 0.0 { - 229●
out_data[(y * w + x) * channels + c] = (weighted_sum / weight_sum) - 230●
.min(255.0) - 231●
.max(0.0) - 232●
.to_int() - 233●
.to_byte() - 234
} else { - 235○
out_data[(y * w + x) * channels + c] = data[(y * w + x) * channels + c] - 236
} - 237
} - 238
} - 239
} - 240
@types.Image::{ - 241
width: w, - 242
height: h, - 243
channels, - 244●
data: Bytes::from_array(out_data), - 245
} - 246
} - 247
- 248
///| - 249
/// 中值滤波增强版(支持任意核大小) - 250
pub fn denoise_median( - 251
img : @types.Image, - 252
kernel_size~ : Int32 = 3, - 253
) -> @types.Image raise @types.LoadError { - 254
// 输入验证 - 255●
if img.width <= 0 || img.height <= 0 { - 256○
raise @types.LoadError::DecodeFailed( - 257
"denoise_median: 图像尺寸必须大于0", - 258
) - 259
} - 260
if img.channels < 1 || img.channels > 4 { - 261●
raise @types.LoadError::DecodeFailed( - 262
"denoise_median: 通道数必须在1-4之间", - 263
) - 264
} - 265
let expected_size = img.width * img.height * img.channels - 266●
if img.data.length() != expected_size { - 267○
raise @types.LoadError::DecodeFailed( - 268
"denoise_median: 数据大小不匹配,期望 " + - 269○
expected_size.to_string() + - 270
",实际 " + - 271○
img.data.length().to_string(), - 272
) - 273
} - 274
if kernel_size <= 0 || kernel_size % 2 == 0 { - 275●
raise @types.LoadError::DecodeFailed( - 276
"denoise_median: kernel_size必须是正奇数", - 277
) - 278
} - 279
- 280
let ks = kernel_size - 281
let w = img.width - 282
let h = img.height - 283
let channels = img.channels - 284
let data = img.data - 285●
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00') - 286
let half = ks / 2 - 287
for y in 0..<h { - 288●
for x in 0..<w { - 289●
for c in 0..<channels { - 290
// 收集邻域像素 - 291●
let neighborhood : Array[Int32] = [] - 292
for dy in -half..<(half + 1) { - 293●
for dx in -half..<(half + 1) { - 294●
let nx = x + dx - 295
let ny = y + dy - 296
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 297●
neighborhood.push(data[(ny * w + nx) * channels + c].to_int()) - 298
} - 299
} - 300
} - 301
// 排序取中值(选择排序) - 302●
let n = neighborhood.length() - 303
for i in 0..<(n / 2 + 1) { - 304●
let mut min_idx = i - 305
for j in (i + 1)..<n { - 306●
if neighborhood[j] < neighborhood[min_idx] { - 307●
min_idx = j - 308
} - 309
} - 310
if min_idx != i { - 311●
let tmp = neighborhood[i] - 312
neighborhood[i] = neighborhood[min_idx] - 313
neighborhood[min_idx] = tmp - 314
} - 315
} - 316
let median = if n > 0 { - 317●
neighborhood[n / 2] - 318
} else { - 319○
data[(y * w + x) * channels + c].to_int() - 320
} - 321●
out_data[(y * w + x) * channels + c] = median.to_byte() - 322
} - 323
} - 324
} - 325
@types.Image::{ - 326
width: w, - 327
height: h, - 328
channels, - 329●
data: Bytes::from_array(out_data), - 330
} - 331
} - 332
- 333
///| - 334
/// 通用去噪接口 - 335
pub fn denoise( - 336
img : @types.Image, - 337
method_type : String, - 338
) -> @types.Image raise @types.LoadError { - 339
// 输入验证 - 340●
if img.width <= 0 || img.height <= 0 { - 341○
raise @types.LoadError::DecodeFailed("denoise: 图像尺寸必须大于0") - 342
} - 343
if img.channels < 1 || img.channels > 4 { - 344●
raise @types.LoadError::DecodeFailed("denoise: 通道数必须在1-4之间") - 345
} - 346
- 347●
match method_type { - 348●
"nlm" => denoise_nlm(img) - 349○
"bilateral" => denoise_bilateral(img) - 350○
"median" => denoise_median(img) - 351○
_ => denoise_bilateral(img) - 352
} - 353
}