image.process\feature\gabor.mbt
82.1% · 92 / 112 points · 318 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # Gabor 滤波 gabor - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// Gabor 滤波(纯 MoonBit) - 7
- 8
///| - 9
/// Gabor 滤波器。 - 10
/// Gabor 核函数 g(x,y) = exp(-(x'²+γ²y'²)/(2σ²)) * cos(2πx'/λ + φ) - 11
/// 其中 x' = x*cos(θ) + y*sin(θ), y' = -x*sin(θ) + y*cos(θ) - 12
/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4)。 - 13
/// ksize 为核大小(奇数),sigma 为标准差,theta 为方向(弧度), - 14
/// lambda 为波长,gamma 为空间纵横比(默认 0.5),phi 为相位偏移(默认 0)。 - 15
pub fn gabor_filter( - 16
img : @types.Image, - 17
ksize : Int32, - 18
sigma : Float, - 19
theta : Float, - 20
lambda~ : Float = 10.0F, - 21
gamma~ : Float = 0.5F, - 22
phi~ : Float = 0.0F, - 23
) -> @types.Image raise @types.LoadError { - 24
// 输入验证 - 25●
if img.width <= 0 || img.height <= 0 { - 26●
raise @types.LoadError::DecodeFailed( - 27
"gabor_filter: 图像尺寸必须大于0", - 28
) - 29
} - 30
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 31○
raise @types.LoadError::DecodeFailed( - 32
"gabor_filter: 仅支持 channels=1/3/4", - 33
) - 34
} - 35
let expected_size = img.width * img.height * img.channels - 36●
if img.data.length() != expected_size { - 37○
raise @types.LoadError::DecodeFailed( - 38
"gabor_filter: 数据大小不匹配,期望 " + - 39○
expected_size.to_string() + - 40
",实际 " + - 41○
img.data.length().to_string(), - 42
) - 43
} - 44
if gamma <= 0.0F { - 45○
raise @types.LoadError::DecodeFailed("gabor_filter: gamma必须大于0") - 46
} - 47
if ksize < 3 || ksize > 31 || ksize % 2 == 0 { - 48●
raise @types.LoadError::DecodeFailed( - 49
"ksize 必须为 3..31 之间的奇数", - 50
) - 51
} - 52
if sigma <= 0.0F { - 53●
raise @types.LoadError::DecodeFailed("sigma 必须 > 0") - 54
} - 55
if lambda <= 0.0F { - 56●
raise @types.LoadError::DecodeFailed("lambda 必须 > 0") - 57
} - 58
let ch = img.channels - 59
let w = img.width - 60
let h = img.height - 61
let n = w * h - 62
if n == 0 { - 63○
return img - 64
} - 65
// 生成 Gabor 核 - 66
let half = ksize / 2 - 67●
let kernel : Array[Float] = Array::make(ksize * ksize, 0.0F) - 68●
let cos_t = @math.cosf(theta) - 69●
let sin_t = @math.sinf(theta) - 70
for ky in 0..<ksize { - 71●
for kx in 0..<ksize { - 72●
let x = Float::from_int(kx - half) - 73●
let y = Float::from_int(ky - half) - 74
let x_prime = x * cos_t + y * sin_t - 75
let y_prime = -x * sin_t + y * cos_t - 76●
let gaussian = @math.expf( - 77
-(x_prime * x_prime + gamma * gamma * y_prime * y_prime) / - 78
(2.0F * sigma * sigma), - 79
) - 80●
let wave = @math.cosf(2.0F * 3.14159265F * x_prime / lambda + phi) - 81
let val = gaussian * wave - 82
kernel[ky * ksize + kx] = val - 83
} - 84
} - 85
// 预计算像素 Float 数组,避免内层循环重复类型转换 - 86●
let src : Array[Float] = Array::make(n * ch, 0.0F) - 87
for i in 0..<(n * ch) { - 88●
src[i] = Float::from_int(img.data[i].to_int()) - 89
} - 90
// 卷积,分离内部/边界区域,ch=1 特化消除内层通道循环 - 91●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 92
if ch == 1 { - 93
// 内部区域 - 94●
for y in half..<(h - half) { - 95●
for x in half..<(w - half) { - 96●
let mut sum = 0.0F - 97
let base = (y - half) * w + (x - half) - 98
for ky in 0..<ksize { - 99●
let row = base + ky * w - 100
let krow = ky * ksize - 101
for kx in 0..<ksize { - 102●
sum += src[row + kx] * kernel[krow + kx] - 103
} - 104
} - 105●
let val = sum.to_int() - 106●
out[y * w + x] = val.clamp(min=0, max=255).to_byte() - 107
} - 108
} - 109
// 边界区域 - 110
for y in 0..<h { - 111●
let y_inner = y >= half && y < h - half - 112
for x in 0..<w { - 113●
if y_inner && x >= half && x < w - half { - 114●
continue - 115
} - 116
let mut sum = 0.0F - 117
for ky in 0..<ksize { - 118●
for kx in 0..<ksize { - 119●
let px = (x + kx - half).clamp(min=0, max=w - 1) - 120●
let py = (y + ky - half).clamp(min=0, max=h - 1) - 121
sum += src[py * w + px] * kernel[ky * ksize + kx] - 122
} - 123
} - 124●
let val = sum.to_int() - 125●
out[y * w + x] = val.clamp(min=0, max=255).to_byte() - 126
} - 127
} - 128
} else { - 129●
for c in 0..<ch { - 130●
if ch == 4 && c == 3 { - 131●
for i in 0..<n { - 132●
out[i * ch + 3] = img.data[i * ch + 3] - 133
} - 134
continue - 135
} - 136
// 内部区域 - 137
for y in half..<(h - half) { - 138●
for x in half..<(w - half) { - 139●
let mut sum = 0.0F - 140
let base = (y - half) * w + (x - half) - 141
for ky in 0..<ksize { - 142●
let row = (base + ky * w) * ch + c - 143
let krow = ky * ksize - 144
for kx in 0..<ksize { - 145●
sum += src[row + kx * ch] * kernel[krow + kx] - 146
} - 147
} - 148●
let val = sum.to_int() - 149●
out[(y * w + x) * ch + c] = val.clamp(min=0, max=255).to_byte() - 150
} - 151
} - 152
// 边界区域 - 153
for y in 0..<h { - 154●
let y_inner = y >= half && y < h - half - 155
for x in 0..<w { - 156●
if y_inner && x >= half && x < w - half { - 157●
continue - 158
} - 159
let mut sum = 0.0F - 160
for ky in 0..<ksize { - 161●
for kx in 0..<ksize { - 162●
let px = (x + kx - half).clamp(min=0, max=w - 1) - 163●
let py = (y + ky - half).clamp(min=0, max=h - 1) - 164
sum += src[(py * w + px) * ch + c] * kernel[ky * ksize + kx] - 165
} - 166
} - 167●
let val = sum.to_int() - 168●
out[(y * w + x) * ch + c] = val.clamp(min=0, max=255).to_byte() - 169
} - 170
} - 171
} - 172
} - 173
@types.Image::{ - 174
width: w, - 175
height: h, - 176
channels: ch, - 177●
data: Bytes::from_array(out), - 178
} - 179
} - 180
- 181
///| - 182
/// Gabor 滤波器组(多方向)。 - 183
/// 对图像应用多个方向的 Gabor 滤波器,返回每个方向的响应幅值之和。 - 184
/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4)。 - 185
/// num_orientations 为方向数(默认 4,即 0°/45°/90°/135°)。 - 186
pub fn gabor_filter_bank( - 187
img : @types.Image, - 188
ksize : Int32, - 189
sigma : Float, - 190
num_orientations~ : Int32 = 4, - 191
lambda~ : Float = 10.0F, - 192
gamma~ : Float = 0.5F, - 193
) -> @types.Image raise @types.LoadError { - 194
// 输入验证 - 195●
if img.width <= 0 || img.height <= 0 { - 196○
raise @types.LoadError::DecodeFailed( - 197
"gabor_filter_bank: 图像尺寸必须大于0", - 198
) - 199
} - 200
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 201○
raise @types.LoadError::DecodeFailed( - 202
"gabor_filter_bank: 仅支持 channels=1/3/4", - 203
) - 204
} - 205
let expected_size = img.width * img.height * img.channels - 206●
if img.data.length() != expected_size { - 207○
raise @types.LoadError::DecodeFailed( - 208
"gabor_filter_bank: 数据大小不匹配,期望 " + - 209○
expected_size.to_string() + - 210
",实际 " + - 211○
img.data.length().to_string(), - 212
) - 213
} - 214
if ksize < 3 || ksize > 31 || ksize % 2 == 0 { - 215○
raise @types.LoadError::DecodeFailed( - 216
"gabor_filter_bank: ksize必须为3..31之间的奇数", - 217
) - 218
} - 219
if sigma <= 0.0F { - 220○
raise @types.LoadError::DecodeFailed( - 221
"gabor_filter_bank: sigma必须大于0", - 222
) - 223
} - 224
if lambda <= 0.0F { - 225○
raise @types.LoadError::DecodeFailed( - 226
"gabor_filter_bank: lambda必须大于0", - 227
) - 228
} - 229
if gamma <= 0.0F { - 230○
raise @types.LoadError::DecodeFailed( - 231
"gabor_filter_bank: gamma必须大于0", - 232
) - 233
} - 234
if num_orientations < 1 || num_orientations > 16 { - 235●
raise @types.LoadError::DecodeFailed( - 236
"num_orientations 必须在 1..16 之间", - 237
) - 238
} - 239
let ch = img.channels - 240
let w = img.width - 241
let h = img.height - 242
let n = w * h - 243
if n == 0 { - 244○
return img - 245
} - 246
// 累加各方向响应 - 247●
let accum : Array[Float] = Array::make(n * ch, 0.0F) - 248
for i in 0..<num_orientations { - 249●
let theta = Float::from_int(i) * - 250
3.14159265F / - 251●
Float::from_int(num_orientations) - 252●
let filtered = gabor_filter(img, ksize, sigma, theta, lambda~, gamma~) - 253
for j in 0..<(n * ch) { - 254●
accum[j] += Float::from_int(filtered.data[j].to_int()) - 255
} - 256
} - 257
// 归一化 - 258●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 259
for j in 0..<(n * ch) { - 260●
let val = (accum[j] / Float::from_int(num_orientations)).to_int() - 261●
out[j] = val.clamp(min=0, max=255).to_byte() - 262
} - 263
@types.Image::{ - 264
width: w, - 265
height: h, - 266
channels: ch, - 267●
data: Bytes::from_array(out), - 268
} - 269
} - 270
- 271
///| - 272
/// 生成 Gabor 核(返回核矩阵,用于可视化或调试)。 - 273
pub fn gabor_kernel( - 274
ksize : Int32, - 275
sigma : Float, - 276
theta : Float, - 277
lambda~ : Float = 10.0F, - 278
gamma~ : Float = 0.5F, - 279
phi~ : Float = 0.0F, - 280
) -> Array[Array[Float]] raise @types.LoadError { - 281
// 参数验证 - 282●
if ksize < 3 || ksize > 31 || ksize % 2 == 0 { - 283●
raise @types.LoadError::DecodeFailed( - 284
"gabor_kernel: ksize必须为3..31之间的奇数", - 285
) - 286
} - 287
if sigma <= 0.0F { - 288●
raise @types.LoadError::DecodeFailed("gabor_kernel: sigma必须大于0") - 289
} - 290
if lambda <= 0.0F { - 291○
raise @types.LoadError::DecodeFailed("gabor_kernel: lambda必须大于0") - 292
} - 293
if gamma <= 0.0F { - 294○
raise @types.LoadError::DecodeFailed("gabor_kernel: gamma必须大于0") - 295
} - 296
let half = ksize / 2 - 297●
let kernel : Array[Array[Float]] = Array::make(ksize, []) - 298
for i in 0..<ksize { - 299●
kernel[i] = Array::make(ksize, 0.0F) - 300
} - 301●
let cos_t = @math.cosf(theta) - 302●
let sin_t = @math.sinf(theta) - 303
for ky in 0..<ksize { - 304●
for kx in 0..<ksize { - 305●
let x = Float::from_int(kx - half) - 306●
let y = Float::from_int(ky - half) - 307
let x_prime = x * cos_t + y * sin_t - 308
let y_prime = -x * sin_t + y * cos_t - 309●
let gaussian = @math.expf( - 310
-(x_prime * x_prime + gamma * gamma * y_prime * y_prime) / - 311
(2.0F * sigma * sigma), - 312
) - 313●
let wave = @math.cosf(2.0F * 3.14159265F * x_prime / lambda + phi) - 314
kernel[ky][kx] = gaussian * wave - 315
} - 316
} - 317
kernel - 318
}