image.process\frequency\haar_wavelet.mbt
95.2% · 79 / 83 points · 264 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # Haar 小波 haar wavelet - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// Haar 小波变换(纯 MoonBit) - 7
- 8
///| - 9
/// Haar 小波变换结果 - 10
pub struct HaarWaveletResult { - 11
width : Int32 - 12
height : Int32 - 13
levels : Int32 // 分解层数 - 14
coefficients : Array[Float] // 小波系数 - 15
} - 16
- 17
///| - 18
/// 一维 Haar 小波变换(原地)。 - 19
/// 对长度为 n(2 的幂)的数组进行 Haar 变换。 - 20
pub fn haar_transform_1d(data : Array[Float]) -> Array[Float] { - 21●
let n = data.length() - 22
if n < 2 { - 23●
return data.copy() - 24
} - 25●
let out = data.copy() - 26●
let sqrt2 = 2.0F.sqrt() - 27
let mut len = n - 28
while len > 1 { - 29●
let half = len / 2 - 30●
let temp : Array[Float] = Array::make(len, 0.0F) - 31
for i in 0..<half { - 32●
temp[i] = (out[2 * i] + out[2 * i + 1]) / sqrt2 - 33
temp[half + i] = (out[2 * i] - out[2 * i + 1]) / sqrt2 - 34
} - 35
for i in 0..<len { - 36●
out[i] = temp[i] - 37
} - 38
len = half - 39
} - 40
out - 41
} - 42
- 43
///| - 44
/// 一维 Haar 小波逆变换。 - 45
pub fn ihaar_transform_1d(data : Array[Float]) -> Array[Float] { - 46●
let n = data.length() - 47
if n < 2 { - 48●
return data.copy() - 49
} - 50●
let out = data.copy() - 51●
let sqrt2 = 2.0F.sqrt() - 52
// 从最小级别开始逆变换 - 53
let mut len = 2 - 54
while len <= n { - 55●
let half = len / 2 - 56●
let temp : Array[Float] = Array::make(len, 0.0F) - 57
for i in 0..<half { - 58●
temp[2 * i] = (out[i] + out[half + i]) / sqrt2 - 59
temp[2 * i + 1] = (out[i] - out[half + i]) / sqrt2 - 60
} - 61
for i in 0..<len { - 62●
out[i] = temp[i] - 63
} - 64
len *= 2 - 65
} - 66
out - 67
} - 68
- 69
///| - 70
/// 二维 Haar 小波变换。 - 71
/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4,自动转灰度)。 - 72
/// levels 为分解层数。 - 73
pub fn haar_transform_2d( - 74
img : @types.Image, - 75
levels~ : Int32 = 1, - 76
) -> HaarWaveletResult raise @types.LoadError { - 77●
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 78●
raise @types.LoadError::DecodeFailed( - 79
"haar_transform_2d 仅支持 channels=1/3/4", - 80
) - 81
} - 82
if levels < 1 || levels > 10 { - 83●
raise @types.LoadError::DecodeFailed("levels 必须在 1..10 之间") - 84
} - 85
let w = img.width - 86
let h = img.height - 87
let n = w * h - 88
if n == 0 { - 89○
return HaarWaveletResult::{ - 90
width: w, - 91
height: h, - 92
levels: 0, - 93
coefficients: [], - 94
} - 95
} - 96
// 转灰度为 Float - 97●
let coeffs : Array[Float] = Array::make(n, 0.0F) - 98
let ch = img.channels - 99●
let data_int : Array[Int32] = Array::make(n * ch, 0) - 100
for i in 0..<(n * ch) { - 101●
data_int[i] = img.data[i].to_int() - 102
} - 103
for i in 0..<n { - 104●
let v = if ch == 1 { - 105●
data_int[i] - 106
} else { - 107●
let r = data_int[i * ch] - 108
let g = data_int[i * ch + 1] - 109
let b = data_int[i * ch + 2] - 110
(r * 299 + g * 587 + b * 114) / 1000 - 111
} - 112●
coeffs[i] = Float::from_int(v) - 113
} - 114
// 多级分解 - 115
let mut cur_w = w - 116
let mut cur_h = h - 117
for _ in 0..<levels { - 118●
if cur_w < 2 || cur_h < 2 { - 119●
break - 120
} - 121
// 行变换 - 122
for y in 0..<cur_h { - 123●
let row : Array[Float] = Array::make(cur_w, 0.0F) - 124
for x in 0..<cur_w { - 125●
row[x] = coeffs[y * w + x] - 126
} - 127●
let transformed = haar_transform_1d(row) - 128
for x in 0..<cur_w { - 129●
coeffs[y * w + x] = transformed[x] - 130
} - 131
} - 132
// 列变换 - 133
for x in 0..<cur_w { - 134●
let col : Array[Float] = Array::make(cur_h, 0.0F) - 135
for y in 0..<cur_h { - 136●
col[y] = coeffs[y * w + x] - 137
} - 138●
let transformed = haar_transform_1d(col) - 139
for y in 0..<cur_h { - 140●
coeffs[y * w + x] = transformed[y] - 141
} - 142
} - 143
cur_w /= 2 - 144
cur_h /= 2 - 145
} - 146
HaarWaveletResult::{ width: w, height: h, levels, coefficients: coeffs, } - 147
} - 148
- 149
///| - 150
/// 二维 Haar 小波逆变换。 - 151
pub fn haar_inverse_transform_2d(result : HaarWaveletResult) -> @types.Image { - 152●
let w = result.width - 153
let h = result.height - 154
let n = w * h - 155
if n == 0 { - 156○
return @types.Image::{ - 157
width: 0, - 158
height: 0, - 159
channels: 1, - 160○
data: Bytes::from_array([]), - 161
} - 162
} - 163●
let coeffs = result.coefficients.copy() - 164
// 多级重构(从最小级别开始) - 165
let mut cur_w = w - 166
let mut cur_h = h - 167
for _ in 0..<result.levels { - 168●
if cur_w < 2 || cur_h < 2 { - 169●
break - 170
} - 171
cur_w /= 2 - 172
cur_h /= 2 - 173
} - 174
for _ in 0..<result.levels { - 175●
if cur_w * 2 > w || cur_h * 2 > h { - 176●
break - 177
} - 178
cur_w *= 2 - 179
cur_h *= 2 - 180
// 列逆变换 - 181
for x in 0..<cur_w { - 182●
let col : Array[Float] = Array::make(cur_h, 0.0F) - 183
for y in 0..<cur_h { - 184●
col[y] = coeffs[y * w + x] - 185
} - 186●
let inversed = ihaar_transform_1d(col) - 187
for y in 0..<cur_h { - 188●
coeffs[y * w + x] = inversed[y] - 189
} - 190
} - 191
// 行逆变换 - 192
for y in 0..<cur_h { - 193●
let row : Array[Float] = Array::make(cur_w, 0.0F) - 194
for x in 0..<cur_w { - 195●
row[x] = coeffs[y * w + x] - 196
} - 197●
let inversed = ihaar_transform_1d(row) - 198
for x in 0..<cur_w { - 199●
coeffs[y * w + x] = inversed[x] - 200
} - 201
} - 202
} - 203
// 转为图像 - 204●
let out : Array[Byte] = Array::make(n, b'\x00') - 205
for i in 0..<n { - 206●
let val = coeffs[i].to_int() - 207●
out[i] = val.clamp(min=0, max=255).to_byte() - 208
} - 209
@types.Image::{ - 210
width: w, - 211
height: h, - 212
channels: 1, - 213●
data: Bytes::from_array(out), - 214
} - 215
} - 216
- 217
///| - 218
/// Haar 小波去噪(阈值去噪)。 - 219
/// 对小波系数应用软阈值或硬阈值,然后逆变换重构。 - 220
/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4,自动转灰度)。 - 221
/// threshold 为阈值,soft=true 为软阈值,false 为硬阈值。 - 222
pub fn haar_denoise( - 223
img : @types.Image, - 224
threshold~ : Float = 10.0F, - 225
soft~ : Bool = true, - 226
levels~ : Int32 = 3, - 227
) -> @types.Image raise @types.LoadError { - 228●
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 229●
raise @types.LoadError::DecodeFailed( - 230
"haar_denoise 仅支持 channels=1/3/4", - 231
) - 232
} - 233●
let result = haar_transform_2d(img, levels~) - 234
let w = result.width - 235
let h = result.height - 236
let coeffs = result.coefficients - 237
// 阈值处理(保留低频系数不变) - 238
let low_w = w >> levels - 239
let low_h = h >> levels - 240
for y in 0..<h { - 241●
for x in 0..<w { - 242
// 保留低频区域 - 243●
if x < low_w && y < low_h { - 244●
continue - 245
} - 246
let idx = y * w + x - 247
let c = coeffs[idx] - 248
if soft { - 249
// 软阈值 - 250●
if c > threshold { - 251●
coeffs[idx] = c - threshold - 252●
} else if c < -threshold { - 253○
coeffs[idx] = c + threshold - 254
} else { - 255●
coeffs[idx] = 0.0F - 256
} - 257
// 硬阈值 - 258●
} else if c.abs() < threshold { - 259●
coeffs[idx] = 0.0F - 260
} - 261
} - 262
} - 263●
haar_inverse_transform_2d(result) - 264
}