image.process\segment\segmentation_16f.mbt

100% · 47 / 47 points · 165 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/// # 图像分割 16-bit/float 变体 Image segmentation 16-bit/float variants
  3. 3///
  4. 4/// 16位分水岭/SLIC watershed_16 / slic_16
  5. 5/// 16位/浮点分水岭/SLIC 分割 16-bit/float watershed/SLIC segmentation
  6. 6// 图像分割 16-bit/float 变体
  7. 7// watershed / kmeans / SLIC 支持 Image16 / ImageF
  8. 8
  9. 9// ========== 辅助函数 ==========
  10. 10
  11. 11///|
  12. 12fn read_u16_le_seg(data : Bytes, offset : Int32) -> Int32 {
  13. 13● data[offset].to_int() + data[offset + 1].to_int() * 256
  14. 14}
  15. 15
  16. 16///|
  17. 17fn read_float_le_seg(data : Bytes, offset : Int32) -> Float {
  18. 18● let b0 = data[offset].to_int()
  19. 19● let b1 = data[offset + 1].to_int()
  20. 20● let b2 = data[offset + 2].to_int()
  21. 21● let b3 = data[offset + 3].to_int()
  22. 22 let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)
  23. 23● Float::reinterpret_from_int(bits)
  24. 24}
  25. 25
  26. 26///|
  27. 27fn img16_to_img8(img : @types.Image16) -> @types.Image {
  28. 28● let w = img.width
  29. 29 let h = img.height
  30. 30 let ch = img.channels
  31. 31● let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00')
  32. 32 for i in 0..<(w * h * ch) {
  33. 33● let v16 = read_u16_le_seg(img.data, i * 2)
  34. 34● data8[i] = (v16 / 257).to_byte()
  35. 35 }
  36. 36 @types.Image::{
  37. 37 width: w,
  38. 38 height: h,
  39. 39 channels: ch,
  40. 40● data: Bytes::from_array(data8),
  41. 41 }
  42. 42}
  43. 43
  44. 44///|
  45. 45fn imgf_to_img8(img : @types.ImageF) -> @types.Image {
  46. 46● let w = img.width
  47. 47 let h = img.height
  48. 48 let ch = img.channels
  49. 49● let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00')
  50. 50 for i in 0..<(w * h * ch) {
  51. 51● let vf = read_float_le_seg(img.data, i * 4)
  52. 52● let v8 = @types.clamp_int((vf * 255.0F).to_int(), 0, 255)
  53. 53● data8[i] = v8.to_byte()
  54. 54 }
  55. 55 @types.Image::{
  56. 56 width: w,
  57. 57 height: h,
  58. 58 channels: ch,
  59. 59● data: Bytes::from_array(data8),
  60. 60 }
  61. 61}
  62. 62
  63. 63// ========== Watershed 16-bit ==========
  64. 64
  65. 65///|
  66. 66/// 16-bit Watershed 分割。
  67. 67/// @see watershed 8-bit 版本,本函数为 Image16 变体
  68. 68pub fn watershed_16(
  69. 69 img : @types.Image16,
  70. 70 markers : Array[Int32],
  71. 71) -> Array[Int32] raise @types.LoadError {
  72. 72● let img8 = img16_to_img8(img)
  73. 73● watershed(img8, markers)
  74. 74}
  75. 75
  76. 76///|
  77. 77/// 16-bit 自动 Watershed 分割。
  78. 78/// @see watershed_auto 8-bit 版本,本函数为 Image16 变体
  79. 79pub fn watershed_auto_16(
  80. 80 img : @types.Image16,
  81. 81) -> (Array[Int32], Int32) raise @types.LoadError {
  82. 82● let img8 = img16_to_img8(img)
  83. 83● watershed_auto(img8)
  84. 84}
  85. 85
  86. 86// ========== Watershed float ==========
  87. 87
  88. 88///|
  89. 89/// float Watershed 分割。
  90. 90/// @see watershed 8-bit 版本,本函数为 ImageF 变体
  91. 91pub fn watershed_f(
  92. 92 img : @types.ImageF,
  93. 93 markers : Array[Int32],
  94. 94) -> Array[Int32] raise @types.LoadError {
  95. 95● let img8 = imgf_to_img8(img)
  96. 96● watershed(img8, markers)
  97. 97}
  98. 98
  99. 99///|
  100. 100/// float 自动 Watershed 分割。
  101. 101/// @see watershed_auto 8-bit 版本,本函数为 ImageF 变体
  102. 102pub fn watershed_auto_f(
  103. 103 img : @types.ImageF,
  104. 104) -> (Array[Int32], Int32) raise @types.LoadError {
  105. 105● let img8 = imgf_to_img8(img)
  106. 106● watershed_auto(img8)
  107. 107}
  108. 108
  109. 109// ========== K-means 16-bit ==========
  110. 110
  111. 111///|
  112. 112/// 16-bit K-means 图像分割。
  113. 113/// @see kmeans_segment 8-bit 版本,本函数为 Image16 变体
  114. 114pub fn kmeans_segment_16(
  115. 115 img : @types.Image16,
  116. 116 k : Int32,
  117. 117 max_iters~ : Int32 = 20,
  118. 118) -> (SegmentLabelImage, Array[SegmentRegion]) raise @types.LoadError {
  119. 119● let img8 = img16_to_img8(img)
  120. 120● kmeans_segment(img8, k, max_iters~)
  121. 121}
  122. 122
  123. 123// ========== K-means float ==========
  124. 124
  125. 125///|
  126. 126/// float K-means 图像分割。
  127. 127/// @see kmeans_segment 8-bit 版本,本函数为 ImageF 变体
  128. 128pub fn kmeans_segment_f(
  129. 129 img : @types.ImageF,
  130. 130 k : Int32,
  131. 131 max_iters~ : Int32 = 20,
  132. 132) -> (SegmentLabelImage, Array[SegmentRegion]) raise @types.LoadError {
  133. 133● let img8 = imgf_to_img8(img)
  134. 134● kmeans_segment(img8, k, max_iters~)
  135. 135}
  136. 136
  137. 137// ========== SLIC 16-bit ==========
  138. 138
  139. 139///|
  140. 140/// 16-bit SLIC 超像素分割。
  141. 141/// @see slic 8-bit 版本,本函数为 Image16 变体
  142. 142pub fn slic_16(
  143. 143 img : @types.Image16,
  144. 144 k : Int32,
  145. 145 m : Int32,
  146. 146 max_iters : Int32,
  147. 147) -> SuperpixelResult raise @types.LoadError {
  148. 148● let img8 = img16_to_img8(img)
  149. 149● slic(img8, k, m, max_iters)
  150. 150}
  151. 151
  152. 152// ========== SLIC float ==========
  153. 153
  154. 154///|
  155. 155/// float SLIC 超像素分割。
  156. 156/// @see slic 8-bit 版本,本函数为 ImageF 变体
  157. 157pub fn slic_f(
  158. 158 img : @types.ImageF,
  159. 159 k : Int32,
  160. 160 m : Int32,
  161. 161 max_iters : Int32,
  162. 162) -> SuperpixelResult raise @types.LoadError {
  163. 163● let img8 = imgf_to_img8(img)
  164. 164● slic(img8, k, m, max_iters)
  165. 165}