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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// 轮廓提取与绘制 16-bit/float 变体
///|
/// 提取轮廓 16-bit 版本。
pub fn find_contours_16(
img : @types.Image16,
) -> Array[Contour] raise @types.LoadError {
// 输入验证
if img.width <= 0 || img.height <= 0 {
raise @types.LoadError::DecodeFailed(
"find_contours_16: 图像尺寸必须大于0",
)
}
if img.channels != 1 && img.channels != 3 && img.channels != 4 {
raise @types.LoadError::DecodeFailed(
"find_contours_16: 仅支持 channels=1/3/4",
)
}
let expected_size = img.width * img.height * img.channels * 2
if img.data.length() != expected_size {
raise @types.LoadError::DecodeFailed(
"find_contours_16: 数据大小不匹配,期望 " +
expected_size.to_string() +
",实际 " +
img.data.length().to_string(),
)
}
let w = img.width
let h = img.height
let ch = img.channels
let binary : Array[Bool] = Array::make(w * h, false)
for i in 0..<(w * h) {
let v = img.data[i * ch * 2].to_int() |
(img.data[i * ch * 2 + 1].to_int() << 8)
binary[i] = v > 0
}
let visited : Array[Bool] = Array::make(w * h, false)
let contours : Array[Contour] = []
for y in 0..<h {
for x in 0..<w {
let idx = y * w + x
if binary[idx] && !visited[idx] {
let is_outer = y == 0 || !binary[idx - w]
let contour = trace_boundary_16f(binary, visited, x, y, w, h, is_outer)
if contour.points.length() > 0 {
contours.push(contour)
}
}
}
}
contours
}
///|
/// 提取轮廓 float 版本。
pub fn find_contours_f(
img : @types.ImageF,
) -> Array[Contour] raise @types.LoadError {
// 输入验证
if img.width <= 0 || img.height <= 0 {
raise @types.LoadError::DecodeFailed(
"find_contours_f: 图像尺寸必须大于0",
)
}
if img.channels != 1 && img.channels != 3 && img.channels != 4 {
raise @types.LoadError::DecodeFailed(
"find_contours_f: 仅支持 channels=1/3/4",
)
}
let expected_size = img.width * img.height * img.channels * 4
if img.data.length() != expected_size {
raise @types.LoadError::DecodeFailed(
"find_contours_f: 数据大小不匹配,期望 " +
expected_size.to_string() +
",实际 " +
img.data.length().to_string(),
)
}
let w = img.width
let h = img.height
let ch = img.channels
let binary : Array[Bool] = Array::make(w * h, false)
for i in 0..<(w * h) {
let bits = img.data[i * ch * 4].to_int() |
(img.data[i * ch * 4 + 1].to_int() << 8) |
(img.data[i * ch * 4 + 2].to_int() << 16) |
(img.data[i * ch * 4 + 3].to_int() << 24)
binary[i] = Float::reinterpret_from_int(bits) > 0.0F
}
let visited : Array[Bool] = Array::make(w * h, false)
let contours : Array[Contour] = []
for y in 0..<h {
for x in 0..<w {
let idx = y * w + x
if binary[idx] && !visited[idx] {
let is_outer = y == 0 || !binary[idx - w]
let contour = trace_boundary_16f(binary, visited, x, y, w, h, is_outer)
if contour.points.length() > 0 {
contours.push(contour)
}
}
}
}
contours
}
///|
/// Moore 边界跟踪(16/f 共用)
fn trace_boundary_16f(
binary : Array[Bool],
visited : Array[Bool],
start_x : Int,
start_y : Int,
w : Int,
h : Int,
is_outer : Bool,
) -> Contour {
let points : Array[ContourPoint] = []
let mut cx = start_x
let mut cy = start_y
let mut dir_idx = 0
let mut count = 0
let max_count = w * h * 8 + 1
while count < max_count {
let idx = cy * w + cx
if !visited[idx] {
visited[idx] = true
points.push(ContourPoint::{ x: cx, y: cy })
}
count += 1
let mut found = false
for i in 0..<8 {
let d = (dir_idx + i) % 8
let (dx, dy) = match d {
0 => (1, 0)
1 => (1, 1)
2 => (0, 1)
3 => (-1, 1)
4 => (-1, 0)
5 => (-1, -1)
6 => (0, -1)
_ => (1, -1)
}
let nx = cx + dx
let ny = cy + dy
if nx >= 0 && nx < w && ny >= 0 && ny < h && binary[ny * w + nx] {
cx = nx
cy = ny
dir_idx = (d + 6) % 8
found = true
break
}
}
if !found {
break
}
if cx == start_x && cy == start_y {
break
}
}
Contour::{ points, is_outer }
}
///|
/// 绘制轮廓 16-bit 版本。
pub fn draw_contours_16(
img : @types.Image16,
contours : Array[Contour],
color : Array[Int],
) -> @types.Image16 raise @types.LoadError {
// 输入验证
if img.width <= 0 || img.height <= 0 {
raise @types.LoadError::DecodeFailed(
"draw_contours_16: 图像尺寸必须大于0",
)
}
if img.channels != 1 && img.channels != 3 && img.channels != 4 {
raise @types.LoadError::DecodeFailed(
"draw_contours_16: 仅支持 channels=1/3/4",
)
}
let expected_size = img.width * img.height * img.channels * 2
if img.data.length() != expected_size {
raise @types.LoadError::DecodeFailed(
"draw_contours_16: 数据大小不匹配,期望 " +
expected_size.to_string() +
",实际 " +
img.data.length().to_string(),
)
}
if color.length() == 0 {
raise @types.LoadError::DecodeFailed(
"draw_contours_16: color数组不能为空",
)
}
let ch = img.channels
let w = img.width
let h = img.height
let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00')
for i in 0..<(w * h * ch) {
out[i * 2] = img.data[i * 2]
out[i * 2 + 1] = img.data[i * 2 + 1]
}
let draw_ch = if ch >= 3 { 3 } else { 1 }
for contour in contours {
for pt in contour.points {
if pt.x >= 0 && pt.x < w && pt.y >= 0 && pt.y < h {
for c in 0..<draw_ch {
if c < color.length() {
let v = color[c]
out[((pt.y * w + pt.x) * ch + c) * 2] = (v & 0xFF).to_byte()
out[((pt.y * w + pt.x) * ch + c) * 2 + 1] = ((v >> 8) & 0xFF).to_byte()
}
}
}
}
}
@types.Image16::{
width: w,
height: h,
channels: ch,
data: Bytes::from_array(out),
}
}
///|
/// 绘制轮廓 float 版本。
pub fn draw_contours_f(
img : @types.ImageF,
contours : Array[Contour],
color : Array[Float],
) -> @types.ImageF raise @types.LoadError {
// 输入验证
if img.width <= 0 || img.height <= 0 {
raise @types.LoadError::DecodeFailed(
"draw_contours_f: 图像尺寸必须大于0",
)
}
if img.channels != 1 && img.channels != 3 && img.channels != 4 {
raise @types.LoadError::DecodeFailed(
"draw_contours_f: 仅支持 channels=1/3/4",
)
}
let expected_size = img.width * img.height * img.channels * 4
if img.data.length() != expected_size {
raise @types.LoadError::DecodeFailed(
"draw_contours_f: 数据大小不匹配,期望 " +
expected_size.to_string() +
",实际 " +
img.data.length().to_string(),
)
}
if color.length() == 0 {
raise @types.LoadError::DecodeFailed(
"draw_contours_f: color数组不能为空",
)
}
let ch = img.channels
let w = img.width
let h = img.height
let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00')
for i in 0..<(w * h * ch) {
out[i * 4] = img.data[i * 4]
out[i * 4 + 1] = img.data[i * 4 + 1]
out[i * 4 + 2] = img.data[i * 4 + 2]
out[i * 4 + 3] = img.data[i * 4 + 3]
}
let draw_ch = if ch >= 3 { 3 } else { 1 }
for contour in contours {
for pt in contour.points {
if pt.x >= 0 && pt.x < w && pt.y >= 0 && pt.y < h {
for c in 0..<draw_ch {
if c < color.length() {
let bits = color[c].reinterpret_as_int()
out[((pt.y * w + pt.x) * ch + c) * 4] = (bits & 0xFF).to_byte()
out[((pt.y * w + pt.x) * ch + c) * 4 + 1] = ((bits >> 8) & 0xFF).to_byte()
out[((pt.y * w + pt.x) * ch + c) * 4 + 2] = ((bits >> 16) & 0xFF).to_byte()
out[((pt.y * w + pt.x) * ch + c) * 4 + 3] = ((bits >> 24) & 0xFF).to_byte()
}
}
}
}
}
@types.ImageF::{
width: w,
height: h,
channels: ch,
data: Bytes::from_array(out),
}
}