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