image.process\segment\morphology.mbt
97.9% · 140 / 143 points · 396 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 形态学 morphology - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 形态学操作(纯 MoonBit):erode / dilate / morph_open / morph_close - 7
// 使用 3x3 结构元素(八邻域) - 8
- 9
///| - 10
/// 腐蚀。每个像素取 3x3 邻域最小值。分离内部/边界区域以消除热点 clamp 调用。 - 11
pub fn erode(img : @types.Image) -> @types.Image { - 12●
let ch = img.channels - 13
let w = img.width - 14
let h = img.height - 15
let n = w * h - 16●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 17
// 预计算像素 Int 数组,避免内层循环重复类型转换 - 18●
let src : Array[Int32] = Array::make(n * ch, 0) - 19
for i in 0..<(n * ch) { - 20●
src[i] = img.data[i].to_int() - 21
} - 22
// 内部区域:x in [1, w-1), y in [1, h-1),3x3 核完全在图像内 - 23
for y in 1..<(h - 1) { - 24●
for x in 1..<(w - 1) { - 25●
for c in 0..<ch { - 26●
let base = ((y - 1) * w + (x - 1)) * ch + c - 27
let mut min_v = src[base] - 28
if src[base + ch] < min_v { - 29●
min_v = src[base + ch] - 30
} - 31
if src[base + 2 * ch] < min_v { - 32●
min_v = src[base + 2 * ch] - 33
} - 34
let row1 = base + w * ch - 35
if src[row1] < min_v { - 36●
min_v = src[row1] - 37
} - 38
if src[row1 + ch] < min_v { - 39●
min_v = src[row1 + ch] - 40
} - 41
if src[row1 + 2 * ch] < min_v { - 42●
min_v = src[row1 + 2 * ch] - 43
} - 44
let row2 = base + 2 * w * ch - 45
if src[row2] < min_v { - 46●
min_v = src[row2] - 47
} - 48
if src[row2 + ch] < min_v { - 49●
min_v = src[row2 + ch] - 50
} - 51
if src[row2 + 2 * ch] < min_v { - 52●
min_v = src[row2 + 2 * ch] - 53
} - 54●
out[(y * w + x) * ch + c] = min_v.to_byte() - 55
} - 56
} - 57
} - 58
// 边界区域:需要 clamp - 59
for y in 0..<h { - 60●
let y_inner = y >= 1 && y < h - 1 - 61
for x in 0..<w { - 62●
if y_inner && x >= 1 && x < w - 1 { - 63●
continue - 64
} - 65
for c in 0..<ch { - 66●
let mut min_v = 255 - 67
for dy in -1..<=1 { - 68●
for dx in -1..<=1 { - 69●
let sx = @types.clamp_int(x + dx, 0, w - 1) - 70●
let sy = @types.clamp_int(y + dy, 0, h - 1) - 71
let v = src[(sy * w + sx) * ch + c] - 72
if v < min_v { - 73●
min_v = v - 74
} - 75
} - 76
} - 77●
out[(y * w + x) * ch + c] = min_v.to_byte() - 78
} - 79
} - 80
} - 81
@types.Image::{ - 82
width: w, - 83
height: h, - 84
channels: ch, - 85●
data: Bytes::from_array(out), - 86
} - 87
} - 88
- 89
///| - 90
/// 膨胀。每个像素取 3x3 邻域最大值。分离内部/边界区域以消除热点 clamp 调用。 - 91
pub fn dilate(img : @types.Image) -> @types.Image { - 92●
let ch = img.channels - 93
let w = img.width - 94
let h = img.height - 95
let n = w * h - 96●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 97
// 预计算像素 Int 数组 - 98●
let src : Array[Int32] = Array::make(n * ch, 0) - 99
for i in 0..<(n * ch) { - 100●
src[i] = img.data[i].to_int() - 101
} - 102
// 内部区域:x in [1, w-1), y in [1, h-1) - 103
for y in 1..<(h - 1) { - 104●
for x in 1..<(w - 1) { - 105●
for c in 0..<ch { - 106●
let base = ((y - 1) * w + (x - 1)) * ch + c - 107
let mut max_v = src[base] - 108
if src[base + ch] > max_v { - 109●
max_v = src[base + ch] - 110
} - 111
if src[base + 2 * ch] > max_v { - 112●
max_v = src[base + 2 * ch] - 113
} - 114
let row1 = base + w * ch - 115
if src[row1] > max_v { - 116●
max_v = src[row1] - 117
} - 118
if src[row1 + ch] > max_v { - 119●
max_v = src[row1 + ch] - 120
} - 121
if src[row1 + 2 * ch] > max_v { - 122●
max_v = src[row1 + 2 * ch] - 123
} - 124
let row2 = base + 2 * w * ch - 125
if src[row2] > max_v { - 126●
max_v = src[row2] - 127
} - 128
if src[row2 + ch] > max_v { - 129●
max_v = src[row2 + ch] - 130
} - 131
if src[row2 + 2 * ch] > max_v { - 132●
max_v = src[row2 + 2 * ch] - 133
} - 134●
out[(y * w + x) * ch + c] = max_v.to_byte() - 135
} - 136
} - 137
} - 138
// 边界区域:需要 clamp - 139
for y in 0..<h { - 140●
let y_inner = y >= 1 && y < h - 1 - 141
for x in 0..<w { - 142●
if y_inner && x >= 1 && x < w - 1 { - 143●
continue - 144
} - 145
for c in 0..<ch { - 146●
let mut max_v = 0 - 147
for dy in -1..<=1 { - 148●
for dx in -1..<=1 { - 149●
let sx = @types.clamp_int(x + dx, 0, w - 1) - 150●
let sy = @types.clamp_int(y + dy, 0, h - 1) - 151
let v = src[(sy * w + sx) * ch + c] - 152
if v > max_v { - 153●
max_v = v - 154
} - 155
} - 156
} - 157●
out[(y * w + x) * ch + c] = max_v.to_byte() - 158
} - 159
} - 160
} - 161
@types.Image::{ - 162
width: w, - 163
height: h, - 164
channels: ch, - 165●
data: Bytes::from_array(out), - 166
} - 167
} - 168
- 169
///| - 170
/// 开运算(先腐蚀后膨胀)。去除小的明亮噪点。 - 171
pub fn morph_open(img : @types.Image) -> @types.Image { - 172●
dilate(erode(img)) - 173
} - 174
- 175
///| - 176
/// 闭运算(先膨胀后腐蚀)。填充小的暗洞。 - 177
pub fn morph_close(img : @types.Image) -> @types.Image { - 178●
erode(dilate(img)) - 179
} - 180
- 181
///| - 182
/// 形态学梯度(dilate - erode)。提取边缘轮廓。 - 183
pub fn morph_gradient(img : @types.Image) -> @types.Image { - 184●
let dilated = dilate(img) - 185●
let eroded = erode(img) - 186
let ch = img.channels - 187
let n = img.width * img.height * ch - 188●
let out : Array[Byte] = Array::make(n, b'\x00') - 189
for i in 0..<n { - 190●
let v = dilated.data[i].to_int() - eroded.data[i].to_int() - 191◐
out[i] = if v < 0 { b'\x00' } else { v.to_byte() } - 192
} - 193
@types.Image::{ - 194
width: img.width, - 195
height: img.height, - 196
channels: ch, - 197●
data: Bytes::from_array(out), - 198
} - 199
} - 200
- 201
///| - 202
/// 顶帽变换(original - open)。提取比结构元素小的明亮细节。 - 203
pub fn morph_tophat(img : @types.Image) -> @types.Image { - 204●
let opened = morph_open(img) - 205
let ch = img.channels - 206
let n = img.width * img.height * ch - 207●
let out : Array[Byte] = Array::make(n, b'\x00') - 208
for i in 0..<n { - 209●
let v = img.data[i].to_int() - opened.data[i].to_int() - 210◐
out[i] = if v < 0 { b'\x00' } else { v.to_byte() } - 211
} - 212
@types.Image::{ - 213
width: img.width, - 214
height: img.height, - 215
channels: ch, - 216●
data: Bytes::from_array(out), - 217
} - 218
} - 219
- 220
///| - 221
/// 黑帽变换(close - original)。提取比结构元素小的暗细节。 - 222
pub fn morph_blackhat(img : @types.Image) -> @types.Image { - 223●
let closed = morph_close(img) - 224
let ch = img.channels - 225
let n = img.width * img.height * ch - 226●
let out : Array[Byte] = Array::make(n, b'\x00') - 227
for i in 0..<n { - 228●
let v = closed.data[i].to_int() - img.data[i].to_int() - 229◐
out[i] = if v < 0 { b'\x00' } else { v.to_byte() } - 230
} - 231
@types.Image::{ - 232
width: img.width, - 233
height: img.height, - 234
channels: ch, - 235●
data: Bytes::from_array(out), - 236
} - 237
} - 238
- 239
///| - 240
/// 结构元素(核)。二维布尔数组,true 表示该位置属于结构元素。 - 241
pub(all) struct StructuringElement { - 242
width : Int32 - 243
height : Int32 - 244
data : Array[Bool] // height * width - 245
} - 246
- 247
///| - 248
/// 创建矩形结构元素(全为 true)。 - 249
pub fn se_rect(width : Int32, height : Int32) -> StructuringElement { - 250●
StructuringElement::{ - 251
width, - 252
height, - 253●
data: Array::make(width * height, true), - 254
} - 255
} - 256
- 257
///| - 258
/// 创建十字形结构元素(中心行+中心列)。 - 259
pub fn se_cross(size : Int32) -> StructuringElement { - 260●
let data : Array[Bool] = Array::make(size * size, false) - 261
let cx = size / 2 - 262
for i in 0..<size { - 263●
data[cx * size + i] = true - 264
data[i * size + cx] = true - 265
} - 266
StructuringElement::{ width: size, height: size, data, } - 267
} - 268
- 269
///| - 270
/// 创建椭圆形结构元素。对于 size×size 的核,包含所有满足 - 271
/// (dx/rx)² + (dy/ry)² ≤ 1 的位置。 - 272
pub fn se_ellipse(size : Int32) -> StructuringElement { - 273●
let data : Array[Bool] = Array::make(size * size, false) - 274
let cx = (size - 1) / 2 - 275
let cy = (size - 1) / 2 - 276●
let rx = Float::from_int(cx) + 0.5F - 277●
let ry = Float::from_int(cy) + 0.5F - 278
for y in 0..<size { - 279●
for x in 0..<size { - 280●
let dx = (Float::from_int(x) - Float::from_int(cx)) / rx - 281●
let dy = (Float::from_int(y) - Float::from_int(cy)) / ry - 282
if dx * dx + dy * dy <= 1.0F { - 283●
data[y * size + x] = true - 284
} - 285
} - 286
} - 287
StructuringElement::{ width: size, height: size, data, } - 288
} - 289
- 290
///| - 291
/// 使用自定义结构元素进行腐蚀。 - 292
pub fn erode_custom( - 293
img : @types.Image, - 294
se : StructuringElement, - 295
) -> @types.Image { - 296●
let ch = img.channels - 297
let w = img.width - 298
let h = img.height - 299
let n = w * h - 300●
let src : Array[Int32] = Array::make(n * ch, 0) - 301
for i in 0..<(n * ch) { - 302●
src[i] = img.data[i].to_int() - 303
} - 304●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 305
let cx = se.width / 2 - 306
let cy = se.height / 2 - 307
for y in 0..<h { - 308●
for x in 0..<w { - 309●
for c in 0..<ch { - 310●
let mut min_v = 255 - 311
for sy in 0..<se.height { - 312●
for sx in 0..<se.width { - 313●
if se.data[sy * se.width + sx] { - 314●
let nx = @types.clamp_int(x + sx - cx, 0, w - 1) - 315●
let ny = @types.clamp_int(y + sy - cy, 0, h - 1) - 316
let v = src[(ny * w + nx) * ch + c] - 317
if v < min_v { - 318●
min_v = v - 319
} - 320
} - 321
} - 322
} - 323●
out[(y * w + x) * ch + c] = min_v.to_byte() - 324
} - 325
} - 326
} - 327
@types.Image::{ - 328
width: w, - 329
height: h, - 330
channels: ch, - 331●
data: Bytes::from_array(out), - 332
} - 333
} - 334
- 335
///| - 336
/// 使用自定义结构元素进行膨胀。 - 337
pub fn dilate_custom( - 338
img : @types.Image, - 339
se : StructuringElement, - 340
) -> @types.Image { - 341●
let ch = img.channels - 342
let w = img.width - 343
let h = img.height - 344
let n = w * h - 345●
let src : Array[Int32] = Array::make(n * ch, 0) - 346
for i in 0..<(n * ch) { - 347●
src[i] = img.data[i].to_int() - 348
} - 349●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 350
let cx = se.width / 2 - 351
let cy = se.height / 2 - 352
for y in 0..<h { - 353●
for x in 0..<w { - 354●
for c in 0..<ch { - 355●
let mut max_v = 0 - 356
for sy in 0..<se.height { - 357●
for sx in 0..<se.width { - 358●
if se.data[sy * se.width + sx] { - 359●
let nx = @types.clamp_int(x + sx - cx, 0, w - 1) - 360●
let ny = @types.clamp_int(y + sy - cy, 0, h - 1) - 361
let v = src[(ny * w + nx) * ch + c] - 362
if v > max_v { - 363●
max_v = v - 364
} - 365
} - 366
} - 367
} - 368●
out[(y * w + x) * ch + c] = max_v.to_byte() - 369
} - 370
} - 371
} - 372
@types.Image::{ - 373
width: w, - 374
height: h, - 375
channels: ch, - 376●
data: Bytes::from_array(out), - 377
} - 378
} - 379
- 380
///| - 381
/// 使用自定义结构元素进行开运算。 - 382
pub fn morph_open_custom( - 383
img : @types.Image, - 384
se : StructuringElement, - 385
) -> @types.Image { - 386●
dilate_custom(erode_custom(img, se), se) - 387
} - 388
- 389
///| - 390
/// 使用自定义结构元素进行闭运算。 - 391
pub fn morph_close_custom( - 392
img : @types.Image, - 393
se : StructuringElement, - 394
) -> @types.Image { - 395●
erode_custom(dilate_custom(img, se), se) - 396
}