image.process\edge\hough_16f.mbt

93.8% · 91 / 97 points · 222 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 16f
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 霍夫变换 16-bit/float 变体
  7. 7// hough_lines / hough_circles
  8. 8
  9. 9///|
  10. 10/// 霍夫直线变换 16-bit 版本。
  11. 11/// @see hough_lines 8-bit 版本,本函数为 Image16 变体
  12. 12pub fn hough_lines_16(
  13. 13 img : @types.Image16,
  14. 14 threshold : Int32,
  15. 15 theta_resolution~ : Float = 1.0F,
  16. 16 rho_resolution~ : Float = 1.0F,
  17. 17) -> Array[HoughLine] raise @types.LoadError {
  18. 18 // 输入验证
  19. 19● if img.width <= 0 || img.height <= 0 {
  20. 20● raise @types.LoadError::DecodeFailed(
  21. 21 "hough_lines_16: 图像尺寸必须大于0",
  22. 22 )
  23. 23 }
  24. 24 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  25. 25● raise @types.LoadError::DecodeFailed(
  26. 26 "hough_lines_16: 仅支持 channels=1/3/4",
  27. 27 )
  28. 28 }
  29. 29 let expected_size = img.width * img.height * img.channels * 2
  30. 30● if img.data.length() != expected_size {
  31. 31● raise @types.LoadError::DecodeFailed(
  32. 32 "hough_lines_16: 数据大小不匹配,期望 " +
  33. 33● expected_size.to_string() +
  34. 34 ",实际 " +
  35. 35● img.data.length().to_string(),
  36. 36 )
  37. 37 }
  38. 38 if threshold <= 0 {
  39. 39● raise @types.LoadError::DecodeFailed(
  40. 40 "hough_lines_16: threshold必须大于0",
  41. 41 )
  42. 42 }
  43. 43 if theta_resolution <= 0.0F {
  44. 44● raise @types.LoadError::DecodeFailed(
  45. 45 "hough_lines_16: theta_resolution必须大于0",
  46. 46 )
  47. 47 }
  48. 48 if rho_resolution <= 0.0F {
  49. 49○ raise @types.LoadError::DecodeFailed(
  50. 50 "hough_lines_16: rho_resolution必须大于0",
  51. 51 )
  52. 52 }
  53. 53 let w = img.width
  54. 54 let h = img.height
  55. 55 let ch = img.channels
  56. 56● let n_theta = (180.0F / theta_resolution).to_int()
  57. 57● let diag = Float::from_int(w * w + h * h).sqrt()
  58. 58● let max_rho = (diag / rho_resolution).to_int() + 1
  59. 59 let n_rho = 2 * max_rho + 1
  60. 60● let accumulator : Array[Int32] = Array::make(n_rho * n_theta, 0)
  61. 61● let cos_table : Array[Float] = Array::make(n_theta, 0.0F)
  62. 62● let sin_table : Array[Float] = Array::make(n_theta, 0.0F)
  63. 63 let angle_scale = theta_resolution * 3.141592653589793F / 180.0F
  64. 64 for t in 0..<n_theta {
  65. 65● let angle = Float::from_int(t) * angle_scale
  66. 66● cos_table[t] = @math.cosf(angle)
  67. 67● sin_table[t] = @math.sinf(angle)
  68. 68 }
  69. 69 let inv_rho_res = 1.0F / rho_resolution
  70. 70 let rho_max_idx = n_rho - 1
  71. 71● let data_int : Array[Int32] = Array::make(w * h, 0)
  72. 72 for i in 0..<(w * h) {
  73. 73● data_int[i] = img.data[i * ch * 2].to_int() |
  74. 74● (img.data[i * ch * 2 + 1].to_int() << 8)
  75. 75 }
  76. 76 for y in 0..<h {
  77. 77● let fy = Float::from_int(y)
  78. 78 let row_base = y * w
  79. 79 for x in 0..<w {
  80. 80● if data_int[row_base + x] == 0 {
  81. 81● continue
  82. 82 }
  83. 83● let fx = Float::from_int(x)
  84. 84 for t in 0..<n_theta {
  85. 85● let rho = fx * cos_table[t] + fy * sin_table[t]
  86. 86● let raw_idx = (rho * inv_rho_res).to_int() + max_rho
  87. 87 let rho_idx = if raw_idx < 0 {
  88. 88○ 0
  89. 89● } else if raw_idx > rho_max_idx {
  90. 90○ rho_max_idx
  91. 91 } else {
  92. 92● raw_idx
  93. 93 }
  94. 94 accumulator[rho_idx * n_theta + t] += 1
  95. 95 }
  96. 96 }
  97. 97 }
  98. 98 let lines : Array[HoughLine] = []
  99. 99● let max_rho_f = Float::from_int(max_rho)
  100. 100 for r in 0..<n_rho {
  101. 101● let row_base = r * n_theta
  102. 102 for t in 0..<n_theta {
  103. 103● let votes = accumulator[row_base + t]
  104. 104 if votes >= threshold {
  105. 105● let rho = (Float::from_int(r) - max_rho_f) * rho_resolution
  106. 106● let theta = Float::from_int(t) * angle_scale
  107. 107● lines.push(HoughLine::{ rho, theta, votes, })
  108. 108 }
  109. 109 }
  110. 110 }
  111. 111● lines.sort_by(fn(a, b) { b.votes.compare(a.votes) })
  112. 112 lines
  113. 113}
  114. 114
  115. 115///|
  116. 116/// 霍夫直线变换 float 版本。
  117. 117/// @see hough_lines 8-bit 版本,本函数为 ImageF 变体
  118. 118pub fn hough_lines_f(
  119. 119 img : @types.ImageF,
  120. 120 threshold : Int32,
  121. 121 theta_resolution~ : Float = 1.0F,
  122. 122 rho_resolution~ : Float = 1.0F,
  123. 123) -> Array[HoughLine] raise @types.LoadError {
  124. 124 // 输入验证
  125. 125● if img.width <= 0 || img.height <= 0 {
  126. 126● raise @types.LoadError::DecodeFailed(
  127. 127 "hough_lines_f: 图像尺寸必须大于0",
  128. 128 )
  129. 129 }
  130. 130 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  131. 131● raise @types.LoadError::DecodeFailed(
  132. 132 "hough_lines_f: 仅支持 channels=1/3/4",
  133. 133 )
  134. 134 }
  135. 135 let expected_size = img.width * img.height * img.channels * 4
  136. 136● if img.data.length() != expected_size {
  137. 137● raise @types.LoadError::DecodeFailed(
  138. 138 "hough_lines_f: 数据大小不匹配,期望 " +
  139. 139● expected_size.to_string() +
  140. 140 ",实际 " +
  141. 141● img.data.length().to_string(),
  142. 142 )
  143. 143 }
  144. 144 if threshold <= 0 {
  145. 145● raise @types.LoadError::DecodeFailed(
  146. 146 "hough_lines_f: threshold必须大于0",
  147. 147 )
  148. 148 }
  149. 149 if theta_resolution <= 0.0F {
  150. 150● raise @types.LoadError::DecodeFailed(
  151. 151 "hough_lines_f: theta_resolution必须大于0",
  152. 152 )
  153. 153 }
  154. 154 if rho_resolution <= 0.0F {
  155. 155○ raise @types.LoadError::DecodeFailed(
  156. 156 "hough_lines_f: rho_resolution必须大于0",
  157. 157 )
  158. 158 }
  159. 159 let w = img.width
  160. 160 let h = img.height
  161. 161 let ch = img.channels
  162. 162● let n_theta = (180.0F / theta_resolution).to_int()
  163. 163● let diag = Float::from_int(w * w + h * h).sqrt()
  164. 164● let max_rho = (diag / rho_resolution).to_int() + 1
  165. 165 let n_rho = 2 * max_rho + 1
  166. 166● let accumulator : Array[Int32] = Array::make(n_rho * n_theta, 0)
  167. 167● let cos_table : Array[Float] = Array::make(n_theta, 0.0F)
  168. 168● let sin_table : Array[Float] = Array::make(n_theta, 0.0F)
  169. 169 let angle_scale = theta_resolution * 3.141592653589793F / 180.0F
  170. 170 for t in 0..<n_theta {
  171. 171● let angle = Float::from_int(t) * angle_scale
  172. 172● cos_table[t] = @math.cosf(angle)
  173. 173● sin_table[t] = @math.sinf(angle)
  174. 174 }
  175. 175 let inv_rho_res = 1.0F / rho_resolution
  176. 176 let rho_max_idx = n_rho - 1
  177. 177● let data_f : Array[Float] = Array::make(w * h, 0.0F)
  178. 178 for i in 0..<(w * h) {
  179. 179● let bits = img.data[i * ch * 4].to_int() |
  180. 180● (img.data[i * ch * 4 + 1].to_int() << 8) |
  181. 181● (img.data[i * ch * 4 + 2].to_int() << 16) |
  182. 182● (img.data[i * ch * 4 + 3].to_int() << 24)
  183. 183● data_f[i] = Float::reinterpret_from_int(bits)
  184. 184 }
  185. 185 for y in 0..<h {
  186. 186● let fy = Float::from_int(y)
  187. 187 let row_base = y * w
  188. 188 for x in 0..<w {
  189. 189● if data_f[row_base + x] == 0.0F {
  190. 190● continue
  191. 191 }
  192. 192● let fx = Float::from_int(x)
  193. 193 for t in 0..<n_theta {
  194. 194● let rho = fx * cos_table[t] + fy * sin_table[t]
  195. 195● let raw_idx = (rho * inv_rho_res).to_int() + max_rho
  196. 196 let rho_idx = if raw_idx < 0 {
  197. 197○ 0
  198. 198● } else if raw_idx > rho_max_idx {
  199. 199○ rho_max_idx
  200. 200 } else {
  201. 201● raw_idx
  202. 202 }
  203. 203 accumulator[rho_idx * n_theta + t] += 1
  204. 204 }
  205. 205 }
  206. 206 }
  207. 207 let lines : Array[HoughLine] = []
  208. 208● let max_rho_f = Float::from_int(max_rho)
  209. 209 for r in 0..<n_rho {
  210. 210● let row_base = r * n_theta
  211. 211 for t in 0..<n_theta {
  212. 212● let votes = accumulator[row_base + t]
  213. 213 if votes >= threshold {
  214. 214● let rho = (Float::from_int(r) - max_rho_f) * rho_resolution
  215. 215● let theta = Float::from_int(t) * angle_scale
  216. 216● lines.push(HoughLine::{ rho, theta, votes, })
  217. 217 }
  218. 218 }
  219. 219 }
  220. 220● lines.sort_by(fn(a, b) { b.votes.compare(a.votes) })
  221. 221 lines
  222. 222}