image.process\edge\hough_circles.mbt

87.3% · 48 / 55 points · 180 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/// # 霍夫圆检测 hough circles
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 霍夫圆检测(纯 MoonBit):hough_circles
  7. 7// 梯度法:对边缘点沿梯度方向投票
  8. 8
  9. 9///|
  10. 10/// 检测到的圆
  11. 11pub(all) struct Circle {
  12. 12 x : Int32
  13. 13 y : Int32
  14. 14 radius : Int32
  15. 15 votes : Int32
  16. 16}
  17. 17
  18. 18///|
  19. 19/// 霍夫圆检测。
  20. 20/// dp 为累加器分辨率倒数(1=同分辨率),min_dist 为圆心间最小距离。
  21. 21/// param1 为 Canny 高阈值,param2 为圆心投票阈值。
  22. 22pub fn hough_circles(
  23. 23 img : @types.Image,
  24. 24 dp~ : Int32 = 1,
  25. 25 min_dist~ : Int32 = 10,
  26. 26 param1~ : Int32 = 100,
  27. 27 param2~ : Int32 = 30,
  28. 28 min_radius~ : Int32 = 0,
  29. 29 max_radius~ : Int32 = 0,
  30. 30) -> Array[Circle] raise @types.LoadError {
  31. 31 // 输入验证
  32. 32● if img.width <= 0 || img.height <= 0 {
  33. 33● raise @types.LoadError::DecodeFailed(
  34. 34 "hough_circles: 图像尺寸必须大于0",
  35. 35 )
  36. 36 }
  37. 37 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  38. 38● raise @types.LoadError::DecodeFailed(
  39. 39 "hough_circles: 仅支持 channels=1/3/4",
  40. 40 )
  41. 41 }
  42. 42 let expected_size = img.width * img.height * img.channels
  43. 43● if img.data.length() != expected_size {
  44. 44● raise @types.LoadError::DecodeFailed(
  45. 45 "hough_circles: 数据大小不匹配,期望 " +
  46. 46● expected_size.to_string() +
  47. 47 ",实际 " +
  48. 48● img.data.length().to_string(),
  49. 49 )
  50. 50 }
  51. 51 if dp < 1 {
  52. 52● raise @types.LoadError::DecodeFailed("hough_circles: dp必须 >= 1")
  53. 53 }
  54. 54 if min_dist < 1 {
  55. 55● raise @types.LoadError::DecodeFailed("hough_circles: min_dist必须 >= 1")
  56. 56 }
  57. 57 if param1 <= 0 {
  58. 58● raise @types.LoadError::DecodeFailed("hough_circles: param1必须大于0")
  59. 59 }
  60. 60 if param2 <= 0 {
  61. 61● raise @types.LoadError::DecodeFailed("hough_circles: param2必须大于0")
  62. 62 }
  63. 63 if min_radius < 0 {
  64. 64● raise @types.LoadError::DecodeFailed("hough_circles: min_radius必须 >= 0")
  65. 65 }
  66. 66 if max_radius < 0 {
  67. 67● raise @types.LoadError::DecodeFailed("hough_circles: max_radius必须 >= 0")
  68. 68 }
  69. 69 let w = img.width
  70. 70 let h = img.height
  71. 71 let n = w * h
  72. 72 if n == 0 {
  73. 73○ return []
  74. 74 }
  75. 75● ignore(dp)
  76. 76 // 转灰度
  77. 77● let gray : Array[Int32] = Array::make(n, 0)
  78. 78 let ch = img.channels
  79. 79 for i in 0..<n {
  80. 80● gray[i] = if ch == 1 {
  81. 81● img.data[i].to_int()
  82. 82 } else {
  83. 83○ let r = img.data[i * ch].to_int()
  84. 84○ let g = img.data[i * ch + 1].to_int()
  85. 85○ let b = img.data[i * ch + 2].to_int()
  86. 86 (r * 299 + g * 587 + b * 114) / 1000
  87. 87 }
  88. 88 }
  89. 89 // Sobel 梯度
  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 // 边缘点(梯度幅值 > param1)
  110. 110● let r_max = if max_radius > 0 { max_radius } else { (w + h) / 4 }
  111. 111 let r_min = min_radius
  112. 112 if r_max <= r_min {
  113. 113○ return []
  114. 114 }
  115. 115 // 累加器:3D [w * dp][h * dp][r_max - r_min + 1]
  116. 116 let acc_w = w
  117. 117 let acc_h = h
  118. 118 let num_r = r_max - r_min + 1
  119. 119● let acc : Array[Int32] = Array::make(acc_w * acc_h * num_r, 0)
  120. 120 // 对每个边缘点投票
  121. 121 for y in 1..<(h - 1) {
  122. 122● for x in 1..<(w - 1) {
  123. 123● let idx = y * w + x
  124. 124 let mag = gx[idx] * gx[idx] + gy[idx] * gy[idx]
  125. 125 if mag < param1 * param1 {
  126. 126● continue
  127. 127 }
  128. 128 // 沿梯度方向投票
  129. 129 let dx = gx[idx]
  130. 130 let dy = gy[idx]
  131. 131● let mag_sqrt = Float::from_int(mag).sqrt().to_int()
  132. 132 if mag_sqrt == 0 {
  133. 133○ continue
  134. 134 }
  135. 135 for r in r_min..<=r_max {
  136. 136● let cx_pos = x + dx * r / mag_sqrt
  137. 137 let cy_pos = y + dy * r / mag_sqrt
  138. 138 let cx_neg = x - dx * r / mag_sqrt
  139. 139 let cy_neg = y - dy * r / mag_sqrt
  140. 140 if cx_pos >= 0 && cx_pos < acc_w && cy_pos >= 0 && cy_pos < acc_h {
  141. 141● acc[(cy_pos * acc_w + cx_pos) * num_r + (r - r_min)] += 1
  142. 142 }
  143. 143 if cx_neg >= 0 && cx_neg < acc_w && cy_neg >= 0 && cy_neg < acc_h {
  144. 144● acc[(cy_neg * acc_w + cx_neg) * num_r + (r - r_min)] += 1
  145. 145 }
  146. 146 }
  147. 147 }
  148. 148 }
  149. 149 // 找峰值
  150. 150 let candidates : Array[Circle] = []
  151. 151 for r in r_min..<=r_max {
  152. 152● for y in 0..<acc_h {
  153. 153● for x in 0..<acc_w {
  154. 154● let votes = acc[(y * acc_w + x) * num_r + (r - r_min)]
  155. 155 if votes >= param2 {
  156. 156● candidates.push(Circle::{ x, y, radius: r, votes, })
  157. 157 }
  158. 158 }
  159. 159 }
  160. 160 }
  161. 161 // 按投票数排序(降序)
  162. 162● candidates.sort_by(fn(a, b) { b.votes.compare(a.votes) })
  163. 163 // 距离过滤
  164. 164 let result : Array[Circle] = []
  165. 165 for c in candidates {
  166. 166● let mut ok = true
  167. 167 for r in result {
  168. 168● let dx = c.x - r.x
  169. 169 let dy = c.y - r.y
  170. 170 if dx * dx + dy * dy < min_dist * min_dist {
  171. 171● ok = false
  172. 172 break
  173. 173 }
  174. 174 }
  175. 175 if ok {
  176. 176● result.push(c)
  177. 177 }
  178. 178 }
  179. 179 result
  180. 180}