image.process\frequency\freq_filter.mbt
77.5% · 55 / 71 points · 216 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 频域滤波 freq filter - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 频域滤波(基于 FFT) - 7
- 8
///| - 9
/// 频域滤波类型 - 10
pub(all) enum FreqFilterType { - 11
LowPass // 低通滤波(保留低频,去除高频) - 12
HighPass // 高通滤波(保留高频,去除低频) - 13
BandPass // 带通滤波(保留中频) - 14
BandStop // 带阻滤波(去除中频) - 15
} - 16
- 17
///| - 18
/// 频域滤波。对灰度图做 FFT,应用指定滤波器,再做 IFFT 回空域。 - 19
/// cutoff 为截止频率(像素数,从中心算起),band_width 为带宽(仅 BandPass/BandStop 使用)。 - 20
pub fn freq_filter( - 21
img : @types.Image, - 22
filter_type : FreqFilterType, - 23
cutoff : Float, - 24
band_width~ : Float = 0.0F, - 25
) -> @types.Image raise @types.LoadError { - 26●
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 27●
raise @types.LoadError::DecodeFailed("freq_filter 仅支持 channels=1/3/4") - 28
} - 29
let ch = img.channels - 30●
let color_ch = if ch >= 3 { 3 } else { 1 } - 31●
let ffts = fft_2d(img) - 32
let filtered : Array[FFTResult] = [] - 33
for c in 0..<color_ch { - 34●
fft_shift_inplace(ffts[c]) - 35
let shifted = ffts[c] - 36
let w = shifted.width - 37
let h = shifted.height - 38
let cx = w / 2 - 39
let cy = h / 2 - 40
let n = w * h - 41●
let out_data : Array[Complex] = Array::make(n, Complex::{ - 42
re: 0.0F, - 43
im: 0.0F, - 44
}) - 45
let data = shifted.data - 46
for y in 0..<h { - 47●
let dy = y - cy - 48
let row = y * w - 49
for x in 0..<w { - 50●
let dx = x - cx - 51●
let dist = Float::from_int(dx * dx + dy * dy).sqrt() - 52●
let gain = filter_gain(filter_type, dist, cutoff, band_width) - 53
let src = data[row + x] - 54
out_data[row + x] = Complex::{ re: src.re * gain, im: src.im * gain, } - 55
} - 56
} - 57
let filtered_fft = FFTResult::{ width: w, height: h, data: out_data, } - 58●
fft_shift_inplace(filtered_fft) - 59●
filtered.push(filtered_fft) - 60
} - 61
if color_ch == 1 { - 62●
return ifft_2d(filtered[0]) - 63
} - 64
let pw = filtered[0].width - 65
let ph = filtered[0].height - 66
let n = pw * ph - 67●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 68
for c in 0..<color_ch { - 69●
let channel_img = ifft_2d(filtered[c]) - 70
for i in 0..<n { - 71●
out[i * ch + c] = channel_img.data[i] - 72
} - 73
} - 74
if ch == 4 { - 75○
for i in 0..<n { - 76○
out[i * 4 + 3] = img.data[i * 4 + 3] - 77
} - 78
} - 79
@types.Image::{ - 80
width: pw, - 81
height: ph, - 82
channels: ch, - 83●
data: Bytes::from_array(out), - 84
} - 85
} - 86
- 87
///| - 88
/// 理想滤波器增益 - 89
fn filter_gain( - 90
filter_type : FreqFilterType, - 91
dist : Float, - 92
cutoff : Float, - 93
band_width : Float, - 94
) -> Float { - 95●
match filter_type { - 96●
LowPass => if dist <= cutoff { 1.0F } else { 0.0F } - 97●
HighPass => if dist >= cutoff { 1.0F } else { 0.0F } - 98●
BandPass => { - 99
let lo = cutoff - band_width / 2.0F - 100
let hi = cutoff + band_width / 2.0F - 101
if dist >= lo && dist <= hi { - 102●
1.0F - 103
} else { - 104●
0.0F - 105
} - 106
} - 107●
BandStop => { - 108
let lo = cutoff - band_width / 2.0F - 109
let hi = cutoff + band_width / 2.0F - 110
if dist >= lo && dist <= hi { - 111●
0.0F - 112
} else { - 113●
1.0F - 114
} - 115
} - 116
} - 117
} - 118
- 119
///| - 120
/// 高斯频域滤波。使用高斯传递函数而非理想滤波器,更平滑。 - 121
pub fn freq_filter_gaussian( - 122
img : @types.Image, - 123
filter_type : FreqFilterType, - 124
cutoff : Float, - 125
band_width~ : Float = 0.0F, - 126
) -> @types.Image raise @types.LoadError { - 127●
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 128●
raise @types.LoadError::DecodeFailed( - 129
"freq_filter_gaussian 仅支持 channels=1/3/4", - 130
) - 131
} - 132
let ch = img.channels - 133◐
let color_ch = if ch >= 3 { 3 } else { 1 } - 134●
let ffts = fft_2d(img) - 135
let filtered : Array[FFTResult] = [] - 136
for c in 0..<color_ch { - 137●
fft_shift_inplace(ffts[c]) - 138
let shifted = ffts[c] - 139
let w = shifted.width - 140
let h = shifted.height - 141
let cx = w / 2 - 142
let cy = h / 2 - 143
let n = w * h - 144●
let out_data : Array[Complex] = Array::make(n, Complex::{ - 145
re: 0.0F, - 146
im: 0.0F, - 147
}) - 148
let data = shifted.data - 149
for y in 0..<h { - 150●
let dy = y - cy - 151
let row = y * w - 152
for x in 0..<w { - 153●
let dx = x - cx - 154●
let dist = Float::from_int(dx * dx + dy * dy).sqrt() - 155●
let gain = gaussian_gain(filter_type, dist, cutoff, band_width) - 156
let src = data[row + x] - 157
out_data[row + x] = Complex::{ re: src.re * gain, im: src.im * gain, } - 158
} - 159
} - 160
let filtered_fft = FFTResult::{ width: w, height: h, data: out_data, } - 161●
fft_shift_inplace(filtered_fft) - 162●
filtered.push(filtered_fft) - 163
} - 164
if color_ch == 1 { - 165●
return ifft_2d(filtered[0]) - 166
} - 167
let pw = filtered[0].width - 168
let ph = filtered[0].height - 169
let n = pw * ph - 170○
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 171
for c in 0..<color_ch { - 172○
let channel_img = ifft_2d(filtered[c]) - 173
for i in 0..<n { - 174○
out[i * ch + c] = channel_img.data[i] - 175
} - 176
} - 177
if ch == 4 { - 178○
for i in 0..<n { - 179○
out[i * 4 + 3] = img.data[i * 4 + 3] - 180
} - 181
} - 182
@types.Image::{ - 183
width: pw, - 184
height: ph, - 185
channels: ch, - 186○
data: Bytes::from_array(out), - 187
} - 188
} - 189
- 190
///| - 191
/// 高斯传递函数增益 - 192
fn gaussian_gain( - 193
filter_type : FreqFilterType, - 194
dist : Float, - 195
cutoff : Float, - 196
band_width : Float, - 197
) -> Float { - 198●
let eps = 0.0001F - 199
let c2 = cutoff * cutoff - 200●
match filter_type { - 201●
LowPass => @math.expf(-(dist * dist) / (c2 + eps)) - 202●
HighPass => 1.0F - @math.expf(-(dist * dist) / (c2 + eps)) - 203○
BandPass => { - 204○
let lp = @math.expf(-(dist * dist) / (c2 + eps)) - 205
let lo = cutoff - band_width / 2.0F - 206○
let lp2 = @math.expf(-(dist * dist) / (lo * lo + eps)) - 207
lp2 - lp - 208
} - 209○
BandStop => { - 210○
let lp = @math.expf(-(dist * dist) / (c2 + eps)) - 211
let lo = cutoff - band_width / 2.0F - 212○
let lp2 = @math.expf(-(dist * dist) / (lo * lo + eps)) - 213
1.0F - (lp2 - lp) - 214
} - 215
} - 216
}