image.process\edge\hough.mbt

95% · 57 / 60 points · 169 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
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 霍夫变换(直线检测,纯 MoonBit)
  7. 7
  8. 8///|
  9. 9/// 检测到的直线
  10. 10pub(all) struct HoughLine {
  11. 11 rho : Float // 极径(原点到直线的垂直距离)
  12. 12 theta : Float // 极角(弧度)
  13. 13 votes : Int32 // 累加器票数
  14. 14}
  15. 15
  16. 16///|
  17. 17/// 霍夫直线变换。对二值图(非零为边缘点)检测直线。
  18. 18/// theta_resolution 为角度分辨率(度数,默认 1.0),rho_resolution 为极径分辨率(像素,默认 1.0)。
  19. 19/// threshold 为最小票数阈值,返回票数 >= threshold 的直线,按票数降序排列。
  20. 20pub fn hough_lines(
  21. 21 img : @types.Image,
  22. 22 threshold : Int32,
  23. 23 theta_resolution~ : Float = 1.0F,
  24. 24 rho_resolution~ : Float = 1.0F,
  25. 25) -> Array[HoughLine] raise @types.LoadError {
  26. 26 // 输入验证
  27. 27● if img.width <= 0 || img.height <= 0 {
  28. 28● raise @types.LoadError::DecodeFailed(
  29. 29 "hough_lines: 图像尺寸必须大于0",
  30. 30 )
  31. 31 }
  32. 32 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  33. 33● raise @types.LoadError::DecodeFailed(
  34. 34 "hough_lines: 仅支持 channels=1/3/4",
  35. 35 )
  36. 36 }
  37. 37 let expected_size = img.width * img.height * img.channels
  38. 38● if img.data.length() != expected_size {
  39. 39● raise @types.LoadError::DecodeFailed(
  40. 40 "hough_lines: 数据大小不匹配,期望 " +
  41. 41● expected_size.to_string() +
  42. 42 ",实际 " +
  43. 43● img.data.length().to_string(),
  44. 44 )
  45. 45 }
  46. 46 if threshold <= 0 {
  47. 47● raise @types.LoadError::DecodeFailed("hough_lines: threshold必须大于0")
  48. 48 }
  49. 49 if theta_resolution <= 0.0F {
  50. 50● raise @types.LoadError::DecodeFailed(
  51. 51 "hough_lines: theta_resolution必须大于0",
  52. 52 )
  53. 53 }
  54. 54 if rho_resolution <= 0.0F {
  55. 55● raise @types.LoadError::DecodeFailed(
  56. 56 "hough_lines: rho_resolution必须大于0",
  57. 57 )
  58. 58 }
  59. 59 let w = img.width
  60. 60 let h = img.height
  61. 61 let ch = img.channels
  62. 62 // 角度范围 0~180 度
  63. 63● let n_theta = (180.0F / theta_resolution).to_int()
  64. 64● let diag = Float::from_int(w * w + h * h).sqrt()
  65. 65● let max_rho = (diag / rho_resolution).to_int() + 1
  66. 66 let n_rho = 2 * max_rho + 1
  67. 67 // 累加器(一维数组,提高缓存友好性)
  68. 68● let accumulator : Array[Int32] = Array::make(n_rho * n_theta, 0)
  69. 69 // 预计算 sin/cos 表
  70. 70● let cos_table : Array[Float] = Array::make(n_theta, 0.0F)
  71. 71● let sin_table : Array[Float] = Array::make(n_theta, 0.0F)
  72. 72 let angle_scale = theta_resolution * 3.141592653589793F / 180.0F
  73. 73 for t in 0..<n_theta {
  74. 74● let angle = Float::from_int(t) * angle_scale
  75. 75● cos_table[t] = @math.cosf(angle)
  76. 76● sin_table[t] = @math.sinf(angle)
  77. 77 }
  78. 78 // 预计算 1/rho_resolution,用乘法替代除法
  79. 79 let inv_rho_res = 1.0F / rho_resolution
  80. 80 let rho_max_idx = n_rho - 1
  81. 81 // 预计算首通道 to_int(只看第一个通道判断边缘)
  82. 82● let data_int : Array[Int32] = Array::make(w * h, 0)
  83. 83 for i in 0..<(w * h) {
  84. 84● data_int[i] = img.data[i * ch].to_int()
  85. 85 }
  86. 86 // 遍历边缘点
  87. 87 for y in 0..<h {
  88. 88● let fy = Float::from_int(y)
  89. 89 let row_base = y * w
  90. 90 for x in 0..<w {
  91. 91● if data_int[row_base + x] == 0 {
  92. 92● continue
  93. 93 }
  94. 94● let fx = Float::from_int(x)
  95. 95 for t in 0..<n_theta {
  96. 96● let rho = fx * cos_table[t] + fy * sin_table[t]
  97. 97● let raw_idx = (rho * inv_rho_res).to_int() + max_rho
  98. 98 // 内联 clamp,避免方法调用开销
  99. 99 let rho_idx = if raw_idx < 0 {
  100. 100○ 0
  101. 101● } else if raw_idx > rho_max_idx {
  102. 102○ rho_max_idx
  103. 103 } else {
  104. 104● raw_idx
  105. 105 }
  106. 106 accumulator[rho_idx * n_theta + t] += 1
  107. 107 }
  108. 108 }
  109. 109 }
  110. 110 // 收集结果
  111. 111 let lines : Array[HoughLine] = []
  112. 112● let max_rho_f = Float::from_int(max_rho)
  113. 113 for r in 0..<n_rho {
  114. 114● let row_base = r * n_theta
  115. 115 for t in 0..<n_theta {
  116. 116● let votes = accumulator[row_base + t]
  117. 117 if votes >= threshold {
  118. 118● lines.push(HoughLine::{
  119. 119● rho: (Float::from_int(r) - max_rho_f) * rho_resolution,
  120. 120● theta: Float::from_int(t) * angle_scale,
  121. 121 votes,
  122. 122 })
  123. 123 }
  124. 124 }
  125. 125 }
  126. 126 // 按票数降序排序
  127. 127● lines.sort_by(fn(a, b) { b.votes - a.votes })
  128. 128 lines
  129. 129}
  130. 130
  131. 131///|
  132. 132/// 非极大值抑制。对霍夫变换结果做 NMS,去除相近的重复直线。
  133. 133pub fn hough_lines_nms(
  134. 134 lines : Array[HoughLine],
  135. 135 rho_threshold~ : Float = 5.0F,
  136. 136 theta_threshold~ : Float = 0.1F,
  137. 137) -> Array[HoughLine] raise @types.LoadError {
  138. 138 // 参数验证
  139. 139● if rho_threshold <= 0.0F {
  140. 140● raise @types.LoadError::DecodeFailed(
  141. 141 "hough_lines_nms: rho_threshold必须大于0",
  142. 142 )
  143. 143 }
  144. 144 if theta_threshold <= 0.0F {
  145. 145● raise @types.LoadError::DecodeFailed(
  146. 146 "hough_lines_nms: theta_threshold必须大于0",
  147. 147 )
  148. 148 }
  149. 149
  150. 150 let result : Array[HoughLine] = []
  151. 151● let suppressed : Array[Bool] = Array::make(lines.length(), false)
  152. 152● for i in 0..<lines.length() {
  153. 153● if suppressed[i] {
  154. 154● continue
  155. 155 }
  156. 156● result.push(lines[i])
  157. 157● for j in (i + 1)..<lines.length() {
  158. 158● if suppressed[j] {
  159. 159○ continue
  160. 160 }
  161. 161● let dr = (lines[i].rho - lines[j].rho).abs()
  162. 162● let dt = (lines[i].theta - lines[j].theta).abs()
  163. 163 if dr < rho_threshold && dt < theta_threshold {
  164. 164● suppressed[j] = true
  165. 165 }
  166. 166 }
  167. 167 }
  168. 168 result
  169. 169}