image.process\edge\contour_16f.mbt
97.2% · 106 / 109 points · 299 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 轮廓 16f contour 16f - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 轮廓提取与绘制 16-bit/float 变体 - 7
- 8
///| - 9
/// 提取轮廓 16-bit 版本。 - 10
/// @see find_contours 8-bit 版本,本函数为 Image16 变体 - 11
pub fn find_contours_16( - 12
img : @types.Image16, - 13
) -> Array[Contour] raise @types.LoadError { - 14
// 输入验证 - 15●
if img.width <= 0 || img.height <= 0 { - 16●
raise @types.LoadError::DecodeFailed( - 17
"find_contours_16: 图像尺寸必须大于0", - 18
) - 19
} - 20
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 21●
raise @types.LoadError::DecodeFailed( - 22
"find_contours_16: 仅支持 channels=1/3/4", - 23
) - 24
} - 25
let expected_size = img.width * img.height * img.channels * 2 - 26●
if img.data.length() != expected_size { - 27●
raise @types.LoadError::DecodeFailed( - 28
"find_contours_16: 数据大小不匹配,期望 " + - 29●
expected_size.to_string() + - 30
",实际 " + - 31●
img.data.length().to_string(), - 32
) - 33
} - 34
let w = img.width - 35
let h = img.height - 36
let ch = img.channels - 37●
let binary : Array[Bool] = Array::make(w * h, false) - 38
for i in 0..<(w * h) { - 39●
let v = img.data[i * ch * 2].to_int() | - 40●
(img.data[i * ch * 2 + 1].to_int() << 8) - 41
binary[i] = v > 0 - 42
} - 43●
let visited : Array[Bool] = Array::make(w * h, false) - 44
let contours : Array[Contour] = [] - 45
for y in 0..<h { - 46●
for x in 0..<w { - 47●
let idx = y * w + x - 48
if binary[idx] && !visited[idx] { - 49●
let is_outer = y == 0 || !binary[idx - w] - 50●
let contour = trace_boundary_16f(binary, visited, x, y, w, h, is_outer) - 51●
if contour.points.length() > 0 { - 52●
contours.push(contour) - 53
} - 54
} - 55
} - 56
} - 57
contours - 58
} - 59
- 60
///| - 61
/// 提取轮廓 float 版本。 - 62
/// @see find_contours 8-bit 版本,本函数为 ImageF 变体 - 63
pub fn find_contours_f( - 64
img : @types.ImageF, - 65
) -> Array[Contour] raise @types.LoadError { - 66
// 输入验证 - 67●
if img.width <= 0 || img.height <= 0 { - 68●
raise @types.LoadError::DecodeFailed( - 69
"find_contours_f: 图像尺寸必须大于0", - 70
) - 71
} - 72
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 73●
raise @types.LoadError::DecodeFailed( - 74
"find_contours_f: 仅支持 channels=1/3/4", - 75
) - 76
} - 77
let expected_size = img.width * img.height * img.channels * 4 - 78●
if img.data.length() != expected_size { - 79●
raise @types.LoadError::DecodeFailed( - 80
"find_contours_f: 数据大小不匹配,期望 " + - 81●
expected_size.to_string() + - 82
",实际 " + - 83●
img.data.length().to_string(), - 84
) - 85
} - 86
let w = img.width - 87
let h = img.height - 88
let ch = img.channels - 89●
let binary : Array[Bool] = Array::make(w * h, false) - 90
for i in 0..<(w * h) { - 91●
let bits = img.data[i * ch * 4].to_int() | - 92●
(img.data[i * ch * 4 + 1].to_int() << 8) | - 93●
(img.data[i * ch * 4 + 2].to_int() << 16) | - 94●
(img.data[i * ch * 4 + 3].to_int() << 24) - 95●
binary[i] = Float::reinterpret_from_int(bits) > 0.0F - 96
} - 97●
let visited : Array[Bool] = Array::make(w * h, false) - 98
let contours : Array[Contour] = [] - 99
for y in 0..<h { - 100●
for x in 0..<w { - 101●
let idx = y * w + x - 102
if binary[idx] && !visited[idx] { - 103●
let is_outer = y == 0 || !binary[idx - w] - 104●
let contour = trace_boundary_16f(binary, visited, x, y, w, h, is_outer) - 105●
if contour.points.length() > 0 { - 106●
contours.push(contour) - 107
} - 108
} - 109
} - 110
} - 111
contours - 112
} - 113
- 114
///| - 115
/// Moore 边界跟踪(16/f 共用) - 116
fn trace_boundary_16f( - 117
binary : Array[Bool], - 118
visited : Array[Bool], - 119
start_x : Int32, - 120
start_y : Int32, - 121
w : Int32, - 122
h : Int32, - 123
is_outer : Bool, - 124
) -> Contour { - 125●
let points : Array[ContourPoint] = [] - 126
let mut cx = start_x - 127
let mut cy = start_y - 128
let mut dir_idx = 0 - 129
let mut count = 0 - 130
let max_count = w * h * 8 + 1 - 131
while count < max_count { - 132●
let idx = cy * w + cx - 133
if !visited[idx] { - 134●
visited[idx] = true - 135●
points.push(ContourPoint::{ x: cx, y: cy, }) - 136
} - 137
count += 1 - 138
let mut found = false - 139
for i in 0..<8 { - 140●
let d = (dir_idx + i) % 8 - 141●
let (dx, dy) = match d { - 142●
0 => (1, 0) - 143●
1 => (1, 1) - 144●
2 => (0, 1) - 145●
3 => (-1, 1) - 146●
4 => (-1, 0) - 147●
5 => (-1, -1) - 148●
6 => (0, -1) - 149●
_ => (1, -1) - 150
} - 151
let nx = cx + dx - 152
let ny = cy + dy - 153
if nx >= 0 && nx < w && ny >= 0 && ny < h && binary[ny * w + nx] { - 154●
cx = nx - 155
cy = ny - 156
dir_idx = (d + 6) % 8 - 157
found = true - 158
break - 159
} - 160
} - 161
if !found { - 162○
break - 163
} - 164
if cx == start_x && cy == start_y { - 165●
break - 166
} - 167
} - 168
Contour::{ points, is_outer, } - 169
} - 170
- 171
///| - 172
/// 绘制轮廓 16-bit 版本。 - 173
/// @see draw_contours 8-bit 版本,本函数为 Image16 变体 - 174
pub fn draw_contours_16( - 175
img : @types.Image16, - 176
contours : Array[Contour], - 177
color : Array[Int32], - 178
) -> @types.Image16 raise @types.LoadError { - 179
// 输入验证 - 180●
if img.width <= 0 || img.height <= 0 { - 181●
raise @types.LoadError::DecodeFailed( - 182
"draw_contours_16: 图像尺寸必须大于0", - 183
) - 184
} - 185
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 186●
raise @types.LoadError::DecodeFailed( - 187
"draw_contours_16: 仅支持 channels=1/3/4", - 188
) - 189
} - 190
let expected_size = img.width * img.height * img.channels * 2 - 191●
if img.data.length() != expected_size { - 192●
raise @types.LoadError::DecodeFailed( - 193
"draw_contours_16: 数据大小不匹配,期望 " + - 194●
expected_size.to_string() + - 195
",实际 " + - 196●
img.data.length().to_string(), - 197
) - 198
} - 199●
if color.length() == 0 { - 200●
raise @types.LoadError::DecodeFailed( - 201
"draw_contours_16: color数组不能为空", - 202
) - 203
} - 204
let ch = img.channels - 205
let w = img.width - 206
let h = img.height - 207●
let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00') - 208
for i in 0..<(w * h * ch) { - 209●
out[i * 2] = img.data[i * 2] - 210
out[i * 2 + 1] = img.data[i * 2 + 1] - 211
} - 212◐
let draw_ch = if ch >= 3 { 3 } else { 1 } - 213
for contour in contours { - 214●
for pt in contour.points { - 215●
if pt.x >= 0 && pt.x < w && pt.y >= 0 && pt.y < h { - 216●
for c in 0..<draw_ch { - 217●
if c < color.length() { - 218●
let v = color[c] - 219●
out[((pt.y * w + pt.x) * ch + c) * 2] = (v & 0xFF).to_byte() - 220●
out[((pt.y * w + pt.x) * ch + c) * 2 + 1] = ((v >> 8) & 0xFF).to_byte() - 221
} - 222
} - 223
} - 224
} - 225
} - 226
@types.Image16::{ - 227
width: w, - 228
height: h, - 229
channels: ch, - 230●
data: Bytes::from_array(out), - 231
} - 232
} - 233
- 234
///| - 235
/// 绘制轮廓 float 版本。 - 236
/// @see draw_contours 8-bit 版本,本函数为 ImageF 变体 - 237
pub fn draw_contours_f( - 238
img : @types.ImageF, - 239
contours : Array[Contour], - 240
color : Array[Float], - 241
) -> @types.ImageF raise @types.LoadError { - 242
// 输入验证 - 243●
if img.width <= 0 || img.height <= 0 { - 244●
raise @types.LoadError::DecodeFailed( - 245
"draw_contours_f: 图像尺寸必须大于0", - 246
) - 247
} - 248
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 249●
raise @types.LoadError::DecodeFailed( - 250
"draw_contours_f: 仅支持 channels=1/3/4", - 251
) - 252
} - 253
let expected_size = img.width * img.height * img.channels * 4 - 254●
if img.data.length() != expected_size { - 255●
raise @types.LoadError::DecodeFailed( - 256
"draw_contours_f: 数据大小不匹配,期望 " + - 257●
expected_size.to_string() + - 258
",实际 " + - 259●
img.data.length().to_string(), - 260
) - 261
} - 262●
if color.length() == 0 { - 263●
raise @types.LoadError::DecodeFailed( - 264
"draw_contours_f: color数组不能为空", - 265
) - 266
} - 267
let ch = img.channels - 268
let w = img.width - 269
let h = img.height - 270●
let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00') - 271
for i in 0..<(w * h * ch) { - 272●
out[i * 4] = img.data[i * 4] - 273
out[i * 4 + 1] = img.data[i * 4 + 1] - 274
out[i * 4 + 2] = img.data[i * 4 + 2] - 275
out[i * 4 + 3] = img.data[i * 4 + 3] - 276
} - 277◐
let draw_ch = if ch >= 3 { 3 } else { 1 } - 278
for contour in contours { - 279●
for pt in contour.points { - 280●
if pt.x >= 0 && pt.x < w && pt.y >= 0 && pt.y < h { - 281●
for c in 0..<draw_ch { - 282●
if c < color.length() { - 283●
let bits = color[c].reinterpret_as_int() - 284●
out[((pt.y * w + pt.x) * ch + c) * 4] = (bits & 0xFF).to_byte() - 285●
out[((pt.y * w + pt.x) * ch + c) * 4 + 1] = ((bits >> 8) & 0xFF).to_byte() - 286●
out[((pt.y * w + pt.x) * ch + c) * 4 + 2] = ((bits >> 16) & 0xFF).to_byte() - 287●
out[((pt.y * w + pt.x) * ch + c) * 4 + 3] = ((bits >> 24) & 0xFF).to_byte() - 288
} - 289
} - 290
} - 291
} - 292
} - 293
@types.ImageF::{ - 294
width: w, - 295
height: h, - 296
channels: ch, - 297●
data: Bytes::from_array(out), - 298
} - 299
}