image.process\segment\connected_components.mbt

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