image.process\edge\contour_analysis.mbt

90.6% · 87 / 96 points · 354 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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