image.process\edge\hough_circles_16f.mbt

82.5% · 113 / 137 points · 347 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 hough circles 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 hough_circles 8-bit 版本,本函数为 Image16 变体
  11. 11pub fn hough_circles_16(
  12. 12 img : @types.Image16,
  13. 13 dp~ : Int32 = 1,
  14. 14 min_dist~ : Int32 = 10,
  15. 15 param1~ : Int32 = 100,
  16. 16 param2~ : Int32 = 30,
  17. 17 min_radius~ : Int32 = 0,
  18. 18 max_radius~ : Int32 = 0,
  19. 19) -> Array[Circle] raise @types.LoadError {
  20. 20 // 输入验证
  21. 21● if img.width <= 0 || img.height <= 0 {
  22. 22● raise @types.LoadError::DecodeFailed(
  23. 23 "hough_circles_16: 图像尺寸必须大于0",
  24. 24 )
  25. 25 }
  26. 26 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  27. 27● raise @types.LoadError::DecodeFailed(
  28. 28 "hough_circles_16: 仅支持 channels=1/3/4",
  29. 29 )
  30. 30 }
  31. 31 let expected_size = img.width * img.height * img.channels * 2
  32. 32● if img.data.length() != expected_size {
  33. 33● raise @types.LoadError::DecodeFailed(
  34. 34 "hough_circles_16: 数据大小不匹配,期望 " +
  35. 35● expected_size.to_string() +
  36. 36 ",实际 " +
  37. 37● img.data.length().to_string(),
  38. 38 )
  39. 39 }
  40. 40 if dp < 1 {
  41. 41● raise @types.LoadError::DecodeFailed("hough_circles_16: dp必须 >= 1")
  42. 42 }
  43. 43 if min_dist < 1 {
  44. 44● raise @types.LoadError::DecodeFailed(
  45. 45 "hough_circles_16: min_dist必须 >= 1",
  46. 46 )
  47. 47 }
  48. 48 if param1 <= 0 {
  49. 49● raise @types.LoadError::DecodeFailed(
  50. 50 "hough_circles_16: param1必须大于0",
  51. 51 )
  52. 52 }
  53. 53 if param2 <= 0 {
  54. 54● raise @types.LoadError::DecodeFailed(
  55. 55 "hough_circles_16: param2必须大于0",
  56. 56 )
  57. 57 }
  58. 58 if min_radius < 0 {
  59. 59● raise @types.LoadError::DecodeFailed(
  60. 60 "hough_circles_16: min_radius必须 >= 0",
  61. 61 )
  62. 62 }
  63. 63 if max_radius < 0 {
  64. 64● raise @types.LoadError::DecodeFailed(
  65. 65 "hough_circles_16: max_radius必须 >= 0",
  66. 66 )
  67. 67 }
  68. 68 let w = img.width
  69. 69 let h = img.height
  70. 70 let n = w * h
  71. 71 if n == 0 {
  72. 72○ return []
  73. 73 }
  74. 74● ignore(dp)
  75. 75 let ch = img.channels
  76. 76● let gray : Array[Int32] = Array::make(n, 0)
  77. 77 for i in 0..<n {
  78. 78● gray[i] = if ch == 1 {
  79. 79● img.data[i * 2].to_int() | (img.data[i * 2 + 1].to_int() << 8)
  80. 80 } else {
  81. 81● let r = img.data[i * ch * 2].to_int() |
  82. 82● (img.data[i * ch * 2 + 1].to_int() << 8)
  83. 83● let g = img.data[(i * ch + 1) * 2].to_int() |
  84. 84● (img.data[(i * ch + 1) * 2 + 1].to_int() << 8)
  85. 85● let b = img.data[(i * ch + 2) * 2].to_int() |
  86. 86● (img.data[(i * ch + 2) * 2 + 1].to_int() << 8)
  87. 87 (r * 299 + g * 587 + b * 114) / 1000
  88. 88 }
  89. 89 }
  90. 90● let gx : Array[Int32] = Array::make(n, 0)
  91. 91● let gy : Array[Int32] = Array::make(n, 0)
  92. 92 for y in 1..<(h - 1) {
  93. 93● for x in 1..<(w - 1) {
  94. 94● let idx = y * w + x
  95. 95 gx[idx] = gray[(y - 1) * w + (x + 1)] +
  96. 96 2 * gray[y * w + (x + 1)] +
  97. 97 gray[(y + 1) * w + (x + 1)] -
  98. 98 gray[(y - 1) * w + (x - 1)] -
  99. 99 2 * gray[y * w + (x - 1)] -
  100. 100 gray[(y + 1) * w + (x - 1)]
  101. 101 gy[idx] = gray[(y + 1) * w + (x - 1)] +
  102. 102 2 * gray[(y + 1) * w + x] +
  103. 103 gray[(y + 1) * w + (x + 1)] -
  104. 104 gray[(y - 1) * w + (x - 1)] -
  105. 105 2 * gray[(y - 1) * w + x] -
  106. 106 gray[(y - 1) * w + (x + 1)]
  107. 107 }
  108. 108 }
  109. 109● let r_max = if max_radius > 0 { max_radius } else { (w + h) / 4 }
  110. 110 let r_min = min_radius
  111. 111 if r_max <= r_min {
  112. 112○ return []
  113. 113 }
  114. 114 let acc_w = w
  115. 115 let acc_h = h
  116. 116 let num_r = r_max - r_min + 1
  117. 117● let acc : Array[Int32] = Array::make(acc_w * acc_h * num_r, 0)
  118. 118 for y in 1..<(h - 1) {
  119. 119● for x in 1..<(w - 1) {
  120. 120● let idx = y * w + x
  121. 121 let mag = gx[idx] * gx[idx] + gy[idx] * gy[idx]
  122. 122 if mag < param1 * param1 {
  123. 123● continue
  124. 124 }
  125. 125 let dx = gx[idx]
  126. 126 let dy = gy[idx]
  127. 127● let mag_sqrt = Float::from_int(mag).sqrt().to_int()
  128. 128 if mag_sqrt == 0 {
  129. 129○ continue
  130. 130 }
  131. 131 for r in r_min..<=r_max {
  132. 132● let cx_pos = x + dx * r / mag_sqrt
  133. 133 let cy_pos = y + dy * r / mag_sqrt
  134. 134 let cx_neg = x - dx * r / mag_sqrt
  135. 135 let cy_neg = y - dy * r / mag_sqrt
  136. 136 if cx_pos >= 0 && cx_pos < acc_w && cy_pos >= 0 && cy_pos < acc_h {
  137. 137● acc[(cy_pos * acc_w + cx_pos) * num_r + (r - r_min)] += 1
  138. 138 }
  139. 139 if cx_neg >= 0 && cx_neg < acc_w && cy_neg >= 0 && cy_neg < acc_h {
  140. 140● acc[(cy_neg * acc_w + cx_neg) * num_r + (r - r_min)] += 1
  141. 141 }
  142. 142 }
  143. 143 }
  144. 144 }
  145. 145 let candidates : Array[Circle] = []
  146. 146 for r in r_min..<=r_max {
  147. 147● for y in 0..<acc_h {
  148. 148● for x in 0..<acc_w {
  149. 149● let votes = acc[(y * acc_w + x) * num_r + (r - r_min)]
  150. 150 if votes >= param2 {
  151. 151○ candidates.push(Circle::{ x, y, radius: r, votes, })
  152. 152 }
  153. 153 }
  154. 154 }
  155. 155 }
  156. 156◐ candidates.sort_by(fn(a, b) { b.votes.compare(a.votes) })
  157. 157 let result : Array[Circle] = []
  158. 158 for c in candidates {
  159. 159○ let mut ok = true
  160. 160 for r in result {
  161. 161○ let dx = c.x - r.x
  162. 162 let dy = c.y - r.y
  163. 163 if dx * dx + dy * dy < min_dist * min_dist {
  164. 164○ ok = false
  165. 165 break
  166. 166 }
  167. 167 }
  168. 168 if ok {
  169. 169○ result.push(c)
  170. 170 }
  171. 171 }
  172. 172 result
  173. 173}
  174. 174
  175. 175///|
  176. 176/// 霍夫圆检测 float 版本。
  177. 177/// @see hough_circles 8-bit 版本,本函数为 ImageF 变体
  178. 178pub fn hough_circles_f(
  179. 179 img : @types.ImageF,
  180. 180 dp~ : Int32 = 1,
  181. 181 min_dist~ : Int32 = 10,
  182. 182 param1~ : Int32 = 100,
  183. 183 param2~ : Int32 = 30,
  184. 184 min_radius~ : Int32 = 0,
  185. 185 max_radius~ : Int32 = 0,
  186. 186) -> Array[Circle] raise @types.LoadError {
  187. 187 // 输入验证
  188. 188● if img.width <= 0 || img.height <= 0 {
  189. 189● raise @types.LoadError::DecodeFailed(
  190. 190 "hough_circles_f: 图像尺寸必须大于0",
  191. 191 )
  192. 192 }
  193. 193 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  194. 194● raise @types.LoadError::DecodeFailed(
  195. 195 "hough_circles_f: 仅支持 channels=1/3/4",
  196. 196 )
  197. 197 }
  198. 198 let expected_size = img.width * img.height * img.channels * 4
  199. 199● if img.data.length() != expected_size {
  200. 200● raise @types.LoadError::DecodeFailed(
  201. 201 "hough_circles_f: 数据大小不匹配,期望 " +
  202. 202● expected_size.to_string() +
  203. 203 ",实际 " +
  204. 204● img.data.length().to_string(),
  205. 205 )
  206. 206 }
  207. 207 if dp < 1 {
  208. 208● raise @types.LoadError::DecodeFailed("hough_circles_f: dp必须 >= 1")
  209. 209 }
  210. 210 if min_dist < 1 {
  211. 211● raise @types.LoadError::DecodeFailed("hough_circles_f: min_dist必须 >= 1")
  212. 212 }
  213. 213 if param1 <= 0 {
  214. 214● raise @types.LoadError::DecodeFailed("hough_circles_f: param1必须大于0")
  215. 215 }
  216. 216 if param2 <= 0 {
  217. 217● raise @types.LoadError::DecodeFailed("hough_circles_f: param2必须大于0")
  218. 218 }
  219. 219 if min_radius < 0 {
  220. 220● raise @types.LoadError::DecodeFailed(
  221. 221 "hough_circles_f: min_radius必须 >= 0",
  222. 222 )
  223. 223 }
  224. 224 if max_radius < 0 {
  225. 225● raise @types.LoadError::DecodeFailed(
  226. 226 "hough_circles_f: max_radius必须 >= 0",
  227. 227 )
  228. 228 }
  229. 229 let w = img.width
  230. 230 let h = img.height
  231. 231 let n = w * h
  232. 232 if n == 0 {
  233. 233○ return []
  234. 234 }
  235. 235● ignore(dp)
  236. 236 let ch = img.channels
  237. 237● let gray : Array[Float] = Array::make(n, 0.0F)
  238. 238 for i in 0..<n {
  239. 239● gray[i] = if ch == 1 {
  240. 240● let bits = img.data[i * 4].to_int() |
  241. 241● (img.data[i * 4 + 1].to_int() << 8) |
  242. 242● (img.data[i * 4 + 2].to_int() << 16) |
  243. 243● (img.data[i * 4 + 3].to_int() << 24)
  244. 244● Float::reinterpret_from_int(bits)
  245. 245 } else {
  246. 246● let bits_r = img.data[i * ch * 4].to_int() |
  247. 247● (img.data[i * ch * 4 + 1].to_int() << 8) |
  248. 248● (img.data[i * ch * 4 + 2].to_int() << 16) |
  249. 249● (img.data[i * ch * 4 + 3].to_int() << 24)
  250. 250● let bits_g = img.data[(i * ch + 1) * 4].to_int() |
  251. 251● (img.data[(i * ch + 1) * 4 + 1].to_int() << 8) |
  252. 252● (img.data[(i * ch + 1) * 4 + 2].to_int() << 16) |
  253. 253● (img.data[(i * ch + 1) * 4 + 3].to_int() << 24)
  254. 254● let bits_b = img.data[(i * ch + 2) * 4].to_int() |
  255. 255● (img.data[(i * ch + 2) * 4 + 1].to_int() << 8) |
  256. 256● (img.data[(i * ch + 2) * 4 + 2].to_int() << 16) |
  257. 257● (img.data[(i * ch + 2) * 4 + 3].to_int() << 24)
  258. 258● Float::reinterpret_from_int(bits_r) * 0.299F +
  259. 259● Float::reinterpret_from_int(bits_g) * 0.587F +
  260. 260● Float::reinterpret_from_int(bits_b) * 0.114F
  261. 261 }
  262. 262 }
  263. 263● let gx : Array[Float] = Array::make(n, 0.0F)
  264. 264● let gy : Array[Float] = Array::make(n, 0.0F)
  265. 265 for y in 1..<(h - 1) {
  266. 266● for x in 1..<(w - 1) {
  267. 267● let idx = y * w + x
  268. 268 gx[idx] = gray[(y - 1) * w + (x + 1)] +
  269. 269 2.0F * gray[y * w + (x + 1)] +
  270. 270 gray[(y + 1) * w + (x + 1)] -
  271. 271 gray[(y - 1) * w + (x - 1)] -
  272. 272 2.0F * gray[y * w + (x - 1)] -
  273. 273 gray[(y + 1) * w + (x - 1)]
  274. 274 gy[idx] = gray[(y + 1) * w + (x - 1)] +
  275. 275 2.0F * gray[(y + 1) * w + x] +
  276. 276 gray[(y + 1) * w + (x + 1)] -
  277. 277 gray[(y - 1) * w + (x - 1)] -
  278. 278 2.0F * gray[(y - 1) * w + x] -
  279. 279 gray[(y - 1) * w + (x + 1)]
  280. 280 }
  281. 281 }
  282. 282● let r_max = if max_radius > 0 { max_radius } else { (w + h) / 4 }
  283. 283 let r_min = min_radius
  284. 284 if r_max <= r_min {
  285. 285○ return []
  286. 286 }
  287. 287 let acc_w = w
  288. 288 let acc_h = h
  289. 289 let num_r = r_max - r_min + 1
  290. 290● let acc : Array[Int32] = Array::make(acc_w * acc_h * num_r, 0)
  291. 291● let param1_f = Float::from_int(param1) / 255.0F
  292. 292 for y in 1..<(h - 1) {
  293. 293● for x in 1..<(w - 1) {
  294. 294● let idx = y * w + x
  295. 295 let mag = gx[idx] * gx[idx] + gy[idx] * gy[idx]
  296. 296 if mag < param1_f * param1_f {
  297. 297● continue
  298. 298 }
  299. 299 let dx = gx[idx]
  300. 300 let dy = gy[idx]
  301. 301● let mag_sqrt = mag.sqrt()
  302. 302 if mag_sqrt == 0.0F {
  303. 303○ continue
  304. 304 }
  305. 305 for r in r_min..<=r_max {
  306. 306● let cx_pos = x + (dx * Float::from_int(r) / mag_sqrt).to_int()
  307. 307● let cy_pos = y + (dy * Float::from_int(r) / mag_sqrt).to_int()
  308. 308● let cx_neg = x - (dx * Float::from_int(r) / mag_sqrt).to_int()
  309. 309● let cy_neg = y - (dy * Float::from_int(r) / mag_sqrt).to_int()
  310. 310 if cx_pos >= 0 && cx_pos < acc_w && cy_pos >= 0 && cy_pos < acc_h {
  311. 311● acc[(cy_pos * acc_w + cx_pos) * num_r + (r - r_min)] += 1
  312. 312 }
  313. 313 if cx_neg >= 0 && cx_neg < acc_w && cy_neg >= 0 && cy_neg < acc_h {
  314. 314● acc[(cy_neg * acc_w + cx_neg) * num_r + (r - r_min)] += 1
  315. 315 }
  316. 316 }
  317. 317 }
  318. 318 }
  319. 319 let candidates : Array[Circle] = []
  320. 320 for r in r_min..<=r_max {
  321. 321● for y in 0..<acc_h {
  322. 322● for x in 0..<acc_w {
  323. 323● let votes = acc[(y * acc_w + x) * num_r + (r - r_min)]
  324. 324 if votes >= param2 {
  325. 325○ candidates.push(Circle::{ x, y, radius: r, votes, })
  326. 326 }
  327. 327 }
  328. 328 }
  329. 329 }
  330. 330◐ candidates.sort_by(fn(a, b) { b.votes.compare(a.votes) })
  331. 331 let result : Array[Circle] = []
  332. 332 for c in candidates {
  333. 333○ let mut ok = true
  334. 334 for r in result {
  335. 335○ let dx = c.x - r.x
  336. 336 let dy = c.y - r.y
  337. 337 if dx * dx + dy * dy < min_dist * min_dist {
  338. 338○ ok = false
  339. 339 break
  340. 340 }
  341. 341 }
  342. 342 if ok {
  343. 343○ result.push(c)
  344. 344 }
  345. 345 }
  346. 346 result
  347. 347}