image.process\edge\contour.mbt

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