image.process\segment\watershed.mbt

86.9% · 73 / 84 points · 258 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/// # 分水岭 watershed
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 分水岭分割(纯 MoonBit)
  7. 7
  8. 8///|
  9. 9/// 分水岭分割。
  10. 10/// 基于沉浸式分水岭算法(Vincent & Soille, 1991)。
  11. 11/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4,自动转灰度)。
  12. 12/// markers 为种子标记图(0=未标记,1..N=不同区域标记)。
  13. 13/// 返回分割结果,每个像素的标签对应其所属区域。
  14. 14pub fn watershed(
  15. 15 img : @types.Image,
  16. 16 markers : Array[Int32],
  17. 17) -> Array[Int32] raise @types.LoadError {
  18. 18● if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  19. 19● raise @types.LoadError::DecodeFailed("watershed 仅支持 channels=1/3/4")
  20. 20 }
  21. 21 let w = img.width
  22. 22 let h = img.height
  23. 23 let n = w * h
  24. 24● if markers.length() != n {
  25. 25● raise @types.LoadError::DecodeFailed(
  26. 26 "markers 长度必须等于 width * height",
  27. 27 )
  28. 28 }
  29. 29 if n == 0 {
  30. 30○ return []
  31. 31 }
  32. 32 // 转灰度
  33. 33● let gray : Array[Int32] = Array::make(n, 0)
  34. 34 let ch = img.channels
  35. 35 for i in 0..<n {
  36. 36● if ch == 1 {
  37. 37● gray[i] = img.data[i].to_int()
  38. 38 } else {
  39. 39○ let r = img.data[i * ch].to_int()
  40. 40○ let g = img.data[i * ch + 1].to_int()
  41. 41○ let b = img.data[i * ch + 2].to_int()
  42. 42 gray[i] = (r * 299 + g * 587 + b * 114) / 1000
  43. 43 }
  44. 44 }
  45. 45 // 复制 markers
  46. 46● let labels : Array[Int32] = Array::make(n, 0)
  47. 47 for i in 0..<n {
  48. 48● labels[i] = markers[i]
  49. 49 }
  50. 50 // 沉浸式分水岭:按灰度值从低到高处理
  51. 51 // 创建像素索引按灰度值排序的列表
  52. 52● let indices : Array[Int32] = Array::make(n, 0)
  53. 53 for i in 0..<n {
  54. 54● indices[i] = i
  55. 55 }
  56. 56 // 按灰度值排序(基数排序,0-255)
  57. 57● let sorted : Array[Array[Int32]] = Array::make(256, [])
  58. 58 for i in 0..<256 {
  59. 59● sorted[i] = []
  60. 60 }
  61. 61 for i in 0..<n {
  62. 62● sorted[gray[i]].push(i)
  63. 63 }
  64. 64 // FIFO 队列
  65. 65 let wshed = -1 // 分水岭线标记
  66. 66 let mut current_label = 0
  67. 67 for i in 0..<n {
  68. 68● if labels[i] > current_label {
  69. 69● current_label = labels[i]
  70. 70 }
  71. 71 }
  72. 72 // 按灰度级别处理
  73. 73 for level in 0..<256 {
  74. 74● let queue : Array[Int32] = []
  75. 75 // 将当前级别的已标记像素加入队列
  76. 76 for idx in sorted[level] {
  77. 77 // 检查邻居是否有已标记的
  78. 78● let x = idx % w
  79. 79 let y = idx / w
  80. 80 let mut has_labeled_neighbor = false
  81. 81 for dy in -1..<=1 {
  82. 82● for dx in -1..<=1 {
  83. 83● if dx == 0 && dy == 0 {
  84. 84● continue
  85. 85 }
  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 nidx = ny * w + nx
  90. 90 if labels[nidx] > 0 || labels[nidx] == wshed {
  91. 91● has_labeled_neighbor = true
  92. 92 break
  93. 93 }
  94. 94 }
  95. 95 }
  96. 96 if has_labeled_neighbor {
  97. 97● break
  98. 98 }
  99. 99 }
  100. 100 if has_labeled_neighbor {
  101. 101● queue.push(idx)
  102. 102 }
  103. 103 }
  104. 104 // BFS 扩展(head 指针队列,O(1) dequeue 避免 remove(0) 的 O(n) 移压)
  105. 105 let mut q_head = 0
  106. 106● while q_head < queue.length() {
  107. 107● let idx = queue[q_head]
  108. 108 q_head += 1
  109. 109 let x = idx % w
  110. 110 let y = idx / w
  111. 111 let mut label = 0
  112. 112 let mut is_wshed = false
  113. 113 // 查看邻居标签
  114. 114 for dy in -1..<=1 {
  115. 115● for dx in -1..<=1 {
  116. 116● if dx == 0 && dy == 0 {
  117. 117● continue
  118. 118 }
  119. 119 let nx = x + dx
  120. 120 let ny = y + dy
  121. 121 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  122. 122● let nidx = ny * w + nx
  123. 123 let nl = labels[nidx]
  124. 124 if nl > 0 {
  125. 125● if label == 0 {
  126. 126● label = nl
  127. 127● } else if label != nl {
  128. 128● is_wshed = true
  129. 129 }
  130. 130 }
  131. 131 }
  132. 132 }
  133. 133 }
  134. 134 if is_wshed {
  135. 135● labels[idx] = wshed
  136. 136● } else if label > 0 {
  137. 137● labels[idx] = label
  138. 138 // 将同级别未标记邻居加入队列
  139. 139 for dy in -1..<=1 {
  140. 140● for dx in -1..<=1 {
  141. 141● if dx == 0 && dy == 0 {
  142. 142● continue
  143. 143 }
  144. 144 let nx = x + dx
  145. 145 let ny = y + dy
  146. 146 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  147. 147● let nidx = ny * w + nx
  148. 148 if labels[nidx] == 0 && gray[nidx] == level {
  149. 149● labels[nidx] = -2 // 临时标记,在队列中
  150. 150● queue.push(nidx)
  151. 151 }
  152. 152 }
  153. 153 }
  154. 154 }
  155. 155 }
  156. 156 }
  157. 157 // 清除临时标记
  158. 158 for idx in sorted[level] {
  159. 159● if labels[idx] == -2 {
  160. 160○ labels[idx] = 0
  161. 161 }
  162. 162 }
  163. 163 }
  164. 164 labels
  165. 165}
  166. 166
  167. 167///|
  168. 168/// 自动分水岭分割(使用局部最小值作为种子点)。
  169. 169/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4,自动转灰度)。
  170. 170/// 返回标签图和区域数量。
  171. 171pub fn watershed_auto(
  172. 172 img : @types.Image,
  173. 173) -> (Array[Int32], Int32) raise @types.LoadError {
  174. 174● if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  175. 175● raise @types.LoadError::DecodeFailed(
  176. 176 "watershed_auto 仅支持 channels=1/3/4",
  177. 177 )
  178. 178 }
  179. 179 let w = img.width
  180. 180 let h = img.height
  181. 181 let n = w * h
  182. 182 if n == 0 {
  183. 183○ return ([], 0)
  184. 184 }
  185. 185 // 转灰度
  186. 186● let gray : Array[Int32] = Array::make(n, 0)
  187. 187 let ch = img.channels
  188. 188 for i in 0..<n {
  189. 189● if ch == 1 {
  190. 190● gray[i] = img.data[i].to_int()
  191. 191 } else {
  192. 192○ let r = img.data[i * ch].to_int()
  193. 193○ let g = img.data[i * ch + 1].to_int()
  194. 194○ let b = img.data[i * ch + 2].to_int()
  195. 195 gray[i] = (r * 299 + g * 587 + b * 114) / 1000
  196. 196 }
  197. 197 }
  198. 198 // 寻找局部最小值作为种子点
  199. 199● let markers : Array[Int32] = Array::make(n, 0)
  200. 200 let mut current_label = 0
  201. 201 for y in 0..<h {
  202. 202● for x in 0..<w {
  203. 203● let idx = y * w + x
  204. 204 let val = gray[idx]
  205. 205 let mut is_min = true
  206. 206 for dy in -1..<=1 {
  207. 207● for dx in -1..<=1 {
  208. 208● if dx == 0 && dy == 0 {
  209. 209● continue
  210. 210 }
  211. 211 let nx = x + dx
  212. 212 let ny = y + dy
  213. 213 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  214. 214● let nidx = ny * w + nx
  215. 215 if gray[nidx] < val {
  216. 216● is_min = false
  217. 217 break
  218. 218 }
  219. 219 }
  220. 220 }
  221. 221 if !is_min {
  222. 222● break
  223. 223 }
  224. 224 }
  225. 225 if is_min && markers[idx] == 0 {
  226. 226● current_label += 1
  227. 227 // BFS 标记连通的等值像素(head 指针队列,避免 remove 开销)
  228. 228 let stack : Array[Int32] = [idx]
  229. 229 let mut sp = 0
  230. 230 markers[idx] = current_label
  231. 231● while sp < stack.length() {
  232. 232● let pidx = stack[sp]
  233. 233 sp += 1
  234. 234 let px = pidx % w
  235. 235 let py = pidx / w
  236. 236 for dy in -1..<=1 {
  237. 237● for dx in -1..<=1 {
  238. 238● if dx == 0 && dy == 0 {
  239. 239● continue
  240. 240 }
  241. 241 let nx = px + dx
  242. 242 let ny = py + dy
  243. 243 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  244. 244● let nidx = ny * w + nx
  245. 245 if markers[nidx] == 0 && gray[nidx] == val {
  246. 246● markers[nidx] = current_label
  247. 247● stack.push(nidx)
  248. 248 }
  249. 249 }
  250. 250 }
  251. 251 }
  252. 252 }
  253. 253 }
  254. 254 }
  255. 255 }
  256. 256● let labels = watershed(img, markers)
  257. 257 (labels, current_label)
  258. 258}