image.process\segment\morphology_16f.mbt

96.3% · 262 / 272 points · 765 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/// # 形态学 16f morphology 16f
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 形态学操作 16-bit/float 变体
  7. 7// 支持 Image16 / ImageF 两种类型
  8. 8// StructuringElement 复用 morphology.mbt 中的定义
  9. 9
  10. 10// 复用 color_16f.mbt 中的 read_u16_le_m / write_u16_le_m / write_f32_le_m
  11. 11// 复用 tonemap.mbt 中的 read_float_le_m
  12. 12
  13. 13// ========== 基础操作:erode / dilate ==========
  14. 14
  15. 15///|
  16. 16/// 16-bit 腐蚀。每个像素取 3x3 邻域最小值。
  17. 17/// @see erode 8-bit 版本,本函数为 Image16 变体
  18. 18pub fn erode_16(img : @types.Image16) -> @types.Image16 {
  19. 19● let ch = img.channels
  20. 20 let w = img.width
  21. 21 let h = img.height
  22. 22 let n = w * h
  23. 23 let bpp = ch * 2
  24. 24● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  25. 25● let src : Array[Int32] = Array::make(n * ch, 0)
  26. 26 for i in 0..<(n * ch) {
  27. 27● src[i] = read_u16_le_m(img.data, i * 2)
  28. 28 }
  29. 29 // 内部区域
  30. 30 for y in 1..<(h - 1) {
  31. 31● for x in 1..<(w - 1) {
  32. 32● for c in 0..<ch {
  33. 33● let base = ((y - 1) * w + (x - 1)) * ch + c
  34. 34 let mut min_v = src[base]
  35. 35 if src[base + ch] < min_v {
  36. 36● min_v = src[base + ch]
  37. 37 }
  38. 38 if src[base + 2 * ch] < min_v {
  39. 39● min_v = src[base + 2 * ch]
  40. 40 }
  41. 41 let row1 = base + w * ch
  42. 42 if src[row1] < min_v {
  43. 43● min_v = src[row1]
  44. 44 }
  45. 45 if src[row1 + ch] < min_v {
  46. 46● min_v = src[row1 + ch]
  47. 47 }
  48. 48 if src[row1 + 2 * ch] < min_v {
  49. 49● min_v = src[row1 + 2 * ch]
  50. 50 }
  51. 51 let row2 = base + 2 * w * ch
  52. 52 if src[row2] < min_v {
  53. 53● min_v = src[row2]
  54. 54 }
  55. 55 if src[row2 + ch] < min_v {
  56. 56● min_v = src[row2 + ch]
  57. 57 }
  58. 58 if src[row2 + 2 * ch] < min_v {
  59. 59● min_v = src[row2 + 2 * ch]
  60. 60 }
  61. 61● write_u16_le_m(out, (y * w + x) * bpp + c * 2, min_v)
  62. 62 }
  63. 63 }
  64. 64 }
  65. 65 // 边界区域
  66. 66 for y in 0..<h {
  67. 67● let y_inner = y >= 1 && y < h - 1
  68. 68 for x in 0..<w {
  69. 69● if y_inner && x >= 1 && x < w - 1 {
  70. 70● continue
  71. 71 }
  72. 72 for c in 0..<ch {
  73. 73● let mut min_v = 65535
  74. 74 for dy in -1..<=1 {
  75. 75● for dx in -1..<=1 {
  76. 76● let sx = @types.clamp_int(x + dx, 0, w - 1)
  77. 77● let sy = @types.clamp_int(y + dy, 0, h - 1)
  78. 78 let v = src[(sy * w + sx) * ch + c]
  79. 79 if v < min_v {
  80. 80● min_v = v
  81. 81 }
  82. 82 }
  83. 83 }
  84. 84● write_u16_le_m(out, (y * w + x) * bpp + c * 2, min_v)
  85. 85 }
  86. 86 }
  87. 87 }
  88. 88 @types.Image16::{
  89. 89 width: w,
  90. 90 height: h,
  91. 91 channels: ch,
  92. 92● data: Bytes::from_array(out),
  93. 93 }
  94. 94}
  95. 95
  96. 96///|
  97. 97/// float 腐蚀。每个像素取 3x3 邻域最小值。
  98. 98/// @see erode 8-bit 版本,本函数为 ImageF 变体
  99. 99pub fn erode_f(img : @types.ImageF) -> @types.ImageF {
  100. 100● let ch = img.channels
  101. 101 let w = img.width
  102. 102 let h = img.height
  103. 103 let n = w * h
  104. 104 let bpp = ch * 4
  105. 105● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  106. 106● let src : Array[Float] = Array::make(n * ch, 0.0F)
  107. 107 for i in 0..<(n * ch) {
  108. 108● src[i] = read_float_le_m(img.data, i * 4)
  109. 109 }
  110. 110 for y in 1..<(h - 1) {
  111. 111● for x in 1..<(w - 1) {
  112. 112● for c in 0..<ch {
  113. 113● let base = ((y - 1) * w + (x - 1)) * ch + c
  114. 114 let mut min_v = src[base]
  115. 115 if src[base + ch] < min_v {
  116. 116● min_v = src[base + ch]
  117. 117 }
  118. 118 if src[base + 2 * ch] < min_v {
  119. 119● min_v = src[base + 2 * ch]
  120. 120 }
  121. 121 let row1 = base + w * ch
  122. 122 if src[row1] < min_v {
  123. 123● min_v = src[row1]
  124. 124 }
  125. 125 if src[row1 + ch] < min_v {
  126. 126● min_v = src[row1 + ch]
  127. 127 }
  128. 128 if src[row1 + 2 * ch] < min_v {
  129. 129● min_v = src[row1 + 2 * ch]
  130. 130 }
  131. 131 let row2 = base + 2 * w * ch
  132. 132 if src[row2] < min_v {
  133. 133● min_v = src[row2]
  134. 134 }
  135. 135 if src[row2 + ch] < min_v {
  136. 136● min_v = src[row2 + ch]
  137. 137 }
  138. 138 if src[row2 + 2 * ch] < min_v {
  139. 139● min_v = src[row2 + 2 * ch]
  140. 140 }
  141. 141● write_f32_le_m(out, (y * w + x) * bpp + c * 4, min_v)
  142. 142 }
  143. 143 }
  144. 144 }
  145. 145 for y in 0..<h {
  146. 146● let y_inner = y >= 1 && y < h - 1
  147. 147 for x in 0..<w {
  148. 148● if y_inner && x >= 1 && x < w - 1 {
  149. 149● continue
  150. 150 }
  151. 151 for c in 0..<ch {
  152. 152● let mut min_v = 1.0F
  153. 153 for dy in -1..<=1 {
  154. 154● for dx in -1..<=1 {
  155. 155● let sx = @types.clamp_int(x + dx, 0, w - 1)
  156. 156● let sy = @types.clamp_int(y + dy, 0, h - 1)
  157. 157 let v = src[(sy * w + sx) * ch + c]
  158. 158 if v < min_v {
  159. 159● min_v = v
  160. 160 }
  161. 161 }
  162. 162 }
  163. 163● write_f32_le_m(out, (y * w + x) * bpp + c * 4, min_v)
  164. 164 }
  165. 165 }
  166. 166 }
  167. 167 @types.ImageF::{
  168. 168 width: w,
  169. 169 height: h,
  170. 170 channels: ch,
  171. 171● data: Bytes::from_array(out),
  172. 172 }
  173. 173}
  174. 174
  175. 175///|
  176. 176/// 16-bit 膨胀。每个像素取 3x3 邻域最大值。
  177. 177/// @see dilate 8-bit 版本,本函数为 Image16 变体
  178. 178pub fn dilate_16(img : @types.Image16) -> @types.Image16 {
  179. 179● let ch = img.channels
  180. 180 let w = img.width
  181. 181 let h = img.height
  182. 182 let n = w * h
  183. 183 let bpp = ch * 2
  184. 184● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  185. 185● let src : Array[Int32] = Array::make(n * ch, 0)
  186. 186 for i in 0..<(n * ch) {
  187. 187● src[i] = read_u16_le_m(img.data, i * 2)
  188. 188 }
  189. 189 for y in 1..<(h - 1) {
  190. 190● for x in 1..<(w - 1) {
  191. 191● for c in 0..<ch {
  192. 192● let base = ((y - 1) * w + (x - 1)) * ch + c
  193. 193 let mut max_v = src[base]
  194. 194 if src[base + ch] > max_v {
  195. 195● max_v = src[base + ch]
  196. 196 }
  197. 197 if src[base + 2 * ch] > max_v {
  198. 198● max_v = src[base + 2 * ch]
  199. 199 }
  200. 200 let row1 = base + w * ch
  201. 201 if src[row1] > max_v {
  202. 202● max_v = src[row1]
  203. 203 }
  204. 204 if src[row1 + ch] > max_v {
  205. 205● max_v = src[row1 + ch]
  206. 206 }
  207. 207 if src[row1 + 2 * ch] > max_v {
  208. 208● max_v = src[row1 + 2 * ch]
  209. 209 }
  210. 210 let row2 = base + 2 * w * ch
  211. 211 if src[row2] > max_v {
  212. 212● max_v = src[row2]
  213. 213 }
  214. 214 if src[row2 + ch] > max_v {
  215. 215● max_v = src[row2 + ch]
  216. 216 }
  217. 217 if src[row2 + 2 * ch] > max_v {
  218. 218● max_v = src[row2 + 2 * ch]
  219. 219 }
  220. 220● write_u16_le_m(out, (y * w + x) * bpp + c * 2, max_v)
  221. 221 }
  222. 222 }
  223. 223 }
  224. 224 for y in 0..<h {
  225. 225● let y_inner = y >= 1 && y < h - 1
  226. 226 for x in 0..<w {
  227. 227● if y_inner && x >= 1 && x < w - 1 {
  228. 228● continue
  229. 229 }
  230. 230 for c in 0..<ch {
  231. 231● let mut max_v = 0
  232. 232 for dy in -1..<=1 {
  233. 233● for dx in -1..<=1 {
  234. 234● let sx = @types.clamp_int(x + dx, 0, w - 1)
  235. 235● let sy = @types.clamp_int(y + dy, 0, h - 1)
  236. 236 let v = src[(sy * w + sx) * ch + c]
  237. 237 if v > max_v {
  238. 238● max_v = v
  239. 239 }
  240. 240 }
  241. 241 }
  242. 242● write_u16_le_m(out, (y * w + x) * bpp + c * 2, max_v)
  243. 243 }
  244. 244 }
  245. 245 }
  246. 246 @types.Image16::{
  247. 247 width: w,
  248. 248 height: h,
  249. 249 channels: ch,
  250. 250● data: Bytes::from_array(out),
  251. 251 }
  252. 252}
  253. 253
  254. 254///|
  255. 255/// float 膨胀。每个像素取 3x3 邻域最大值。
  256. 256/// @see dilate 8-bit 版本,本函数为 ImageF 变体
  257. 257pub fn dilate_f(img : @types.ImageF) -> @types.ImageF {
  258. 258● let ch = img.channels
  259. 259 let w = img.width
  260. 260 let h = img.height
  261. 261 let n = w * h
  262. 262 let bpp = ch * 4
  263. 263● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  264. 264● let src : Array[Float] = Array::make(n * ch, 0.0F)
  265. 265 for i in 0..<(n * ch) {
  266. 266● src[i] = read_float_le_m(img.data, i * 4)
  267. 267 }
  268. 268 for y in 1..<(h - 1) {
  269. 269● for x in 1..<(w - 1) {
  270. 270● for c in 0..<ch {
  271. 271● let base = ((y - 1) * w + (x - 1)) * ch + c
  272. 272 let mut max_v = src[base]
  273. 273 if src[base + ch] > max_v {
  274. 274● max_v = src[base + ch]
  275. 275 }
  276. 276 if src[base + 2 * ch] > max_v {
  277. 277● max_v = src[base + 2 * ch]
  278. 278 }
  279. 279 let row1 = base + w * ch
  280. 280 if src[row1] > max_v {
  281. 281● max_v = src[row1]
  282. 282 }
  283. 283 if src[row1 + ch] > max_v {
  284. 284● max_v = src[row1 + ch]
  285. 285 }
  286. 286 if src[row1 + 2 * ch] > max_v {
  287. 287● max_v = src[row1 + 2 * ch]
  288. 288 }
  289. 289 let row2 = base + 2 * w * ch
  290. 290 if src[row2] > max_v {
  291. 291● max_v = src[row2]
  292. 292 }
  293. 293 if src[row2 + ch] > max_v {
  294. 294● max_v = src[row2 + ch]
  295. 295 }
  296. 296 if src[row2 + 2 * ch] > max_v {
  297. 297● max_v = src[row2 + 2 * ch]
  298. 298 }
  299. 299● write_f32_le_m(out, (y * w + x) * bpp + c * 4, max_v)
  300. 300 }
  301. 301 }
  302. 302 }
  303. 303 for y in 0..<h {
  304. 304● let y_inner = y >= 1 && y < h - 1
  305. 305 for x in 0..<w {
  306. 306● if y_inner && x >= 1 && x < w - 1 {
  307. 307● continue
  308. 308 }
  309. 309 for c in 0..<ch {
  310. 310● let mut max_v = 0.0F
  311. 311 for dy in -1..<=1 {
  312. 312● for dx in -1..<=1 {
  313. 313● let sx = @types.clamp_int(x + dx, 0, w - 1)
  314. 314● let sy = @types.clamp_int(y + dy, 0, h - 1)
  315. 315 let v = src[(sy * w + sx) * ch + c]
  316. 316 if v > max_v {
  317. 317● max_v = v
  318. 318 }
  319. 319 }
  320. 320 }
  321. 321● write_f32_le_m(out, (y * w + x) * bpp + c * 4, max_v)
  322. 322 }
  323. 323 }
  324. 324 }
  325. 325 @types.ImageF::{
  326. 326 width: w,
  327. 327 height: h,
  328. 328 channels: ch,
  329. 329● data: Bytes::from_array(out),
  330. 330 }
  331. 331}
  332. 332
  333. 333// ========== 组合操作 ==========
  334. 334
  335. 335///|
  336. 336/// 16-bit 开运算(先腐蚀后膨胀)。
  337. 337/// @see morph_open 8-bit 版本,本函数为 Image16 变体
  338. 338pub fn morph_open_16(img : @types.Image16) -> @types.Image16 {
  339. 339● dilate_16(erode_16(img))
  340. 340}
  341. 341
  342. 342///|
  343. 343/// float 开运算(先腐蚀后膨胀)。
  344. 344/// @see morph_open 8-bit 版本,本函数为 ImageF 变体
  345. 345pub fn morph_open_f(img : @types.ImageF) -> @types.ImageF {
  346. 346● dilate_f(erode_f(img))
  347. 347}
  348. 348
  349. 349///|
  350. 350/// 16-bit 闭运算(先膨胀后腐蚀)。
  351. 351/// @see morph_close 8-bit 版本,本函数为 Image16 变体
  352. 352pub fn morph_close_16(img : @types.Image16) -> @types.Image16 {
  353. 353● erode_16(dilate_16(img))
  354. 354}
  355. 355
  356. 356///|
  357. 357/// float 闭运算(先膨胀后腐蚀)。
  358. 358/// @see morph_close 8-bit 版本,本函数为 ImageF 变体
  359. 359pub fn morph_close_f(img : @types.ImageF) -> @types.ImageF {
  360. 360● erode_f(dilate_f(img))
  361. 361}
  362. 362
  363. 363///|
  364. 364/// 16-bit 形态学梯度(dilate - erode)。
  365. 365/// @see morph_gradient 8-bit 版本,本函数为 Image16 变体
  366. 366pub fn morph_gradient_16(img : @types.Image16) -> @types.Image16 {
  367. 367● let dilated = dilate_16(img)
  368. 368● let eroded = erode_16(img)
  369. 369 let ch = img.channels
  370. 370 let w = img.width
  371. 371 let h = img.height
  372. 372 let n = w * h
  373. 373 let bpp = ch * 2
  374. 374● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  375. 375 for i in 0..<(n * ch) {
  376. 376● let v = read_u16_le_m(dilated.data, i * 2) -
  377. 377● read_u16_le_m(eroded.data, i * 2)
  378. 378◐ write_u16_le_m(out, i * 2, if v < 0 { 0 } else { v })
  379. 379 }
  380. 380 @types.Image16::{
  381. 381 width: w,
  382. 382 height: h,
  383. 383 channels: ch,
  384. 384● data: Bytes::from_array(out),
  385. 385 }
  386. 386}
  387. 387
  388. 388///|
  389. 389/// float 形态学梯度(dilate - erode)。
  390. 390/// @see morph_gradient 8-bit 版本,本函数为 ImageF 变体
  391. 391pub fn morph_gradient_f(img : @types.ImageF) -> @types.ImageF {
  392. 392● let dilated = dilate_f(img)
  393. 393● let eroded = erode_f(img)
  394. 394 let ch = img.channels
  395. 395 let w = img.width
  396. 396 let h = img.height
  397. 397 let n = w * h
  398. 398 let bpp = ch * 4
  399. 399● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  400. 400 for i in 0..<(n * ch) {
  401. 401● let v = read_float_le_m(dilated.data, i * 4) -
  402. 402● read_float_le_m(eroded.data, i * 4)
  403. 403◐ write_f32_le_m(out, i * 4, if v < 0.0F { 0.0F } else { v })
  404. 404 }
  405. 405 @types.ImageF::{
  406. 406 width: w,
  407. 407 height: h,
  408. 408 channels: ch,
  409. 409● data: Bytes::from_array(out),
  410. 410 }
  411. 411}
  412. 412
  413. 413///|
  414. 414/// 16-bit 顶帽变换(original - open)。
  415. 415/// @see morph_tophat 8-bit 版本,本函数为 Image16 变体
  416. 416pub fn morph_tophat_16(img : @types.Image16) -> @types.Image16 {
  417. 417● let opened = morph_open_16(img)
  418. 418 let ch = img.channels
  419. 419 let w = img.width
  420. 420 let h = img.height
  421. 421 let n = w * h
  422. 422 let bpp = ch * 2
  423. 423● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  424. 424 for i in 0..<(n * ch) {
  425. 425● let v = read_u16_le_m(img.data, i * 2) - read_u16_le_m(opened.data, i * 2)
  426. 426◐ write_u16_le_m(out, i * 2, if v < 0 { 0 } else { v })
  427. 427 }
  428. 428 @types.Image16::{
  429. 429 width: w,
  430. 430 height: h,
  431. 431 channels: ch,
  432. 432● data: Bytes::from_array(out),
  433. 433 }
  434. 434}
  435. 435
  436. 436///|
  437. 437/// float 顶帽变换(original - open)。
  438. 438/// @see morph_tophat 8-bit 版本,本函数为 ImageF 变体
  439. 439pub fn morph_tophat_f(img : @types.ImageF) -> @types.ImageF {
  440. 440● let opened = morph_open_f(img)
  441. 441 let ch = img.channels
  442. 442 let w = img.width
  443. 443 let h = img.height
  444. 444 let n = w * h
  445. 445 let bpp = ch * 4
  446. 446● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  447. 447 for i in 0..<(n * ch) {
  448. 448● let v = read_float_le_m(img.data, i * 4) -
  449. 449● read_float_le_m(opened.data, i * 4)
  450. 450◐ write_f32_le_m(out, i * 4, if v < 0.0F { 0.0F } else { v })
  451. 451 }
  452. 452 @types.ImageF::{
  453. 453 width: w,
  454. 454 height: h,
  455. 455 channels: ch,
  456. 456● data: Bytes::from_array(out),
  457. 457 }
  458. 458}
  459. 459
  460. 460///|
  461. 461/// 16-bit 黑帽变换(close - original)。
  462. 462/// @see morph_blackhat 8-bit 版本,本函数为 Image16 变体
  463. 463pub fn morph_blackhat_16(img : @types.Image16) -> @types.Image16 {
  464. 464● let closed = morph_close_16(img)
  465. 465 let ch = img.channels
  466. 466 let w = img.width
  467. 467 let h = img.height
  468. 468 let n = w * h
  469. 469 let bpp = ch * 2
  470. 470● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  471. 471 for i in 0..<(n * ch) {
  472. 472● let v = read_u16_le_m(closed.data, i * 2) - read_u16_le_m(img.data, i * 2)
  473. 473◐ write_u16_le_m(out, i * 2, if v < 0 { 0 } else { v })
  474. 474 }
  475. 475 @types.Image16::{
  476. 476 width: w,
  477. 477 height: h,
  478. 478 channels: ch,
  479. 479● data: Bytes::from_array(out),
  480. 480 }
  481. 481}
  482. 482
  483. 483///|
  484. 484/// float 黑帽变换(close - original)。
  485. 485/// @see morph_blackhat 8-bit 版本,本函数为 ImageF 变体
  486. 486pub fn morph_blackhat_f(img : @types.ImageF) -> @types.ImageF {
  487. 487● let closed = morph_close_f(img)
  488. 488 let ch = img.channels
  489. 489 let w = img.width
  490. 490 let h = img.height
  491. 491 let n = w * h
  492. 492 let bpp = ch * 4
  493. 493● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  494. 494 for i in 0..<(n * ch) {
  495. 495● let v = read_float_le_m(closed.data, i * 4) -
  496. 496● read_float_le_m(img.data, i * 4)
  497. 497◐ write_f32_le_m(out, i * 4, if v < 0.0F { 0.0F } else { v })
  498. 498 }
  499. 499 @types.ImageF::{
  500. 500 width: w,
  501. 501 height: h,
  502. 502 channels: ch,
  503. 503● data: Bytes::from_array(out),
  504. 504 }
  505. 505}
  506. 506
  507. 507// ========== 自定义结构元素 ==========
  508. 508
  509. 509///|
  510. 510/// 16-bit 自定义结构元素腐蚀。
  511. 511/// @see erode_custom 8-bit 版本,本函数为 Image16 变体
  512. 512pub fn erode_custom_16(
  513. 513 img : @types.Image16,
  514. 514 se : StructuringElement,
  515. 515) -> @types.Image16 {
  516. 516● let ch = img.channels
  517. 517 let w = img.width
  518. 518 let h = img.height
  519. 519 let n = w * h
  520. 520 let bpp = ch * 2
  521. 521● let src : Array[Int32] = Array::make(n * ch, 0)
  522. 522 for i in 0..<(n * ch) {
  523. 523● src[i] = read_u16_le_m(img.data, i * 2)
  524. 524 }
  525. 525● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  526. 526 let cx = se.width / 2
  527. 527 let cy = se.height / 2
  528. 528 for y in 0..<h {
  529. 529● for x in 0..<w {
  530. 530● for c in 0..<ch {
  531. 531● let mut min_v = 65535
  532. 532 for sy in 0..<se.height {
  533. 533● for sx in 0..<se.width {
  534. 534● if se.data[sy * se.width + sx] {
  535. 535● let nx = @types.clamp_int(x + sx - cx, 0, w - 1)
  536. 536● let ny = @types.clamp_int(y + sy - cy, 0, h - 1)
  537. 537 let v = src[(ny * w + nx) * ch + c]
  538. 538 if v < min_v {
  539. 539● min_v = v
  540. 540 }
  541. 541 }
  542. 542 }
  543. 543 }
  544. 544● write_u16_le_m(out, (y * w + x) * bpp + c * 2, min_v)
  545. 545 }
  546. 546 }
  547. 547 }
  548. 548 @types.Image16::{
  549. 549 width: w,
  550. 550 height: h,
  551. 551 channels: ch,
  552. 552● data: Bytes::from_array(out),
  553. 553 }
  554. 554}
  555. 555
  556. 556///|
  557. 557/// float 自定义结构元素腐蚀。
  558. 558/// @see erode_custom 8-bit 版本,本函数为 ImageF 变体
  559. 559pub fn erode_custom_f(
  560. 560 img : @types.ImageF,
  561. 561 se : StructuringElement,
  562. 562) -> @types.ImageF {
  563. 563● let ch = img.channels
  564. 564 let w = img.width
  565. 565 let h = img.height
  566. 566 let n = w * h
  567. 567 let bpp = ch * 4
  568. 568● let src : Array[Float] = Array::make(n * ch, 0.0F)
  569. 569 for i in 0..<(n * ch) {
  570. 570● src[i] = read_float_le_m(img.data, i * 4)
  571. 571 }
  572. 572● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  573. 573 let cx = se.width / 2
  574. 574 let cy = se.height / 2
  575. 575 for y in 0..<h {
  576. 576● for x in 0..<w {
  577. 577● for c in 0..<ch {
  578. 578● let mut min_v = 1.0F
  579. 579 for sy in 0..<se.height {
  580. 580● for sx in 0..<se.width {
  581. 581● if se.data[sy * se.width + sx] {
  582. 582● let nx = @types.clamp_int(x + sx - cx, 0, w - 1)
  583. 583● let ny = @types.clamp_int(y + sy - cy, 0, h - 1)
  584. 584 let v = src[(ny * w + nx) * ch + c]
  585. 585 if v < min_v {
  586. 586● min_v = v
  587. 587 }
  588. 588 }
  589. 589 }
  590. 590 }
  591. 591● write_f32_le_m(out, (y * w + x) * bpp + c * 4, min_v)
  592. 592 }
  593. 593 }
  594. 594 }
  595. 595 @types.ImageF::{
  596. 596 width: w,
  597. 597 height: h,
  598. 598 channels: ch,
  599. 599● data: Bytes::from_array(out),
  600. 600 }
  601. 601}
  602. 602
  603. 603///|
  604. 604/// 16-bit 自定义结构元素膨胀。
  605. 605/// @see dilate_custom 8-bit 版本,本函数为 Image16 变体
  606. 606pub fn dilate_custom_16(
  607. 607 img : @types.Image16,
  608. 608 se : StructuringElement,
  609. 609) -> @types.Image16 {
  610. 610● let ch = img.channels
  611. 611 let w = img.width
  612. 612 let h = img.height
  613. 613 let n = w * h
  614. 614 let bpp = ch * 2
  615. 615● let src : Array[Int32] = Array::make(n * ch, 0)
  616. 616 for i in 0..<(n * ch) {
  617. 617● src[i] = read_u16_le_m(img.data, i * 2)
  618. 618 }
  619. 619● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  620. 620 let cx = se.width / 2
  621. 621 let cy = se.height / 2
  622. 622 for y in 0..<h {
  623. 623● for x in 0..<w {
  624. 624● for c in 0..<ch {
  625. 625● let mut max_v = 0
  626. 626 for sy in 0..<se.height {
  627. 627● for sx in 0..<se.width {
  628. 628● if se.data[sy * se.width + sx] {
  629. 629● let nx = @types.clamp_int(x + sx - cx, 0, w - 1)
  630. 630● let ny = @types.clamp_int(y + sy - cy, 0, h - 1)
  631. 631 let v = src[(ny * w + nx) * ch + c]
  632. 632 if v > max_v {
  633. 633● max_v = v
  634. 634 }
  635. 635 }
  636. 636 }
  637. 637 }
  638. 638● write_u16_le_m(out, (y * w + x) * bpp + c * 2, max_v)
  639. 639 }
  640. 640 }
  641. 641 }
  642. 642 @types.Image16::{
  643. 643 width: w,
  644. 644 height: h,
  645. 645 channels: ch,
  646. 646● data: Bytes::from_array(out),
  647. 647 }
  648. 648}
  649. 649
  650. 650///|
  651. 651/// float 自定义结构元素膨胀。
  652. 652/// @see dilate_custom 8-bit 版本,本函数为 ImageF 变体
  653. 653pub fn dilate_custom_f(
  654. 654 img : @types.ImageF,
  655. 655 se : StructuringElement,
  656. 656) -> @types.ImageF {
  657. 657● let ch = img.channels
  658. 658 let w = img.width
  659. 659 let h = img.height
  660. 660 let n = w * h
  661. 661 let bpp = ch * 4
  662. 662● let src : Array[Float] = Array::make(n * ch, 0.0F)
  663. 663 for i in 0..<(n * ch) {
  664. 664● src[i] = read_float_le_m(img.data, i * 4)
  665. 665 }
  666. 666● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  667. 667 let cx = se.width / 2
  668. 668 let cy = se.height / 2
  669. 669 for y in 0..<h {
  670. 670● for x in 0..<w {
  671. 671● for c in 0..<ch {
  672. 672● let mut max_v = 0.0F
  673. 673 for sy in 0..<se.height {
  674. 674● for sx in 0..<se.width {
  675. 675● if se.data[sy * se.width + sx] {
  676. 676● let nx = @types.clamp_int(x + sx - cx, 0, w - 1)
  677. 677● let ny = @types.clamp_int(y + sy - cy, 0, h - 1)
  678. 678 let v = src[(ny * w + nx) * ch + c]
  679. 679 if v > max_v {
  680. 680● max_v = v
  681. 681 }
  682. 682 }
  683. 683 }
  684. 684 }
  685. 685● write_f32_le_m(out, (y * w + x) * bpp + c * 4, max_v)
  686. 686 }
  687. 687 }
  688. 688 }
  689. 689 @types.ImageF::{
  690. 690 width: w,
  691. 691 height: h,
  692. 692 channels: ch,
  693. 693● data: Bytes::from_array(out),
  694. 694 }
  695. 695}
  696. 696
  697. 697///|
  698. 698/// 16-bit 自定义结构元素开运算。
  699. 699/// @see morph_open_custom 8-bit 版本,本函数为 Image16 变体
  700. 700pub fn morph_open_custom_16(
  701. 701 img : @types.Image16,
  702. 702 se : StructuringElement,
  703. 703) -> @types.Image16 {
  704. 704● dilate_custom_16(erode_custom_16(img, se), se)
  705. 705}
  706. 706
  707. 707///|
  708. 708/// float 自定义结构元素开运算。
  709. 709/// @see morph_open_custom 8-bit 版本,本函数为 ImageF 变体
  710. 710pub fn morph_open_custom_f(
  711. 711 img : @types.ImageF,
  712. 712 se : StructuringElement,
  713. 713) -> @types.ImageF {
  714. 714○ dilate_custom_f(erode_custom_f(img, se), se)
  715. 715}
  716. 716
  717. 717///|
  718. 718/// 16-bit 自定义结构元素闭运算。
  719. 719/// @see morph_close_custom 8-bit 版本,本函数为 Image16 变体
  720. 720pub fn morph_close_custom_16(
  721. 721 img : @types.Image16,
  722. 722 se : StructuringElement,
  723. 723) -> @types.Image16 {
  724. 724○ erode_custom_16(dilate_custom_16(img, se), se)
  725. 725}
  726. 726
  727. 727///|
  728. 728/// float 自定义结构元素闭运算。
  729. 729/// @see morph_close_custom 8-bit 版本,本函数为 ImageF 变体
  730. 730pub fn morph_close_custom_f(
  731. 731 img : @types.ImageF,
  732. 732 se : StructuringElement,
  733. 733) -> @types.ImageF {
  734. 734● erode_custom_f(dilate_custom_f(img, se), se)
  735. 735}
  736. 736
  737. 737///|
  738. 738fn read_u16_le_m(data : Bytes, offset : Int32) -> Int32 {
  739. 739● data[offset].to_int() + data[offset + 1].to_int() * 256
  740. 740}
  741. 741
  742. 742///|
  743. 743fn write_u16_le_m(buf : Array[Byte], offset : Int32, val : Int32) -> Unit {
  744. 744● buf[offset] = (val & 0xFF).to_byte()
  745. 745● buf[offset + 1] = ((val >> 8) & 0xFF).to_byte()
  746. 746}
  747. 747
  748. 748///|
  749. 749fn read_float_le_m(data : Bytes, offset : Int32) -> Float {
  750. 750● let b0 = data[offset].to_int()
  751. 751● let b1 = data[offset + 1].to_int()
  752. 752● let b2 = data[offset + 2].to_int()
  753. 753● let b3 = data[offset + 3].to_int()
  754. 754 let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)
  755. 755● Float::reinterpret_from_int(bits)
  756. 756}
  757. 757
  758. 758///|
  759. 759fn write_f32_le_m(buf : Array[Byte], offset : Int32, val : Float) -> Unit {
  760. 760● let bits = val.reinterpret_as_int()
  761. 761● buf[offset] = (bits & 0xFF).to_byte()
  762. 762● buf[offset + 1] = ((bits >> 8) & 0xFF).to_byte()
  763. 763● buf[offset + 2] = ((bits >> 16) & 0xFF).to_byte()
  764. 764● buf[offset + 3] = ((bits >> 24) & 0xFF).to_byte()
  765. 765}