image.process\edge\contour.mbt
95.9% · 70 / 73 points · 224 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 轮廓 contour - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 轮廓提取与绘制(纯 MoonBit) - 7
- 8
///| - 9
/// 轮廓点 - 10
pub(all) struct ContourPoint { - 11
x : Int32 - 12
y : Int32 - 13
} - 14
- 15
///| - 16
/// 轮廓 - 17
pub(all) struct Contour { - 18
points : Array[ContourPoint] - 19
is_outer : Bool // true=外轮廓, false=内轮廓(孔洞) - 20
} - 21
- 22
///| - 23
/// 提取轮廓(Suzuki 边界跟踪简化版)。 - 24
/// 对二值图(非零为前景)提取所有外轮廓。 - 25
/// 仅处理灰度图(channels=1)或 RGB 图(channels=3/4,取首通道)。 - 26
pub fn find_contours( - 27
img : @types.Image, - 28
) -> Array[Contour] raise @types.LoadError { - 29
// 输入验证 - 30●
if img.width <= 0 || img.height <= 0 { - 31●
raise @types.LoadError::DecodeFailed( - 32
"find_contours: 图像尺寸必须大于0", - 33
) - 34
} - 35
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 36●
raise @types.LoadError::DecodeFailed( - 37
"find_contours: 仅支持 channels=1/3/4", - 38
) - 39
} - 40
let expected_size = img.width * img.height * img.channels - 41●
if img.data.length() != expected_size { - 42●
raise @types.LoadError::DecodeFailed( - 43
"find_contours: 数据大小不匹配,期望 " + - 44●
expected_size.to_string() + - 45
",实际 " + - 46●
img.data.length().to_string(), - 47
) - 48
} - 49
let w = img.width - 50
let h = img.height - 51
let ch = img.channels - 52
// 转为二值(直接从首通道计算,消除中间数组) - 53●
let binary : Array[Bool] = Array::make(w * h, false) - 54
for i in 0..<(w * h) { - 55●
binary[i] = img.data[i * ch].to_int() > 0 - 56
} - 57●
let visited : Array[Bool] = Array::make(w * h, false) - 58
let contours : Array[Contour] = [] - 59
for y in 0..<h { - 60●
for x in 0..<w { - 61●
let idx = y * w + x - 62
if binary[idx] && !visited[idx] { - 63
// 检查是否为外轮廓起点(上方为背景或边界) - 64●
let is_outer = y == 0 || !binary[idx - w] - 65●
let contour = trace_boundary(binary, visited, x, y, w, h, is_outer) - 66●
if contour.points.length() > 0 { - 67●
contours.push(contour) - 68
} - 69
} - 70
} - 71
} - 72
contours - 73
} - 74
- 75
///| - 76
/// Moore 边界跟踪 - 77
fn trace_boundary( - 78
binary : Array[Bool], - 79
visited : Array[Bool], - 80
start_x : Int32, - 81
start_y : Int32, - 82
w : Int32, - 83
h : Int32, - 84
is_outer : Bool, - 85
) -> Contour { - 86●
let points : Array[ContourPoint] = [] - 87
let mut cx = start_x - 88
let mut cy = start_y - 89
let mut dir_idx = 0 // 起始方向 - 90
let mut count = 0 - 91
let max_count = w * h * 8 + 1 - 92
while count < max_count { - 93●
let idx = cy * w + cx - 94
if !visited[idx] { - 95●
visited[idx] = true - 96●
points.push(ContourPoint::{ x: cx, y: cy, }) - 97
} - 98
count += 1 - 99
// 寻找下一个前景像素(match 替代数组查找,消除堆分配) - 100
let mut found = false - 101
for i in 0..<8 { - 102●
let d = (dir_idx + i) % 8 - 103●
let (dx, dy) = match d { - 104●
0 => (1, 0) - 105●
1 => (1, 1) - 106●
2 => (0, 1) - 107●
3 => (-1, 1) - 108●
4 => (-1, 0) - 109●
5 => (-1, -1) - 110●
6 => (0, -1) - 111●
_ => (1, -1) - 112
} - 113
let nx = cx + dx - 114
let ny = cy + dy - 115
if nx >= 0 && nx < w && ny >= 0 && ny < h && binary[ny * w + nx] { - 116●
cx = nx - 117
cy = ny - 118
// 下次搜索方向:回退两个方向 - 119
dir_idx = (d + 6) % 8 - 120
found = true - 121
break - 122
} - 123
} - 124
if !found { - 125●
break - 126
} - 127
// 回到起点则结束 - 128
if cx == start_x && cy == start_y { - 129●
break - 130
} - 131
} - 132
Contour::{ points, is_outer, } - 133
} - 134
- 135
///| - 136
/// 绘制轮廓到图像上。color 为绘制颜色数组(长度 >= channels)。 - 137
pub fn draw_contours( - 138
img : @types.Image, - 139
contours : Array[Contour], - 140
color : Array[Byte], - 141
) -> @types.Image raise @types.LoadError { - 142
// 输入验证 - 143●
if img.width <= 0 || img.height <= 0 { - 144●
raise @types.LoadError::DecodeFailed( - 145
"draw_contours: 图像尺寸必须大于0", - 146
) - 147
} - 148
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 149●
raise @types.LoadError::DecodeFailed( - 150
"draw_contours: 仅支持 channels=1/3/4", - 151
) - 152
} - 153
let expected_size = img.width * img.height * img.channels - 154●
if img.data.length() != expected_size { - 155●
raise @types.LoadError::DecodeFailed( - 156
"draw_contours: 数据大小不匹配,期望 " + - 157●
expected_size.to_string() + - 158
",实际 " + - 159●
img.data.length().to_string(), - 160
) - 161
} - 162●
if color.length() == 0 { - 163●
raise @types.LoadError::DecodeFailed( - 164
"draw_contours: color数组不能为空", - 165
) - 166
} - 167
let ch = img.channels - 168
let w = img.width - 169
let h = img.height - 170●
let out : Array[Byte] = Array::make(w * h * ch, b'\x00') - 171
for i in 0..<(w * h * ch) { - 172●
out[i] = img.data[i] - 173
} - 174◐
let draw_ch = if ch >= 3 { 3 } else { 1 } - 175
for contour in contours { - 176●
for pt in contour.points { - 177●
if pt.x >= 0 && pt.x < w && pt.y >= 0 && pt.y < h { - 178●
for c in 0..<draw_ch { - 179●
if c < color.length() { - 180●
out[(pt.y * w + pt.x) * ch + c] = color[c] - 181
} - 182
} - 183
} - 184
} - 185
} - 186
@types.Image::{ - 187
width: w, - 188
height: h, - 189
channels: ch, - 190●
data: Bytes::from_array(out), - 191
} - 192
} - 193
- 194
///| - 195
/// 计算轮廓周长 - 196
pub fn contour_perimeter(contour : Contour) -> Float { - 197●
let pts = contour.points - 198●
if pts.length() < 2 { - 199○
return 0.0F - 200
} - 201
let mut perim = 0.0F - 202●
for i in 0..<pts.length() { - 203●
let j = (i + 1) % pts.length() - 204
let dx = pts[i].x - pts[j].x - 205
let dy = pts[i].y - pts[j].y - 206●
perim += Float::from_int(dx * dx + dy * dy).sqrt() - 207
} - 208
perim - 209
} - 210
- 211
///| - 212
/// 计算轮廓面积(鞋带公式) - 213
pub fn contour_area(contour : Contour) -> Float { - 214●
let pts = contour.points - 215●
if pts.length() < 3 { - 216○
return 0.0F - 217
} - 218
let mut area = 0.0F - 219●
for i in 0..<pts.length() { - 220●
let j = (i + 1) % pts.length() - 221●
area += Float::from_int(pts[i].x * pts[j].y - pts[j].x * pts[i].y) - 222
} - 223●
(area / 2.0F).abs() - 224
}