image.process\segment\connected_components.mbt
97.5% · 78 / 80 points · 262 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 连通域 connected components - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 连通域标记(纯 MoonBit,两遍扫描 + Union-Find) - 7
- 8
///| - 9
/// 连通域信息 - 10
pub struct ConnectedComponent { - 11
label : Int32 // 标签号(从 1 开始) - 12
area : Int32 // 面积(像素数) - 13
bbox_x : Int32 // 边界框左上角 x - 14
bbox_y : Int32 // 边界框左上角 y - 15
bbox_w : Int32 // 边界框宽 - 16
bbox_h : Int32 // 边界框高 - 17
centroid_x : Float // 质心 x - 18
centroid_y : Float // 质心 y - 19
} - 20
- 21
///| - 22
/// 连通域标记。对二值图(非零为前景)标记连通域。 - 23
/// connectivity=4 为四连通,connectivity=8 为八连通。 - 24
/// 返回标记图(每个像素的标签,0=背景,1..N=前景域)和连通域信息列表。 - 25
pub fn connected_components( - 26
img : @types.Image, - 27
connectivity : Int32, - 28
) -> (ConnectedComponentLabelImage, Array[ConnectedComponent]) raise @types.LoadError { - 29●
if connectivity != 4 && connectivity != 8 { - 30●
raise @types.LoadError::DecodeFailed("connectivity 必须为 4 或 8") - 31
} - 32
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 33●
raise @types.LoadError::DecodeFailed( - 34
"connected_components 仅支持 channels=1/3/4", - 35
) - 36
} - 37
let w = img.width - 38
let h = img.height - 39
let n = w * h - 40
// 转为二值 - 41●
let binary : Array[Bool] = Array::make(n, false) - 42
let ch = img.channels - 43
for i in 0..<n { - 44●
binary[i] = img.data[i * ch].to_int() > 0 - 45
} - 46
// 第一遍扫描:标记 + Union-Find - 47●
let labels : Array[Int32] = Array::make(n, 0) - 48
let parent : Array[Int32] = [0] // parent[0] 是背景 - 49
let mut next_label = 1 - 50
for y in 0..<h { - 51●
for x in 0..<w { - 52●
let idx = y * w + x - 53
if !binary[idx] { - 54●
continue - 55
} - 56
// 检查邻居 - 57
let mut left = 0 - 58
let mut up = 0 - 59
let mut ul = 0 - 60
let mut ur = 0 - 61
if x > 0 && binary[idx - 1] { - 62●
left = labels[idx - 1] - 63
} - 64
if y > 0 && binary[idx - w] { - 65●
up = labels[idx - w] - 66
} - 67
if connectivity == 8 { - 68●
if x > 0 && y > 0 && binary[idx - w - 1] { - 69●
ul = labels[idx - w - 1] - 70
} - 71
if x < w - 1 && y > 0 && binary[idx - w + 1] { - 72●
ur = labels[idx - w + 1] - 73
} - 74
} - 75
// 收集非零邻居标签(用变量代替数组,避免堆分配) - 76
let mut n1 = 0 - 77
let mut n2 = 0 - 78
let mut n3 = 0 - 79
let mut n4 = 0 - 80
let mut nc = 0 - 81
if left > 0 { - 82●
n1 = left - 83
nc = 1 - 84
} - 85
if up > 0 { - 86●
if nc == 0 { - 87●
n1 = up - 88
nc = 1 - 89
} else { - 90●
n2 = up - 91
nc = 2 - 92
} - 93
} - 94
if ul > 0 { - 95●
if nc == 0 { - 96●
n1 = ul - 97
nc = 1 - 98●
} else if nc == 1 { - 99●
n2 = ul - 100
nc = 2 - 101
} else { - 102●
n3 = ul - 103
nc = 3 - 104
} - 105
} - 106
if ur > 0 { - 107●
if nc == 0 { - 108●
n1 = ur - 109
nc = 1 - 110●
} else if nc == 1 { - 111●
n2 = ur - 112
nc = 2 - 113●
} else if nc == 2 { - 114●
n3 = ur - 115
nc = 3 - 116
} else { - 117●
n4 = ur - 118
nc = 4 - 119
} - 120
} - 121
if nc == 0 { - 122●
labels[idx] = next_label - 123●
parent.push(next_label) - 124
next_label += 1 - 125
} else { - 126●
let mut min_label = n1 - 127
if nc >= 2 && n2 < min_label { - 128●
min_label = n2 - 129
} - 130
if nc >= 3 && n3 < min_label { - 131●
min_label = n3 - 132
} - 133
if nc >= 4 && n4 < min_label { - 134●
min_label = n4 - 135
} - 136
labels[idx] = min_label - 137●
union_labels(parent, min_label, n1) - 138
if nc >= 2 { - 139●
union_labels(parent, min_label, n2) - 140
} - 141
if nc >= 3 { - 142●
union_labels(parent, min_label, n3) - 143
} - 144
if nc >= 4 { - 145●
union_labels(parent, min_label, n4) - 146
} - 147
} - 148
} - 149
} - 150
// 压缩路径并建立标签映射 - 151●
let label_map : Array[Int32] = Array::make(parent.length(), 0) - 152
let mut final_count = 0 - 153●
for i in 1..<parent.length() { - 154●
let root = find_root(parent, i) - 155
if label_map[root] == 0 { - 156●
final_count += 1 - 157
label_map[root] = final_count - 158
} - 159
label_map[i] = label_map[root] - 160
} - 161
// 第二遍扫描:替换为最终标签 - 162●
let out_labels : Array[Int32] = Array::make(n, 0) - 163
for i in 0..<n { - 164●
out_labels[i] = label_map[labels[i]] - 165
} - 166
// 统计连通域信息 - 167●
let areas : Array[Int32] = Array::make(final_count + 1, 0) - 168●
let sum_x : Array[Int32] = Array::make(final_count + 1, 0) - 169●
let sum_y : Array[Int32] = Array::make(final_count + 1, 0) - 170●
let min_x : Array[Int32] = Array::make(final_count + 1, w) - 171●
let min_y : Array[Int32] = Array::make(final_count + 1, h) - 172●
let max_x : Array[Int32] = Array::make(final_count + 1, 0) - 173●
let max_y : Array[Int32] = Array::make(final_count + 1, 0) - 174
for y in 0..<h { - 175●
for x in 0..<w { - 176●
let label = out_labels[y * w + x] - 177
if label == 0 { - 178●
continue - 179
} - 180
areas[label] += 1 - 181
sum_x[label] += x - 182
sum_y[label] += y - 183
if x < min_x[label] { - 184●
min_x[label] = x - 185
} - 186
if y < min_y[label] { - 187●
min_y[label] = y - 188
} - 189
if x > max_x[label] { - 190●
max_x[label] = x - 191
} - 192
if y > max_y[label] { - 193●
max_y[label] = y - 194
} - 195
} - 196
} - 197
let components : Array[ConnectedComponent] = [] - 198
for label in 1..<=final_count { - 199●
let area = areas[label] - 200
let cx = if area > 0 { - 201●
Float::from_int(sum_x[label]) / Float::from_int(area) - 202
} else { - 203○
0.0F - 204
} - 205
let cy = if area > 0 { - 206●
Float::from_int(sum_y[label]) / Float::from_int(area) - 207
} else { - 208○
0.0F - 209
} - 210●
components.push(ConnectedComponent::{ - 211
label, - 212
area, - 213
bbox_x: min_x[label], - 214
bbox_y: min_y[label], - 215
bbox_w: max_x[label] - min_x[label] + 1, - 216
bbox_h: max_y[label] - min_y[label] + 1, - 217
centroid_x: cx, - 218
centroid_y: cy, - 219
}) - 220
} - 221
( - 222
ConnectedComponentLabelImage::{ width: w, height: h, labels: out_labels, }, - 223
components, - 224
) - 225
} - 226
- 227
///| - 228
/// 连通域标记图 - 229
pub struct ConnectedComponentLabelImage { - 230
width : Int32 - 231
height : Int32 - 232
labels : Array[Int32] // 每个像素的标签(0=背景) - 233
} - 234
- 235
///| - 236
fn find_root(parent : Array[Int32], x : Int32) -> Int32 { - 237●
let mut root = x - 238
while parent[root] != root { - 239●
root = parent[root] - 240
} - 241
// 路径压缩 - 242
let mut i = x - 243
while parent[i] != root { - 244●
let next = parent[i] - 245
parent[i] = root - 246
i = next - 247
} - 248
root - 249
} - 250
- 251
///| - 252
fn union_labels(parent : Array[Int32], a : Int32, b : Int32) -> Unit { - 253●
let ra = find_root(parent, a) - 254●
let rb = find_root(parent, b) - 255
if ra != rb { - 256●
if ra < rb { - 257●
parent[rb] = ra - 258
} else { - 259●
parent[ra] = rb - 260
} - 261
} - 262
}