image.process\segment\morphology.mbt

97.9% · 140 / 143 points · 396 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/// # 形态学 morphology
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 形态学操作(纯 MoonBit):erode / dilate / morph_open / morph_close
  7. 7// 使用 3x3 结构元素(八邻域)
  8. 8
  9. 9///|
  10. 10/// 腐蚀。每个像素取 3x3 邻域最小值。分离内部/边界区域以消除热点 clamp 调用。
  11. 11pub fn erode(img : @types.Image) -> @types.Image {
  12. 12● let ch = img.channels
  13. 13 let w = img.width
  14. 14 let h = img.height
  15. 15 let n = w * h
  16. 16● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  17. 17 // 预计算像素 Int 数组,避免内层循环重复类型转换
  18. 18● let src : Array[Int32] = Array::make(n * ch, 0)
  19. 19 for i in 0..<(n * ch) {
  20. 20● src[i] = img.data[i].to_int()
  21. 21 }
  22. 22 // 内部区域:x in [1, w-1), y in [1, h-1),3x3 核完全在图像内
  23. 23 for y in 1..<(h - 1) {
  24. 24● for x in 1..<(w - 1) {
  25. 25● for c in 0..<ch {
  26. 26● let base = ((y - 1) * w + (x - 1)) * ch + c
  27. 27 let mut min_v = src[base]
  28. 28 if src[base + ch] < min_v {
  29. 29● min_v = src[base + ch]
  30. 30 }
  31. 31 if src[base + 2 * ch] < min_v {
  32. 32● min_v = src[base + 2 * ch]
  33. 33 }
  34. 34 let row1 = base + w * ch
  35. 35 if src[row1] < min_v {
  36. 36● min_v = src[row1]
  37. 37 }
  38. 38 if src[row1 + ch] < min_v {
  39. 39● min_v = src[row1 + ch]
  40. 40 }
  41. 41 if src[row1 + 2 * ch] < min_v {
  42. 42● min_v = src[row1 + 2 * ch]
  43. 43 }
  44. 44 let row2 = base + 2 * w * ch
  45. 45 if src[row2] < min_v {
  46. 46● min_v = src[row2]
  47. 47 }
  48. 48 if src[row2 + ch] < min_v {
  49. 49● min_v = src[row2 + ch]
  50. 50 }
  51. 51 if src[row2 + 2 * ch] < min_v {
  52. 52● min_v = src[row2 + 2 * ch]
  53. 53 }
  54. 54● out[(y * w + x) * ch + c] = min_v.to_byte()
  55. 55 }
  56. 56 }
  57. 57 }
  58. 58 // 边界区域:需要 clamp
  59. 59 for y in 0..<h {
  60. 60● let y_inner = y >= 1 && y < h - 1
  61. 61 for x in 0..<w {
  62. 62● if y_inner && x >= 1 && x < w - 1 {
  63. 63● continue
  64. 64 }
  65. 65 for c in 0..<ch {
  66. 66● let mut min_v = 255
  67. 67 for dy in -1..<=1 {
  68. 68● for dx in -1..<=1 {
  69. 69● let sx = @types.clamp_int(x + dx, 0, w - 1)
  70. 70● let sy = @types.clamp_int(y + dy, 0, h - 1)
  71. 71 let v = src[(sy * w + sx) * ch + c]
  72. 72 if v < min_v {
  73. 73● min_v = v
  74. 74 }
  75. 75 }
  76. 76 }
  77. 77● out[(y * w + x) * ch + c] = min_v.to_byte()
  78. 78 }
  79. 79 }
  80. 80 }
  81. 81 @types.Image::{
  82. 82 width: w,
  83. 83 height: h,
  84. 84 channels: ch,
  85. 85● data: Bytes::from_array(out),
  86. 86 }
  87. 87}
  88. 88
  89. 89///|
  90. 90/// 膨胀。每个像素取 3x3 邻域最大值。分离内部/边界区域以消除热点 clamp 调用。
  91. 91pub fn dilate(img : @types.Image) -> @types.Image {
  92. 92● let ch = img.channels
  93. 93 let w = img.width
  94. 94 let h = img.height
  95. 95 let n = w * h
  96. 96● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  97. 97 // 预计算像素 Int 数组
  98. 98● let src : Array[Int32] = Array::make(n * ch, 0)
  99. 99 for i in 0..<(n * ch) {
  100. 100● src[i] = img.data[i].to_int()
  101. 101 }
  102. 102 // 内部区域:x in [1, w-1), y in [1, h-1)
  103. 103 for y in 1..<(h - 1) {
  104. 104● for x in 1..<(w - 1) {
  105. 105● for c in 0..<ch {
  106. 106● let base = ((y - 1) * w + (x - 1)) * ch + c
  107. 107 let mut max_v = src[base]
  108. 108 if src[base + ch] > max_v {
  109. 109● max_v = src[base + ch]
  110. 110 }
  111. 111 if src[base + 2 * ch] > max_v {
  112. 112● max_v = src[base + 2 * ch]
  113. 113 }
  114. 114 let row1 = base + w * ch
  115. 115 if src[row1] > max_v {
  116. 116● max_v = src[row1]
  117. 117 }
  118. 118 if src[row1 + ch] > max_v {
  119. 119● max_v = src[row1 + ch]
  120. 120 }
  121. 121 if src[row1 + 2 * ch] > max_v {
  122. 122● max_v = src[row1 + 2 * ch]
  123. 123 }
  124. 124 let row2 = base + 2 * w * ch
  125. 125 if src[row2] > max_v {
  126. 126● max_v = src[row2]
  127. 127 }
  128. 128 if src[row2 + ch] > max_v {
  129. 129● max_v = src[row2 + ch]
  130. 130 }
  131. 131 if src[row2 + 2 * ch] > max_v {
  132. 132● max_v = src[row2 + 2 * ch]
  133. 133 }
  134. 134● out[(y * w + x) * ch + c] = max_v.to_byte()
  135. 135 }
  136. 136 }
  137. 137 }
  138. 138 // 边界区域:需要 clamp
  139. 139 for y in 0..<h {
  140. 140● let y_inner = y >= 1 && y < h - 1
  141. 141 for x in 0..<w {
  142. 142● if y_inner && x >= 1 && x < w - 1 {
  143. 143● continue
  144. 144 }
  145. 145 for c in 0..<ch {
  146. 146● let mut max_v = 0
  147. 147 for dy in -1..<=1 {
  148. 148● for dx in -1..<=1 {
  149. 149● let sx = @types.clamp_int(x + dx, 0, w - 1)
  150. 150● let sy = @types.clamp_int(y + dy, 0, h - 1)
  151. 151 let v = src[(sy * w + sx) * ch + c]
  152. 152 if v > max_v {
  153. 153● max_v = v
  154. 154 }
  155. 155 }
  156. 156 }
  157. 157● out[(y * w + x) * ch + c] = max_v.to_byte()
  158. 158 }
  159. 159 }
  160. 160 }
  161. 161 @types.Image::{
  162. 162 width: w,
  163. 163 height: h,
  164. 164 channels: ch,
  165. 165● data: Bytes::from_array(out),
  166. 166 }
  167. 167}
  168. 168
  169. 169///|
  170. 170/// 开运算(先腐蚀后膨胀)。去除小的明亮噪点。
  171. 171pub fn morph_open(img : @types.Image) -> @types.Image {
  172. 172● dilate(erode(img))
  173. 173}
  174. 174
  175. 175///|
  176. 176/// 闭运算(先膨胀后腐蚀)。填充小的暗洞。
  177. 177pub fn morph_close(img : @types.Image) -> @types.Image {
  178. 178● erode(dilate(img))
  179. 179}
  180. 180
  181. 181///|
  182. 182/// 形态学梯度(dilate - erode)。提取边缘轮廓。
  183. 183pub fn morph_gradient(img : @types.Image) -> @types.Image {
  184. 184● let dilated = dilate(img)
  185. 185● let eroded = erode(img)
  186. 186 let ch = img.channels
  187. 187 let n = img.width * img.height * ch
  188. 188● let out : Array[Byte] = Array::make(n, b'\x00')
  189. 189 for i in 0..<n {
  190. 190● let v = dilated.data[i].to_int() - eroded.data[i].to_int()
  191. 191◐ out[i] = if v < 0 { b'\x00' } else { v.to_byte() }
  192. 192 }
  193. 193 @types.Image::{
  194. 194 width: img.width,
  195. 195 height: img.height,
  196. 196 channels: ch,
  197. 197● data: Bytes::from_array(out),
  198. 198 }
  199. 199}
  200. 200
  201. 201///|
  202. 202/// 顶帽变换(original - open)。提取比结构元素小的明亮细节。
  203. 203pub fn morph_tophat(img : @types.Image) -> @types.Image {
  204. 204● let opened = morph_open(img)
  205. 205 let ch = img.channels
  206. 206 let n = img.width * img.height * ch
  207. 207● let out : Array[Byte] = Array::make(n, b'\x00')
  208. 208 for i in 0..<n {
  209. 209● let v = img.data[i].to_int() - opened.data[i].to_int()
  210. 210◐ out[i] = if v < 0 { b'\x00' } else { v.to_byte() }
  211. 211 }
  212. 212 @types.Image::{
  213. 213 width: img.width,
  214. 214 height: img.height,
  215. 215 channels: ch,
  216. 216● data: Bytes::from_array(out),
  217. 217 }
  218. 218}
  219. 219
  220. 220///|
  221. 221/// 黑帽变换(close - original)。提取比结构元素小的暗细节。
  222. 222pub fn morph_blackhat(img : @types.Image) -> @types.Image {
  223. 223● let closed = morph_close(img)
  224. 224 let ch = img.channels
  225. 225 let n = img.width * img.height * ch
  226. 226● let out : Array[Byte] = Array::make(n, b'\x00')
  227. 227 for i in 0..<n {
  228. 228● let v = closed.data[i].to_int() - img.data[i].to_int()
  229. 229◐ out[i] = if v < 0 { b'\x00' } else { v.to_byte() }
  230. 230 }
  231. 231 @types.Image::{
  232. 232 width: img.width,
  233. 233 height: img.height,
  234. 234 channels: ch,
  235. 235● data: Bytes::from_array(out),
  236. 236 }
  237. 237}
  238. 238
  239. 239///|
  240. 240/// 结构元素(核)。二维布尔数组,true 表示该位置属于结构元素。
  241. 241pub(all) struct StructuringElement {
  242. 242 width : Int32
  243. 243 height : Int32
  244. 244 data : Array[Bool] // height * width
  245. 245}
  246. 246
  247. 247///|
  248. 248/// 创建矩形结构元素(全为 true)。
  249. 249pub fn se_rect(width : Int32, height : Int32) -> StructuringElement {
  250. 250● StructuringElement::{
  251. 251 width,
  252. 252 height,
  253. 253● data: Array::make(width * height, true),
  254. 254 }
  255. 255}
  256. 256
  257. 257///|
  258. 258/// 创建十字形结构元素(中心行+中心列)。
  259. 259pub fn se_cross(size : Int32) -> StructuringElement {
  260. 260● let data : Array[Bool] = Array::make(size * size, false)
  261. 261 let cx = size / 2
  262. 262 for i in 0..<size {
  263. 263● data[cx * size + i] = true
  264. 264 data[i * size + cx] = true
  265. 265 }
  266. 266 StructuringElement::{ width: size, height: size, data, }
  267. 267}
  268. 268
  269. 269///|
  270. 270/// 创建椭圆形结构元素。对于 size×size 的核,包含所有满足
  271. 271/// (dx/rx)² + (dy/ry)² ≤ 1 的位置。
  272. 272pub fn se_ellipse(size : Int32) -> StructuringElement {
  273. 273● let data : Array[Bool] = Array::make(size * size, false)
  274. 274 let cx = (size - 1) / 2
  275. 275 let cy = (size - 1) / 2
  276. 276● let rx = Float::from_int(cx) + 0.5F
  277. 277● let ry = Float::from_int(cy) + 0.5F
  278. 278 for y in 0..<size {
  279. 279● for x in 0..<size {
  280. 280● let dx = (Float::from_int(x) - Float::from_int(cx)) / rx
  281. 281● let dy = (Float::from_int(y) - Float::from_int(cy)) / ry
  282. 282 if dx * dx + dy * dy <= 1.0F {
  283. 283● data[y * size + x] = true
  284. 284 }
  285. 285 }
  286. 286 }
  287. 287 StructuringElement::{ width: size, height: size, data, }
  288. 288}
  289. 289
  290. 290///|
  291. 291/// 使用自定义结构元素进行腐蚀。
  292. 292pub fn erode_custom(
  293. 293 img : @types.Image,
  294. 294 se : StructuringElement,
  295. 295) -> @types.Image {
  296. 296● let ch = img.channels
  297. 297 let w = img.width
  298. 298 let h = img.height
  299. 299 let n = w * h
  300. 300● let src : Array[Int32] = Array::make(n * ch, 0)
  301. 301 for i in 0..<(n * ch) {
  302. 302● src[i] = img.data[i].to_int()
  303. 303 }
  304. 304● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  305. 305 let cx = se.width / 2
  306. 306 let cy = se.height / 2
  307. 307 for y in 0..<h {
  308. 308● for x in 0..<w {
  309. 309● for c in 0..<ch {
  310. 310● let mut min_v = 255
  311. 311 for sy in 0..<se.height {
  312. 312● for sx in 0..<se.width {
  313. 313● if se.data[sy * se.width + sx] {
  314. 314● let nx = @types.clamp_int(x + sx - cx, 0, w - 1)
  315. 315● let ny = @types.clamp_int(y + sy - cy, 0, h - 1)
  316. 316 let v = src[(ny * w + nx) * ch + c]
  317. 317 if v < min_v {
  318. 318● min_v = v
  319. 319 }
  320. 320 }
  321. 321 }
  322. 322 }
  323. 323● out[(y * w + x) * ch + c] = min_v.to_byte()
  324. 324 }
  325. 325 }
  326. 326 }
  327. 327 @types.Image::{
  328. 328 width: w,
  329. 329 height: h,
  330. 330 channels: ch,
  331. 331● data: Bytes::from_array(out),
  332. 332 }
  333. 333}
  334. 334
  335. 335///|
  336. 336/// 使用自定义结构元素进行膨胀。
  337. 337pub fn dilate_custom(
  338. 338 img : @types.Image,
  339. 339 se : StructuringElement,
  340. 340) -> @types.Image {
  341. 341● let ch = img.channels
  342. 342 let w = img.width
  343. 343 let h = img.height
  344. 344 let n = w * h
  345. 345● let src : Array[Int32] = Array::make(n * ch, 0)
  346. 346 for i in 0..<(n * ch) {
  347. 347● src[i] = img.data[i].to_int()
  348. 348 }
  349. 349● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  350. 350 let cx = se.width / 2
  351. 351 let cy = se.height / 2
  352. 352 for y in 0..<h {
  353. 353● for x in 0..<w {
  354. 354● for c in 0..<ch {
  355. 355● let mut max_v = 0
  356. 356 for sy in 0..<se.height {
  357. 357● for sx in 0..<se.width {
  358. 358● if se.data[sy * se.width + sx] {
  359. 359● let nx = @types.clamp_int(x + sx - cx, 0, w - 1)
  360. 360● let ny = @types.clamp_int(y + sy - cy, 0, h - 1)
  361. 361 let v = src[(ny * w + nx) * ch + c]
  362. 362 if v > max_v {
  363. 363● max_v = v
  364. 364 }
  365. 365 }
  366. 366 }
  367. 367 }
  368. 368● out[(y * w + x) * ch + c] = max_v.to_byte()
  369. 369 }
  370. 370 }
  371. 371 }
  372. 372 @types.Image::{
  373. 373 width: w,
  374. 374 height: h,
  375. 375 channels: ch,
  376. 376● data: Bytes::from_array(out),
  377. 377 }
  378. 378}
  379. 379
  380. 380///|
  381. 381/// 使用自定义结构元素进行开运算。
  382. 382pub fn morph_open_custom(
  383. 383 img : @types.Image,
  384. 384 se : StructuringElement,
  385. 385) -> @types.Image {
  386. 386● dilate_custom(erode_custom(img, se), se)
  387. 387}
  388. 388
  389. 389///|
  390. 390/// 使用自定义结构元素进行闭运算。
  391. 391pub fn morph_close_custom(
  392. 392 img : @types.Image,
  393. 393 se : StructuringElement,
  394. 394) -> @types.Image {
  395. 395● erode_custom(dilate_custom(img, se), se)
  396. 396}