image.process\segment\distance_transform.mbt

81.3% · 65 / 80 points · 274 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/// # 距离变换 distance transform
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 距离变换(纯 MoonBit)
  7. 7
  8. 8///|
  9. 9/// 距离变换。对二值图(非零为前景)计算每个前景像素到最近背景像素的距离。
  10. 10/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4,取首通道)。
  11. 11/// distance_type 为距离度量:1=城市距离(L1), 2=欧氏距离(L2近似), 3=棋盘距离(Linf)。
  12. 12/// 返回 Float 数组,背景像素距离为 0。
  13. 13pub fn distance_transform(
  14. 14 img : @types.Image,
  15. 15 distance_type~ : Int32 = 2,
  16. 16) -> Array[Float] raise @types.LoadError {
  17. 17● if distance_type != 1 && distance_type != 2 && distance_type != 3 {
  18. 18● raise @types.LoadError::DecodeFailed(
  19. 19 "distance_type 必须为 1(L1), 2(L2), 3(Linf)",
  20. 20 )
  21. 21 }
  22. 22 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  23. 23● raise @types.LoadError::DecodeFailed(
  24. 24 "distance_transform 仅支持 channels=1/3/4",
  25. 25 )
  26. 26 }
  27. 27 let w = img.width
  28. 28 let h = img.height
  29. 29 let n = w * h
  30. 30 if n == 0 {
  31. 31○ return []
  32. 32 }
  33. 33 // 转为二值(非零为前景)
  34. 34● let binary : Array[Bool] = Array::make(n, false)
  35. 35 let ch = img.channels
  36. 36 for i in 0..<n {
  37. 37● binary[i] = img.data[i * ch].to_int() > 0
  38. 38 }
  39. 39 // 两遍扫描算法
  40. 40● let dist : Array[Float] = Array::make(n, 10000000000.0F)
  41. 41 // 初始化:背景为 0,前景为无穷大
  42. 42 for i in 0..<n {
  43. 43● if !binary[i] {
  44. 44● dist[i] = 0.0F
  45. 45 }
  46. 46 }
  47. 47 // 距离掩码
  48. 48● let (mask1, mask2) = match distance_type {
  49. 49● 1 => ([(0, -1, 1.0F), (-1, 0, 1.0F)], [(0, 1, 1.0F), (1, 0, 1.0F)])
  50. 50● 3 => ([(0, -1, 1.0F), (-1, 0, 1.0F)], [(0, 1, 1.0F), (1, 0, 1.0F)])
  51. 51 _ =>
  52. 52● (
  53. 53 [(0, -1, 1.0F), (-1, 0, 1.0F), (-1, -1, 1.414F), (-1, 1, 1.414F)],
  54. 54 [(0, 1, 1.0F), (1, 0, 1.0F), (1, 1, 1.414F), (1, -1, 1.414F)],
  55. 55 )
  56. 56 }
  57. 57 // 前向扫描(左上到右下)
  58. 58 // 内部区域:x >= 1 && y >= 1 && y < h - 1
  59. 59 for y in 1..<(h - 1) {
  60. 60● for x in 1..<w {
  61. 61● let idx = y * w + x
  62. 62 if !binary[idx] {
  63. 63● continue
  64. 64 }
  65. 65 for m in mask1 {
  66. 66● let (dx, dy, d) = m
  67. 67 let nd = dist[(y + dy) * w + (x + dx)] + d
  68. 68 if nd < dist[idx] {
  69. 69● dist[idx] = nd
  70. 70 }
  71. 71 }
  72. 72 }
  73. 73 }
  74. 74 // 边界行
  75. 75 for y in 0..<h {
  76. 76● if y >= 1 && y < h - 1 {
  77. 77● continue
  78. 78 }
  79. 79 for x in 0..<w {
  80. 80● let idx = y * w + x
  81. 81 if !binary[idx] {
  82. 82● continue
  83. 83 }
  84. 84 for m in mask1 {
  85. 85● let (dx, dy, d) = m
  86. 86 let nx = x + dx
  87. 87 let ny = y + dy
  88. 88 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  89. 89● let nd = dist[ny * w + nx] + d
  90. 90 if nd < dist[idx] {
  91. 91● dist[idx] = nd
  92. 92 }
  93. 93 }
  94. 94 }
  95. 95 }
  96. 96 }
  97. 97 // 内部行的边界列
  98. 98 for y in 1..<(h - 1) {
  99. 99● let idx = y * w
  100. 100 if binary[idx] {
  101. 101○ for m in mask1 {
  102. 102○ let (dx, dy, d) = m
  103. 103 let nx = dx
  104. 104 let ny = y + dy
  105. 105 if nx >= 0 && ny >= 0 && ny < h {
  106. 106○ let nd = dist[ny * w + nx] + d
  107. 107 if nd < dist[idx] {
  108. 108○ dist[idx] = nd
  109. 109 }
  110. 110 }
  111. 111 }
  112. 112 }
  113. 113 }
  114. 114 // 反向扫描(右下到左上)
  115. 115 // 内部区域:x < w - 1 && y >= 1 && y < h - 1
  116. 116 for y in 1..<(h - 1) {
  117. 117● for x in 0..<(w - 1) {
  118. 118● let idx = y * w + x
  119. 119 if !binary[idx] {
  120. 120● continue
  121. 121 }
  122. 122 for m in mask2 {
  123. 123● let (dx, dy, d) = m
  124. 124 let nd = dist[(y + dy) * w + (x + dx)] + d
  125. 125 if nd < dist[idx] {
  126. 126● dist[idx] = nd
  127. 127 }
  128. 128 }
  129. 129 }
  130. 130 }
  131. 131 // 边界行
  132. 132 for yy in 0..<h {
  133. 133● if yy >= 1 && yy < h - 1 {
  134. 134● continue
  135. 135 }
  136. 136 let y = yy
  137. 137 for xx in 0..<w {
  138. 138● let x = w - 1 - xx
  139. 139 let idx = y * w + x
  140. 140 if !binary[idx] {
  141. 141● continue
  142. 142 }
  143. 143 for m in mask2 {
  144. 144● let (dx, dy, d) = m
  145. 145 let nx = x + dx
  146. 146 let ny = y + dy
  147. 147 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  148. 148● let nd = dist[ny * w + nx] + d
  149. 149 if nd < dist[idx] {
  150. 150● dist[idx] = nd
  151. 151 }
  152. 152 }
  153. 153 }
  154. 154 }
  155. 155 }
  156. 156 // 内部行的边界列
  157. 157 for y in 1..<(h - 1) {
  158. 158● let x = w - 1
  159. 159 let idx = y * w + x
  160. 160 if binary[idx] {
  161. 161○ for m in mask2 {
  162. 162○ let (dx, dy, d) = m
  163. 163 let nx = x + dx
  164. 164 let ny = y + dy
  165. 165 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  166. 166○ let nd = dist[ny * w + nx] + d
  167. 167 if nd < dist[idx] {
  168. 168○ dist[idx] = nd
  169. 169 }
  170. 170 }
  171. 171 }
  172. 172 }
  173. 173 }
  174. 174 dist
  175. 175}
  176. 176
  177. 177///|
  178. 178/// 距离变换可视化。将距离变换结果归一化到 [0, 255] 并返回灰度图。
  179. 179pub fn visualize_distance_transform(
  180. 180 dist : Array[Float],
  181. 181 w : Int32,
  182. 182 h : Int32,
  183. 183) -> @types.Image {
  184. 184● let n = w * h
  185. 185● let out : Array[Byte] = Array::make(n, b'\x00')
  186. 186 if n == 0 {
  187. 187○ return @types.Image::{
  188. 188 width: 0,
  189. 189 height: 0,
  190. 190 channels: 1,
  191. 191○ data: Bytes::from_array([]),
  192. 192 }
  193. 193 }
  194. 194 let mut max_dist = 0.0F
  195. 195 for i in 0..<n {
  196. 196● if dist[i] > max_dist {
  197. 197● max_dist = dist[i]
  198. 198 }
  199. 199 }
  200. 200 if max_dist > 0.0F {
  201. 201● for i in 0..<n {
  202. 202● let val = (dist[i] / max_dist * 255.0F).to_int()
  203. 203● out[i] = val.clamp(min=0, max=255).to_byte()
  204. 204 }
  205. 205 }
  206. 206 @types.Image::{
  207. 207 width: w,
  208. 208 height: h,
  209. 209 channels: 1,
  210. 210● data: Bytes::from_array(out),
  211. 211 }
  212. 212}
  213. 213
  214. 214///|
  215. 215/// 骨架化(基于距离变换)。
  216. 216/// 提取二值图的骨架(中轴线)。
  217. 217/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4,取首通道)。
  218. 218pub fn skeletonize(img : @types.Image) -> @types.Image raise @types.LoadError {
  219. 219● if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  220. 220● raise @types.LoadError::DecodeFailed("skeletonize 仅支持 channels=1/3/4")
  221. 221 }
  222. 222 let w = img.width
  223. 223 let h = img.height
  224. 224 let n = w * h
  225. 225 if n == 0 {
  226. 226○ return img
  227. 227 }
  228. 228 // 计算距离变换
  229. 229● let dist = distance_transform(img, distance_type=2)
  230. 230 // 骨架点:局部最大值
  231. 231 let ch = img.channels
  232. 232● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  233. 233 for y in 1..<(h - 1) {
  234. 234● for x in 1..<(w - 1) {
  235. 235● let idx = y * w + x
  236. 236 if dist[idx] == 0.0F {
  237. 237● continue
  238. 238 }
  239. 239 let mut is_max = true
  240. 240 for dy in -1..<=1 {
  241. 241● for dx in -1..<=1 {
  242. 242● if dx == 0 && dy == 0 {
  243. 243● continue
  244. 244 }
  245. 245 if dist[(y + dy) * w + (x + dx)] > dist[idx] {
  246. 246● is_max = false
  247. 247 break
  248. 248 }
  249. 249 }
  250. 250 if !is_max {
  251. 251● break
  252. 252 }
  253. 253 }
  254. 254 if is_max {
  255. 255● for c in 0..<ch {
  256. 256● if ch == 1 {
  257. 257● out[idx] = b'\xFF'
  258. 258○ } else if c < 3 {
  259. 259○ out[idx * ch + c] = b'\xFF'
  260. 260 }
  261. 261 }
  262. 262 if ch == 4 {
  263. 263○ out[idx * ch + 3] = img.data[idx * ch + 3]
  264. 264 }
  265. 265 }
  266. 266 }
  267. 267 }
  268. 268 @types.Image::{
  269. 269 width: w,
  270. 270 height: h,
  271. 271 channels: ch,
  272. 272● data: Bytes::from_array(out),
  273. 273 }
  274. 274}