image.process\edge\contour_analysis.mbt
90.6% · 87 / 96 points · 354 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 analysis - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 轮廓分析(纯 MoonBit):凸包 + 多边形逼近 + 矩 + Hu矩 + 最小外接圆 - 7
- 8
///| - 9
/// 图像矩(空间矩 m00..m22 + 中心矩 mu20..mu02 + 归一化矩 nu20..nu02) - 10
pub(all) struct Moments { - 11
m00 : Double - 12
m10 : Double - 13
m01 : Double - 14
m20 : Double - 15
m11 : Double - 16
m02 : Double - 17
m30 : Double - 18
m21 : Double - 19
m12 : Double - 20
m03 : Double - 21
mu20 : Double - 22
mu11 : Double - 23
mu02 : Double - 24
mu30 : Double - 25
mu21 : Double - 26
mu12 : Double - 27
mu03 : Double - 28
nu20 : Double - 29
nu11 : Double - 30
nu02 : Double - 31
} - 32
- 33
///| - 34
/// 凸包(Graham 扫描法)。输入点集,返回凸包顶点(逆时针)。 - 35
pub fn convex_hull(points : Array[ContourPoint]) -> Array[ContourPoint] { - 36
// 空数组返回空数组 - 37●
if points.length() == 0 { - 38●
return [] - 39
} - 40
- 41●
let n = points.length() - 42
if n <= 2 { - 43●
let out : Array[ContourPoint] = [] - 44
for p in points { - 45●
out.push(p) - 46
} - 47
return out - 48
} - 49
// 找最下方的点(y 最大,x 最小) - 50
let mut pivot = 0 - 51
for i in 1..<n { - 52●
if points[i].y > points[pivot].y || - 53
(points[i].y == points[pivot].y && points[i].x < points[pivot].x) { - 54●
pivot = i - 55
} - 56
} - 57
// 按极角排序 - 58
let p0 = points[pivot] - 59
let sorted : Array[ContourPoint] = [] - 60
for i in 0..<n { - 61●
if i != pivot { - 62●
sorted.push(points[i]) - 63
} - 64
} - 65●
sorted.sort_by(fn(a, b) { - 66●
let cross = (a.x - p0.x) * (b.y - p0.y) - (a.y - p0.y) * (b.x - p0.x) - 67●
(0 - cross).compare(0) - 68
}) - 69
// Graham 扫描 - 70
let hull : Array[ContourPoint] = [p0] - 71
for p in sorted { - 72●
while hull.length() >= 2 { - 73●
let m = hull.length() - 74
let p1 = hull[m - 2] - 75
let p2 = hull[m - 1] - 76
let cross = (p2.x - p1.x) * (p.y - p1.y) - (p2.y - p1.y) * (p.x - p1.x) - 77
if cross <= 0 { - 78●
ignore(hull.pop()) - 79
} else { - 80●
break - 81
} - 82
} - 83●
hull.push(p) - 84
} - 85
hull - 86
} - 87
- 88
///| - 89
/// Douglas-Peucker 多边形逼近。简化轮廓,保留主要形状。 - 90
pub fn approx_poly_dp( - 91
points : Array[ContourPoint], - 92
epsilon : Float, - 93
closed~ : Bool = true, - 94
) -> Array[ContourPoint] raise @types.LoadError { - 95
// 参数验证 - 96●
if epsilon < 0.0F { - 97●
raise @types.LoadError::DecodeFailed("approx_poly_dp: epsilon必须 >= 0") - 98
} - 99
// 空数组返回空数组 - 100●
if points.length() == 0 { - 101●
return [] - 102
} - 103
- 104●
let n = points.length() - 105
if n <= 2 { - 106○
let out : Array[ContourPoint] = [] - 107
for p in points { - 108○
out.push(p) - 109
} - 110
return out - 111
} - 112
// 标记保留的点 - 113●
let keep : Array[Bool] = Array::make(n, false) - 114
keep[0] = true - 115
keep[n - 1] = true - 116●
approx_dp_recursive(points, 0, n - 1, epsilon, keep) - 117
if closed { - 118
// 对闭合轮廓,还需处理首尾之间的段 - 119●
approx_dp_recursive(points, n - 1, 0, epsilon, keep) - 120
} - 121
let out : Array[ContourPoint] = [] - 122
for i in 0..<n { - 123●
if keep[i] { - 124●
out.push(points[i]) - 125
} - 126
} - 127
out - 128
} - 129
- 130
///| - 131
fn approx_dp_recursive( - 132
points : Array[ContourPoint], - 133
start : Int32, - 134
end : Int32, - 135
epsilon : Float, - 136
keep : Array[Bool], - 137
) -> Unit { - 138●
let n = points.length() - 139
let mut max_dist = 0.0F - 140
let mut max_idx = -1 - 141
let p_start = points[start] - 142
let p_end = points[end] - 143●
let dx = Float::from_int(p_end.x - p_start.x) - 144●
let dy = Float::from_int(p_end.y - p_start.y) - 145
let len_sq = dx * dx + dy * dy - 146
let mut i = (start + 1) % n - 147
while i != end { - 148●
let p = points[i] - 149●
let px = Float::from_int(p.x - p_start.x) - 150●
let py = Float::from_int(p.y - p_start.y) - 151
let dist = if len_sq == 0.0F { - 152●
(px * px + py * py).sqrt() - 153
} else { - 154●
let t = (px * dx + py * dy) / len_sq - 155
let proj_x = t * dx - 156
let proj_y = t * dy - 157●
((px - proj_x) * (px - proj_x) + (py - proj_y) * (py - proj_y)).sqrt() - 158
} - 159
if dist > max_dist { - 160●
max_dist = dist - 161
max_idx = i - 162
} - 163
i = (i + 1) % n - 164
if i == start { - 165○
break - 166
} - 167
} - 168
if max_idx >= 0 && max_dist > epsilon { - 169●
keep[max_idx] = true - 170●
approx_dp_recursive(points, start, max_idx, epsilon, keep) - 171●
approx_dp_recursive(points, max_idx, end, epsilon, keep) - 172
} - 173
} - 174
- 175
///| - 176
/// 计算轮廓的空间矩和中心矩。 - 177
pub fn image_moments(points : Array[ContourPoint]) -> Moments { - 178
// 空数组返回零矩 - 179●
if points.length() == 0 { - 180●
return Moments::{ - 181
m00: 0.0, - 182
m10: 0.0, - 183
m01: 0.0, - 184
m20: 0.0, - 185
m11: 0.0, - 186
m02: 0.0, - 187
m30: 0.0, - 188
m21: 0.0, - 189
m12: 0.0, - 190
m03: 0.0, - 191
mu20: 0.0, - 192
mu11: 0.0, - 193
mu02: 0.0, - 194
mu30: 0.0, - 195
mu21: 0.0, - 196
mu12: 0.0, - 197
mu03: 0.0, - 198
nu20: 0.0, - 199
nu11: 0.0, - 200
nu02: 0.0, - 201
} - 202
} - 203
- 204
let mut m00 = 0.0 - 205
let mut m10 = 0.0 - 206
let mut m01 = 0.0 - 207
let mut m20 = 0.0 - 208
let mut m11 = 0.0 - 209
let mut m02 = 0.0 - 210
let mut m30 = 0.0 - 211
let mut m21 = 0.0 - 212
let mut m12 = 0.0 - 213
let mut m03 = 0.0 - 214
for p in points { - 215●
let x = p.x.to_double() - 216●
let y = p.y.to_double() - 217
m00 += 1.0 - 218
m10 += x - 219
m01 += y - 220
m20 += x * x - 221
m11 += x * y - 222
m02 += y * y - 223
m30 += x * x * x - 224
m21 += x * x * y - 225
m12 += x * y * y - 226
m03 += y * y * y - 227
} - 228
// 中心矩 - 229◐
let x_bar = if m00 > 0.0 { m10 / m00 } else { 0.0 } - 230◐
let y_bar = if m00 > 0.0 { m01 / m00 } else { 0.0 } - 231
let mu20 = m20 - x_bar * m10 - 232
let mu11 = m11 - x_bar * m01 - 233
let mu02 = m02 - y_bar * m01 - 234
let mu30 = m30 - 3.0 * x_bar * m20 + 2.0 * x_bar * x_bar * m10 - 235
let mu21 = m21 - 2.0 * x_bar * m11 - y_bar * m20 + 2.0 * x_bar * x_bar * m01 - 236
let mu12 = m12 - 2.0 * y_bar * m11 - x_bar * m02 + 2.0 * y_bar * y_bar * m10 - 237
let mu03 = m03 - 3.0 * y_bar * m02 + 2.0 * y_bar * y_bar * m01 - 238
// 归一化矩 - 239◐
let nu20 = if m00 > 0.0 { mu20 / (m00 * m00) } else { 0.0 } - 240◐
let nu11 = if m00 > 0.0 { mu11 / (m00 * m00) } else { 0.0 } - 241◐
let nu02 = if m00 > 0.0 { mu02 / (m00 * m00) } else { 0.0 } - 242
Moments::{ - 243
m00, - 244
m10, - 245
m01, - 246
m20, - 247
m11, - 248
m02, - 249
m30, - 250
m21, - 251
m12, - 252
m03, - 253
mu20, - 254
mu11, - 255
mu02, - 256
mu30, - 257
mu21, - 258
mu12, - 259
mu03, - 260
nu20, - 261
nu11, - 262
nu02, - 263
} - 264
} - 265
- 266
///| - 267
/// 计算 Hu 矩不变量(7 个)。对平移、旋转、缩放不变。 - 268
pub fn hu_moments(m : Moments) -> Array[Double] raise @types.LoadError { - 269
// 参数验证 - 270●
if m.m00 == 0.0 { - 271●
raise @types.LoadError::DecodeFailed( - 272
"hu_moments: m00不能为0(空轮廓)", - 273
) - 274
} - 275
- 276●
let hu : Array[Double] = Array::make(7, 0.0) - 277
let n20 = m.nu20 - 278
let n11 = m.nu11 - 279
let n02 = m.nu02 - 280●
let n30 = m.mu30 / (m.m00 * m.m00 * m.m00).sqrt() - 281●
let n21 = m.mu21 / (m.m00 * m.m00 * m.m00).sqrt() - 282●
let n12 = m.mu12 / (m.m00 * m.m00 * m.m00).sqrt() - 283●
let n03 = m.mu03 / (m.m00 * m.m00 * m.m00).sqrt() - 284
hu[0] = n20 + n02 - 285
hu[1] = (n20 - n02) * (n20 - n02) + 4.0 * n11 * n11 - 286
hu[2] = (n30 - 3.0 * n12) * (n30 - 3.0 * n12) + - 287
(3.0 * n21 - n03) * (3.0 * n21 - n03) - 288
hu[3] = (n30 + n12) * (n30 + n12) + (n21 + n03) * (n21 + n03) - 289
hu[4] = (n30 - 3.0 * n12) * - 290
(n30 + n12) * - 291
((n30 + n12) * (n30 + n12) - 3.0 * (n21 + n03) * (n21 + n03)) + - 292
(3.0 * n21 - n03) * - 293
(n21 + n03) * - 294
(3.0 * (n30 + n12) * (n30 + n12) - (n21 + n03) * (n21 + n03)) - 295
hu[5] = (n20 - n02) * ((n30 + n12) * (n30 + n12) - (n21 + n03) * (n21 + n03)) + - 296
4.0 * n11 * (n30 + n12) * (n21 + n03) - 297
hu[6] = (3.0 * n21 - n03) * - 298
(n30 + n12) * - 299
((n30 + n12) * (n30 + n12) - 3.0 * (n21 + n03) * (n21 + n03)) - - 300
(n30 - 3.0 * n12) * - 301
(n21 + n03) * - 302
(3.0 * (n30 + n12) * (n30 + n12) - (n21 + n03) * (n21 + n03)) - 303
hu - 304
} - 305
- 306
///| - 307
/// 最小外接圆。Welzl 算法简化版。 - 308
pub fn min_enclosing_circle( - 309
points : Array[ContourPoint], - 310
) -> (ContourPoint, Float) { - 311
// 空数组返回默认值 - 312●
if points.length() == 0 { - 313●
return (ContourPoint::{ x: 0, y: 0, }, 0.0F) - 314
} - 315
- 316●
let n = points.length() - 317
if n == 1 { - 318●
return (points[0], 0.0F) - 319
} - 320
// 简化:用质心作为圆心,最大距离作为半径 - 321
let mut cx = 0.0F - 322
let mut cy = 0.0F - 323
for p in points { - 324●
cx += Float::from_int(p.x) - 325●
cy += Float::from_int(p.y) - 326
} - 327●
cx = cx / Float::from_int(n) - 328●
cy = cy / Float::from_int(n) - 329
// 迭代优化:移动圆心向最远点 - 330
let mut center_x = cx - 331
let mut center_y = cy - 332
let mut radius = 0.0F - 333
for _iter in 0..<20 { - 334●
let mut far_x = 0 - 335
let mut far_y = 0 - 336
let mut max_dist = 0.0F - 337
for p in points { - 338●
let dx = Float::from_int(p.x) - center_x - 339●
let dy = Float::from_int(p.y) - center_y - 340●
let dist = (dx * dx + dy * dy).sqrt() - 341
if dist > max_dist { - 342●
max_dist = dist - 343
far_x = p.x - 344
far_y = p.y - 345
} - 346
} - 347
radius = max_dist - 348
// 向最远点移动一小步 - 349
let step = 0.1F - 350●
center_x += (Float::from_int(far_x) - center_x) * step - 351●
center_y += (Float::from_int(far_y) - center_y) * step - 352
} - 353●
(ContourPoint::{ x: center_x.to_int(), y: center_y.to_int(), }, radius) - 354
}