image.process\segment\distance_transform.mbt
81.3% · 65 / 80 points · 274 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 距离变换 distance transform - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 距离变换(纯 MoonBit) - 7
- 8
///| - 9
/// 距离变换。对二值图(非零为前景)计算每个前景像素到最近背景像素的距离。 - 10
/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4,取首通道)。 - 11
/// distance_type 为距离度量:1=城市距离(L1), 2=欧氏距离(L2近似), 3=棋盘距离(Linf)。 - 12
/// 返回 Float 数组,背景像素距离为 0。 - 13
pub fn distance_transform( - 14
img : @types.Image, - 15
distance_type~ : Int32 = 2, - 16
) -> Array[Float] raise @types.LoadError { - 17●
if distance_type != 1 && distance_type != 2 && distance_type != 3 { - 18●
raise @types.LoadError::DecodeFailed( - 19
"distance_type 必须为 1(L1), 2(L2), 3(Linf)", - 20
) - 21
} - 22
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 23●
raise @types.LoadError::DecodeFailed( - 24
"distance_transform 仅支持 channels=1/3/4", - 25
) - 26
} - 27
let w = img.width - 28
let h = img.height - 29
let n = w * h - 30
if n == 0 { - 31○
return [] - 32
} - 33
// 转为二值(非零为前景) - 34●
let binary : Array[Bool] = Array::make(n, false) - 35
let ch = img.channels - 36
for i in 0..<n { - 37●
binary[i] = img.data[i * ch].to_int() > 0 - 38
} - 39
// 两遍扫描算法 - 40●
let dist : Array[Float] = Array::make(n, 10000000000.0F) - 41
// 初始化:背景为 0,前景为无穷大 - 42
for i in 0..<n { - 43●
if !binary[i] { - 44●
dist[i] = 0.0F - 45
} - 46
} - 47
// 距离掩码 - 48●
let (mask1, mask2) = match distance_type { - 49●
1 => ([(0, -1, 1.0F), (-1, 0, 1.0F)], [(0, 1, 1.0F), (1, 0, 1.0F)]) - 50●
3 => ([(0, -1, 1.0F), (-1, 0, 1.0F)], [(0, 1, 1.0F), (1, 0, 1.0F)]) - 51
_ => - 52●
( - 53
[(0, -1, 1.0F), (-1, 0, 1.0F), (-1, -1, 1.414F), (-1, 1, 1.414F)], - 54
[(0, 1, 1.0F), (1, 0, 1.0F), (1, 1, 1.414F), (1, -1, 1.414F)], - 55
) - 56
} - 57
// 前向扫描(左上到右下) - 58
// 内部区域:x >= 1 && y >= 1 && y < h - 1 - 59
for y in 1..<(h - 1) { - 60●
for x in 1..<w { - 61●
let idx = y * w + x - 62
if !binary[idx] { - 63●
continue - 64
} - 65
for m in mask1 { - 66●
let (dx, dy, d) = m - 67
let nd = dist[(y + dy) * w + (x + dx)] + d - 68
if nd < dist[idx] { - 69●
dist[idx] = nd - 70
} - 71
} - 72
} - 73
} - 74
// 边界行 - 75
for y in 0..<h { - 76●
if y >= 1 && y < h - 1 { - 77●
continue - 78
} - 79
for x in 0..<w { - 80●
let idx = y * w + x - 81
if !binary[idx] { - 82●
continue - 83
} - 84
for m in mask1 { - 85●
let (dx, dy, d) = m - 86
let nx = x + dx - 87
let ny = y + dy - 88
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 89●
let nd = dist[ny * w + nx] + d - 90
if nd < dist[idx] { - 91●
dist[idx] = nd - 92
} - 93
} - 94
} - 95
} - 96
} - 97
// 内部行的边界列 - 98
for y in 1..<(h - 1) { - 99●
let idx = y * w - 100
if binary[idx] { - 101○
for m in mask1 { - 102○
let (dx, dy, d) = m - 103
let nx = dx - 104
let ny = y + dy - 105
if nx >= 0 && ny >= 0 && ny < h { - 106○
let nd = dist[ny * w + nx] + d - 107
if nd < dist[idx] { - 108○
dist[idx] = nd - 109
} - 110
} - 111
} - 112
} - 113
} - 114
// 反向扫描(右下到左上) - 115
// 内部区域:x < w - 1 && y >= 1 && y < h - 1 - 116
for y in 1..<(h - 1) { - 117●
for x in 0..<(w - 1) { - 118●
let idx = y * w + x - 119
if !binary[idx] { - 120●
continue - 121
} - 122
for m in mask2 { - 123●
let (dx, dy, d) = m - 124
let nd = dist[(y + dy) * w + (x + dx)] + d - 125
if nd < dist[idx] { - 126●
dist[idx] = nd - 127
} - 128
} - 129
} - 130
} - 131
// 边界行 - 132
for yy in 0..<h { - 133●
if yy >= 1 && yy < h - 1 { - 134●
continue - 135
} - 136
let y = yy - 137
for xx in 0..<w { - 138●
let x = w - 1 - xx - 139
let idx = y * w + x - 140
if !binary[idx] { - 141●
continue - 142
} - 143
for m in mask2 { - 144●
let (dx, dy, d) = m - 145
let nx = x + dx - 146
let ny = y + dy - 147
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 148●
let nd = dist[ny * w + nx] + d - 149
if nd < dist[idx] { - 150●
dist[idx] = nd - 151
} - 152
} - 153
} - 154
} - 155
} - 156
// 内部行的边界列 - 157
for y in 1..<(h - 1) { - 158●
let x = w - 1 - 159
let idx = y * w + x - 160
if binary[idx] { - 161○
for m in mask2 { - 162○
let (dx, dy, d) = m - 163
let nx = x + dx - 164
let ny = y + dy - 165
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 166○
let nd = dist[ny * w + nx] + d - 167
if nd < dist[idx] { - 168○
dist[idx] = nd - 169
} - 170
} - 171
} - 172
} - 173
} - 174
dist - 175
} - 176
- 177
///| - 178
/// 距离变换可视化。将距离变换结果归一化到 [0, 255] 并返回灰度图。 - 179
pub fn visualize_distance_transform( - 180
dist : Array[Float], - 181
w : Int32, - 182
h : Int32, - 183
) -> @types.Image { - 184●
let n = w * h - 185●
let out : Array[Byte] = Array::make(n, b'\x00') - 186
if n == 0 { - 187○
return @types.Image::{ - 188
width: 0, - 189
height: 0, - 190
channels: 1, - 191○
data: Bytes::from_array([]), - 192
} - 193
} - 194
let mut max_dist = 0.0F - 195
for i in 0..<n { - 196●
if dist[i] > max_dist { - 197●
max_dist = dist[i] - 198
} - 199
} - 200
if max_dist > 0.0F { - 201●
for i in 0..<n { - 202●
let val = (dist[i] / max_dist * 255.0F).to_int() - 203●
out[i] = val.clamp(min=0, max=255).to_byte() - 204
} - 205
} - 206
@types.Image::{ - 207
width: w, - 208
height: h, - 209
channels: 1, - 210●
data: Bytes::from_array(out), - 211
} - 212
} - 213
- 214
///| - 215
/// 骨架化(基于距离变换)。 - 216
/// 提取二值图的骨架(中轴线)。 - 217
/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4,取首通道)。 - 218
pub fn skeletonize(img : @types.Image) -> @types.Image raise @types.LoadError { - 219●
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 220●
raise @types.LoadError::DecodeFailed("skeletonize 仅支持 channels=1/3/4") - 221
} - 222
let w = img.width - 223
let h = img.height - 224
let n = w * h - 225
if n == 0 { - 226○
return img - 227
} - 228
// 计算距离变换 - 229●
let dist = distance_transform(img, distance_type=2) - 230
// 骨架点:局部最大值 - 231
let ch = img.channels - 232●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 233
for y in 1..<(h - 1) { - 234●
for x in 1..<(w - 1) { - 235●
let idx = y * w + x - 236
if dist[idx] == 0.0F { - 237●
continue - 238
} - 239
let mut is_max = true - 240
for dy in -1..<=1 { - 241●
for dx in -1..<=1 { - 242●
if dx == 0 && dy == 0 { - 243●
continue - 244
} - 245
if dist[(y + dy) * w + (x + dx)] > dist[idx] { - 246●
is_max = false - 247
break - 248
} - 249
} - 250
if !is_max { - 251●
break - 252
} - 253
} - 254
if is_max { - 255●
for c in 0..<ch { - 256●
if ch == 1 { - 257●
out[idx] = b'\xFF' - 258○
} else if c < 3 { - 259○
out[idx * ch + c] = b'\xFF' - 260
} - 261
} - 262
if ch == 4 { - 263○
out[idx * ch + 3] = img.data[idx * ch + 3] - 264
} - 265
} - 266
} - 267
} - 268
@types.Image::{ - 269
width: w, - 270
height: h, - 271
channels: ch, - 272●
data: Bytes::from_array(out), - 273
} - 274
}