image.process\feature\lbp.mbt
90.8% · 99 / 109 points · 285 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # LBP 局部二值模式 lbp - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 局部二值模式(LBP,纯 MoonBit) - 7
- 8
///| - 9
/// 基本局部二值模式(LBP)。对每个像素,与其 3x3 邻域的 8 个像素比较, - 10
/// 大于等于中心记 1,否则记 0,按顺时针顺序组成 8 位二进制数。 - 11
/// 仅处理灰度图(channels=1)或 RGB 图(channels=3/4),alpha 通道不变。 - 12
pub fn lbp(img : @types.Image) -> @types.Image raise @types.LoadError { - 13
// 输入验证 - 14●
if img.width <= 0 || img.height <= 0 { - 15●
raise @types.LoadError::DecodeFailed("lbp: 图像尺寸必须大于0") - 16
} - 17
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 18●
raise @types.LoadError::DecodeFailed("lbp: 仅支持 channels=1/3/4") - 19
} - 20
let expected_size = img.width * img.height * img.channels - 21●
if img.data.length() != expected_size { - 22●
raise @types.LoadError::DecodeFailed( - 23
"lbp: 数据大小不匹配,期望 " + - 24●
expected_size.to_string() + - 25
",实际 " + - 26●
img.data.length().to_string(), - 27
) - 28
} - 29
let ch = img.channels - 30●
let color_ch = if ch >= 3 { 3 } else { 1 } - 31
let w = img.width - 32
let h = img.height - 33
let n = w * h - 34●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 35
// 预计算像素 Int 数组 - 36●
let src : Array[Int32] = Array::make(n * ch, 0) - 37
for i in 0..<(n * ch) { - 38●
src[i] = img.data[i].to_int() - 39
} - 40
for c in 0..<color_ch { - 41
// 内部区域:x in [1, w-1), y in [1, h-1),直接访问 8 邻域 - 42●
for y in 1..<(h - 1) { - 43●
for x in 1..<(w - 1) { - 44●
let center = src[(y * w + x) * ch + c] - 45
let mut lbp_val = 0 - 46
// 左上、上、右上、右、右下、下、左下、左 - 47
if src[((y - 1) * w + (x - 1)) * ch + c] >= center { - 48●
lbp_val = lbp_val | 1 - 49
} - 50
if src[((y - 1) * w + x) * ch + c] >= center { - 51●
lbp_val = lbp_val | 2 - 52
} - 53
if src[((y - 1) * w + (x + 1)) * ch + c] >= center { - 54●
lbp_val = lbp_val | 4 - 55
} - 56
if src[(y * w + (x + 1)) * ch + c] >= center { - 57●
lbp_val = lbp_val | 8 - 58
} - 59
if src[((y + 1) * w + (x + 1)) * ch + c] >= center { - 60●
lbp_val = lbp_val | 16 - 61
} - 62
if src[((y + 1) * w + x) * ch + c] >= center { - 63●
lbp_val = lbp_val | 32 - 64
} - 65
if src[((y + 1) * w + (x - 1)) * ch + c] >= center { - 66●
lbp_val = lbp_val | 64 - 67
} - 68
if src[(y * w + (x - 1)) * ch + c] >= center { - 69●
lbp_val = lbp_val | 128 - 70
} - 71●
out[(y * w + x) * ch + c] = lbp_val.to_byte() - 72
} - 73
} - 74
// 边界区域:使用 get_pixel_safe - 75
for y in 0..<h { - 76●
let y_inner = y >= 1 && y < h - 1 - 77
for x in 0..<w { - 78●
if y_inner && x >= 1 && x < w - 1 { - 79●
continue - 80
} - 81
let center = src[(y * w + x) * ch + c] - 82
let mut lbp_val = 0 - 83●
if get_pixel_safe(src, x - 1, y - 1, c, ch, w, h) >= center { - 84●
lbp_val = lbp_val | 1 - 85
} - 86●
if get_pixel_safe(src, x, y - 1, c, ch, w, h) >= center { - 87●
lbp_val = lbp_val | 2 - 88
} - 89●
if get_pixel_safe(src, x + 1, y - 1, c, ch, w, h) >= center { - 90●
lbp_val = lbp_val | 4 - 91
} - 92●
if get_pixel_safe(src, x + 1, y, c, ch, w, h) >= center { - 93●
lbp_val = lbp_val | 8 - 94
} - 95●
if get_pixel_safe(src, x + 1, y + 1, c, ch, w, h) >= center { - 96●
lbp_val = lbp_val | 16 - 97
} - 98●
if get_pixel_safe(src, x, y + 1, c, ch, w, h) >= center { - 99●
lbp_val = lbp_val | 32 - 100
} - 101●
if get_pixel_safe(src, x - 1, y + 1, c, ch, w, h) >= center { - 102●
lbp_val = lbp_val | 64 - 103
} - 104●
if get_pixel_safe(src, x - 1, y, c, ch, w, h) >= center { - 105●
lbp_val = lbp_val | 128 - 106
} - 107●
out[(y * w + x) * ch + c] = lbp_val.to_byte() - 108
} - 109
} - 110
} - 111
if ch == 4 { - 112○
for i in 0..<n { - 113○
out[i * 4 + 3] = img.data[i * 4 + 3] - 114
} - 115
} - 116
@types.Image::{ - 117
width: w, - 118
height: h, - 119
channels: ch, - 120●
data: Bytes::from_array(out), - 121
} - 122
} - 123
- 124
///| - 125
/// 均匀局部二值模式(Uniform LBP)。 - 126
/// 均匀模式指 0→1 和 1→0 跳变次数 <= 2 的模式,共有 58 种均匀模式。 - 127
/// 非均匀模式统一映射为 59。输出范围 0~59。 - 128
pub fn lbp_uniform(img : @types.Image) -> @types.Image raise @types.LoadError { - 129
// 输入验证 - 130●
if img.width <= 0 || img.height <= 0 { - 131●
raise @types.LoadError::DecodeFailed( - 132
"lbp_uniform: 图像尺寸必须大于0", - 133
) - 134
} - 135
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 136○
raise @types.LoadError::DecodeFailed( - 137
"lbp_uniform: 仅支持 channels=1/3/4", - 138
) - 139
} - 140
let expected_size = img.width * img.height * img.channels - 141●
if img.data.length() != expected_size { - 142○
raise @types.LoadError::DecodeFailed( - 143
"lbp_uniform: 数据大小不匹配,期望 " + - 144○
expected_size.to_string() + - 145
",实际 " + - 146○
img.data.length().to_string(), - 147
) - 148
} - 149
let ch = img.channels - 150◐
let color_ch = if ch >= 3 { 3 } else { 1 } - 151
let w = img.width - 152
let h = img.height - 153
let n = w * h - 154●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 155
// 预计算均匀模式映射表 - 156●
let uniform_map = build_uniform_map() - 157
// 预计算像素 Int 数组 - 158●
let src : Array[Int32] = Array::make(n * ch, 0) - 159
for i in 0..<(n * ch) { - 160●
src[i] = img.data[i].to_int() - 161
} - 162
for c in 0..<color_ch { - 163
// 内部区域 - 164●
for y in 1..<(h - 1) { - 165●
for x in 1..<(w - 1) { - 166●
let center = src[(y * w + x) * ch + c] - 167
let mut lbp_val = 0 - 168
if src[((y - 1) * w + (x - 1)) * ch + c] >= center { - 169●
lbp_val = lbp_val | 1 - 170
} - 171
if src[((y - 1) * w + x) * ch + c] >= center { - 172●
lbp_val = lbp_val | 2 - 173
} - 174
if src[((y - 1) * w + (x + 1)) * ch + c] >= center { - 175●
lbp_val = lbp_val | 4 - 176
} - 177
if src[(y * w + (x + 1)) * ch + c] >= center { - 178●
lbp_val = lbp_val | 8 - 179
} - 180
if src[((y + 1) * w + (x + 1)) * ch + c] >= center { - 181●
lbp_val = lbp_val | 16 - 182
} - 183
if src[((y + 1) * w + x) * ch + c] >= center { - 184●
lbp_val = lbp_val | 32 - 185
} - 186
if src[((y + 1) * w + (x - 1)) * ch + c] >= center { - 187●
lbp_val = lbp_val | 64 - 188
} - 189
if src[(y * w + (x - 1)) * ch + c] >= center { - 190●
lbp_val = lbp_val | 128 - 191
} - 192●
out[(y * w + x) * ch + c] = uniform_map[lbp_val].to_byte() - 193
} - 194
} - 195
// 边界区域 - 196
for y in 0..<h { - 197●
let y_inner = y >= 1 && y < h - 1 - 198
for x in 0..<w { - 199●
if y_inner && x >= 1 && x < w - 1 { - 200●
continue - 201
} - 202
let center = src[(y * w + x) * ch + c] - 203
let mut lbp_val = 0 - 204●
if get_pixel_safe(src, x - 1, y - 1, c, ch, w, h) >= center { - 205●
lbp_val = lbp_val | 1 - 206
} - 207●
if get_pixel_safe(src, x, y - 1, c, ch, w, h) >= center { - 208●
lbp_val = lbp_val | 2 - 209
} - 210●
if get_pixel_safe(src, x + 1, y - 1, c, ch, w, h) >= center { - 211●
lbp_val = lbp_val | 4 - 212
} - 213●
if get_pixel_safe(src, x + 1, y, c, ch, w, h) >= center { - 214●
lbp_val = lbp_val | 8 - 215
} - 216●
if get_pixel_safe(src, x + 1, y + 1, c, ch, w, h) >= center { - 217●
lbp_val = lbp_val | 16 - 218
} - 219●
if get_pixel_safe(src, x, y + 1, c, ch, w, h) >= center { - 220●
lbp_val = lbp_val | 32 - 221
} - 222●
if get_pixel_safe(src, x - 1, y + 1, c, ch, w, h) >= center { - 223●
lbp_val = lbp_val | 64 - 224
} - 225●
if get_pixel_safe(src, x - 1, y, c, ch, w, h) >= center { - 226●
lbp_val = lbp_val | 128 - 227
} - 228●
out[(y * w + x) * ch + c] = uniform_map[lbp_val].to_byte() - 229
} - 230
} - 231
} - 232
if ch == 4 { - 233○
for i in 0..<n { - 234○
out[i * 4 + 3] = img.data[i * 4 + 3] - 235
} - 236
} - 237
@types.Image::{ - 238
width: w, - 239
height: h, - 240
channels: ch, - 241●
data: Bytes::from_array(out), - 242
} - 243
} - 244
- 245
///| - 246
fn get_pixel_safe( - 247
src : Array[Int32], - 248
x : Int32, - 249
y : Int32, - 250
c : Int32, - 251
ch : Int32, - 252
w : Int32, - 253
h : Int32, - 254
) -> Int32 { - 255●
if x < 0 || x >= w || y < 0 || y >= h { - 256●
return 0 - 257
} - 258
src[(y * w + x) * ch + c] - 259
} - 260
- 261
///| - 262
fn build_uniform_map() -> Array[Int32] { - 263●
let map : Array[Int32] = Array::make(256, 59) - 264
let mut label = 0 - 265
for v in 0..<256 { - 266●
if count_transitions(v) <= 2 { - 267●
map[v] = label - 268
label += 1 - 269
} - 270
} - 271
map - 272
} - 273
- 274
///| - 275
fn count_transitions(v : Int32) -> Int32 { - 276●
let mut count = 0 - 277
for i in 0..<8 { - 278●
let bit_i = (v >> i) & 1 - 279
let bit_next = (v >> ((i + 1) % 8)) & 1 - 280
if bit_i != bit_next { - 281●
count += 1 - 282
} - 283
} - 284
count - 285
}