image.process\edge\contour_16f.mbt

97.2% · 106 / 109 points · 299 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 contour 16f
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 轮廓提取与绘制 16-bit/float 变体
  7. 7
  8. 8///|
  9. 9/// 提取轮廓 16-bit 版本。
  10. 10/// @see find_contours 8-bit 版本,本函数为 Image16 变体
  11. 11pub fn find_contours_16(
  12. 12 img : @types.Image16,
  13. 13) -> Array[Contour] raise @types.LoadError {
  14. 14 // 输入验证
  15. 15● if img.width <= 0 || img.height <= 0 {
  16. 16● raise @types.LoadError::DecodeFailed(
  17. 17 "find_contours_16: 图像尺寸必须大于0",
  18. 18 )
  19. 19 }
  20. 20 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  21. 21● raise @types.LoadError::DecodeFailed(
  22. 22 "find_contours_16: 仅支持 channels=1/3/4",
  23. 23 )
  24. 24 }
  25. 25 let expected_size = img.width * img.height * img.channels * 2
  26. 26● if img.data.length() != expected_size {
  27. 27● raise @types.LoadError::DecodeFailed(
  28. 28 "find_contours_16: 数据大小不匹配,期望 " +
  29. 29● expected_size.to_string() +
  30. 30 ",实际 " +
  31. 31● img.data.length().to_string(),
  32. 32 )
  33. 33 }
  34. 34 let w = img.width
  35. 35 let h = img.height
  36. 36 let ch = img.channels
  37. 37● let binary : Array[Bool] = Array::make(w * h, false)
  38. 38 for i in 0..<(w * h) {
  39. 39● let v = img.data[i * ch * 2].to_int() |
  40. 40● (img.data[i * ch * 2 + 1].to_int() << 8)
  41. 41 binary[i] = v > 0
  42. 42 }
  43. 43● let visited : Array[Bool] = Array::make(w * h, false)
  44. 44 let contours : Array[Contour] = []
  45. 45 for y in 0..<h {
  46. 46● for x in 0..<w {
  47. 47● let idx = y * w + x
  48. 48 if binary[idx] && !visited[idx] {
  49. 49● let is_outer = y == 0 || !binary[idx - w]
  50. 50● let contour = trace_boundary_16f(binary, visited, x, y, w, h, is_outer)
  51. 51● if contour.points.length() > 0 {
  52. 52● contours.push(contour)
  53. 53 }
  54. 54 }
  55. 55 }
  56. 56 }
  57. 57 contours
  58. 58}
  59. 59
  60. 60///|
  61. 61/// 提取轮廓 float 版本。
  62. 62/// @see find_contours 8-bit 版本,本函数为 ImageF 变体
  63. 63pub fn find_contours_f(
  64. 64 img : @types.ImageF,
  65. 65) -> Array[Contour] raise @types.LoadError {
  66. 66 // 输入验证
  67. 67● if img.width <= 0 || img.height <= 0 {
  68. 68● raise @types.LoadError::DecodeFailed(
  69. 69 "find_contours_f: 图像尺寸必须大于0",
  70. 70 )
  71. 71 }
  72. 72 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  73. 73● raise @types.LoadError::DecodeFailed(
  74. 74 "find_contours_f: 仅支持 channels=1/3/4",
  75. 75 )
  76. 76 }
  77. 77 let expected_size = img.width * img.height * img.channels * 4
  78. 78● if img.data.length() != expected_size {
  79. 79● raise @types.LoadError::DecodeFailed(
  80. 80 "find_contours_f: 数据大小不匹配,期望 " +
  81. 81● expected_size.to_string() +
  82. 82 ",实际 " +
  83. 83● img.data.length().to_string(),
  84. 84 )
  85. 85 }
  86. 86 let w = img.width
  87. 87 let h = img.height
  88. 88 let ch = img.channels
  89. 89● let binary : Array[Bool] = Array::make(w * h, false)
  90. 90 for i in 0..<(w * h) {
  91. 91● let bits = img.data[i * ch * 4].to_int() |
  92. 92● (img.data[i * ch * 4 + 1].to_int() << 8) |
  93. 93● (img.data[i * ch * 4 + 2].to_int() << 16) |
  94. 94● (img.data[i * ch * 4 + 3].to_int() << 24)
  95. 95● binary[i] = Float::reinterpret_from_int(bits) > 0.0F
  96. 96 }
  97. 97● let visited : Array[Bool] = Array::make(w * h, false)
  98. 98 let contours : Array[Contour] = []
  99. 99 for y in 0..<h {
  100. 100● for x in 0..<w {
  101. 101● let idx = y * w + x
  102. 102 if binary[idx] && !visited[idx] {
  103. 103● let is_outer = y == 0 || !binary[idx - w]
  104. 104● let contour = trace_boundary_16f(binary, visited, x, y, w, h, is_outer)
  105. 105● if contour.points.length() > 0 {
  106. 106● contours.push(contour)
  107. 107 }
  108. 108 }
  109. 109 }
  110. 110 }
  111. 111 contours
  112. 112}
  113. 113
  114. 114///|
  115. 115/// Moore 边界跟踪(16/f 共用)
  116. 116fn trace_boundary_16f(
  117. 117 binary : Array[Bool],
  118. 118 visited : Array[Bool],
  119. 119 start_x : Int32,
  120. 120 start_y : Int32,
  121. 121 w : Int32,
  122. 122 h : Int32,
  123. 123 is_outer : Bool,
  124. 124) -> Contour {
  125. 125● let points : Array[ContourPoint] = []
  126. 126 let mut cx = start_x
  127. 127 let mut cy = start_y
  128. 128 let mut dir_idx = 0
  129. 129 let mut count = 0
  130. 130 let max_count = w * h * 8 + 1
  131. 131 while count < max_count {
  132. 132● let idx = cy * w + cx
  133. 133 if !visited[idx] {
  134. 134● visited[idx] = true
  135. 135● points.push(ContourPoint::{ x: cx, y: cy, })
  136. 136 }
  137. 137 count += 1
  138. 138 let mut found = false
  139. 139 for i in 0..<8 {
  140. 140● let d = (dir_idx + i) % 8
  141. 141● let (dx, dy) = match d {
  142. 142● 0 => (1, 0)
  143. 143● 1 => (1, 1)
  144. 144● 2 => (0, 1)
  145. 145● 3 => (-1, 1)
  146. 146● 4 => (-1, 0)
  147. 147● 5 => (-1, -1)
  148. 148● 6 => (0, -1)
  149. 149● _ => (1, -1)
  150. 150 }
  151. 151 let nx = cx + dx
  152. 152 let ny = cy + dy
  153. 153 if nx >= 0 && nx < w && ny >= 0 && ny < h && binary[ny * w + nx] {
  154. 154● cx = nx
  155. 155 cy = ny
  156. 156 dir_idx = (d + 6) % 8
  157. 157 found = true
  158. 158 break
  159. 159 }
  160. 160 }
  161. 161 if !found {
  162. 162○ break
  163. 163 }
  164. 164 if cx == start_x && cy == start_y {
  165. 165● break
  166. 166 }
  167. 167 }
  168. 168 Contour::{ points, is_outer, }
  169. 169}
  170. 170
  171. 171///|
  172. 172/// 绘制轮廓 16-bit 版本。
  173. 173/// @see draw_contours 8-bit 版本,本函数为 Image16 变体
  174. 174pub fn draw_contours_16(
  175. 175 img : @types.Image16,
  176. 176 contours : Array[Contour],
  177. 177 color : Array[Int32],
  178. 178) -> @types.Image16 raise @types.LoadError {
  179. 179 // 输入验证
  180. 180● if img.width <= 0 || img.height <= 0 {
  181. 181● raise @types.LoadError::DecodeFailed(
  182. 182 "draw_contours_16: 图像尺寸必须大于0",
  183. 183 )
  184. 184 }
  185. 185 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  186. 186● raise @types.LoadError::DecodeFailed(
  187. 187 "draw_contours_16: 仅支持 channels=1/3/4",
  188. 188 )
  189. 189 }
  190. 190 let expected_size = img.width * img.height * img.channels * 2
  191. 191● if img.data.length() != expected_size {
  192. 192● raise @types.LoadError::DecodeFailed(
  193. 193 "draw_contours_16: 数据大小不匹配,期望 " +
  194. 194● expected_size.to_string() +
  195. 195 ",实际 " +
  196. 196● img.data.length().to_string(),
  197. 197 )
  198. 198 }
  199. 199● if color.length() == 0 {
  200. 200● raise @types.LoadError::DecodeFailed(
  201. 201 "draw_contours_16: color数组不能为空",
  202. 202 )
  203. 203 }
  204. 204 let ch = img.channels
  205. 205 let w = img.width
  206. 206 let h = img.height
  207. 207● let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00')
  208. 208 for i in 0..<(w * h * ch) {
  209. 209● out[i * 2] = img.data[i * 2]
  210. 210 out[i * 2 + 1] = img.data[i * 2 + 1]
  211. 211 }
  212. 212◐ let draw_ch = if ch >= 3 { 3 } else { 1 }
  213. 213 for contour in contours {
  214. 214● for pt in contour.points {
  215. 215● if pt.x >= 0 && pt.x < w && pt.y >= 0 && pt.y < h {
  216. 216● for c in 0..<draw_ch {
  217. 217● if c < color.length() {
  218. 218● let v = color[c]
  219. 219● out[((pt.y * w + pt.x) * ch + c) * 2] = (v & 0xFF).to_byte()
  220. 220● out[((pt.y * w + pt.x) * ch + c) * 2 + 1] = ((v >> 8) & 0xFF).to_byte()
  221. 221 }
  222. 222 }
  223. 223 }
  224. 224 }
  225. 225 }
  226. 226 @types.Image16::{
  227. 227 width: w,
  228. 228 height: h,
  229. 229 channels: ch,
  230. 230● data: Bytes::from_array(out),
  231. 231 }
  232. 232}
  233. 233
  234. 234///|
  235. 235/// 绘制轮廓 float 版本。
  236. 236/// @see draw_contours 8-bit 版本,本函数为 ImageF 变体
  237. 237pub fn draw_contours_f(
  238. 238 img : @types.ImageF,
  239. 239 contours : Array[Contour],
  240. 240 color : Array[Float],
  241. 241) -> @types.ImageF raise @types.LoadError {
  242. 242 // 输入验证
  243. 243● if img.width <= 0 || img.height <= 0 {
  244. 244● raise @types.LoadError::DecodeFailed(
  245. 245 "draw_contours_f: 图像尺寸必须大于0",
  246. 246 )
  247. 247 }
  248. 248 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  249. 249● raise @types.LoadError::DecodeFailed(
  250. 250 "draw_contours_f: 仅支持 channels=1/3/4",
  251. 251 )
  252. 252 }
  253. 253 let expected_size = img.width * img.height * img.channels * 4
  254. 254● if img.data.length() != expected_size {
  255. 255● raise @types.LoadError::DecodeFailed(
  256. 256 "draw_contours_f: 数据大小不匹配,期望 " +
  257. 257● expected_size.to_string() +
  258. 258 ",实际 " +
  259. 259● img.data.length().to_string(),
  260. 260 )
  261. 261 }
  262. 262● if color.length() == 0 {
  263. 263● raise @types.LoadError::DecodeFailed(
  264. 264 "draw_contours_f: color数组不能为空",
  265. 265 )
  266. 266 }
  267. 267 let ch = img.channels
  268. 268 let w = img.width
  269. 269 let h = img.height
  270. 270● let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00')
  271. 271 for i in 0..<(w * h * ch) {
  272. 272● out[i * 4] = img.data[i * 4]
  273. 273 out[i * 4 + 1] = img.data[i * 4 + 1]
  274. 274 out[i * 4 + 2] = img.data[i * 4 + 2]
  275. 275 out[i * 4 + 3] = img.data[i * 4 + 3]
  276. 276 }
  277. 277◐ let draw_ch = if ch >= 3 { 3 } else { 1 }
  278. 278 for contour in contours {
  279. 279● for pt in contour.points {
  280. 280● if pt.x >= 0 && pt.x < w && pt.y >= 0 && pt.y < h {
  281. 281● for c in 0..<draw_ch {
  282. 282● if c < color.length() {
  283. 283● let bits = color[c].reinterpret_as_int()
  284. 284● out[((pt.y * w + pt.x) * ch + c) * 4] = (bits & 0xFF).to_byte()
  285. 285● out[((pt.y * w + pt.x) * ch + c) * 4 + 1] = ((bits >> 8) & 0xFF).to_byte()
  286. 286● out[((pt.y * w + pt.x) * ch + c) * 4 + 2] = ((bits >> 16) & 0xFF).to_byte()
  287. 287● out[((pt.y * w + pt.x) * ch + c) * 4 + 3] = ((bits >> 24) & 0xFF).to_byte()
  288. 288 }
  289. 289 }
  290. 290 }
  291. 291 }
  292. 292 }
  293. 293 @types.ImageF::{
  294. 294 width: w,
  295. 295 height: h,
  296. 296 channels: ch,
  297. 297● data: Bytes::from_array(out),
  298. 298 }
  299. 299}