image.process\feature\shi_tomasi.mbt

85.7% · 60 / 70 points · 229 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/// # Shi-Tomasi 角点 shi tomasi
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// Shi-Tomasi 角点检测 (good_features_to_track)
  7. 7// 基于 Harris 的结构张量,用 min eigenvalue 代替 Harris 响应
  8. 8
  9. 9///|
  10. 10/// Shi-Tomasi 角点检测(goodFeaturesToTrack)。
  11. 11/// 使用结构张量的最小特征值作为响应:R = min(λ1, λ2)。
  12. 12/// 适用于光流跟踪的前置角点检测。
  13. 13pub fn good_features_to_track(
  14. 14 img : @types.Image,
  15. 15 max_corners : Int32,
  16. 16 quality_level : Float,
  17. 17 min_distance~ : Int32 = 5,
  18. 18) -> Array[CornerPoint] raise @types.LoadError {
  19. 19 // 输入验证
  20. 20● if img.width <= 0 || img.height <= 0 {
  21. 21● raise @types.LoadError::DecodeFailed(
  22. 22 "good_features_to_track: 图像尺寸必须大于0",
  23. 23 )
  24. 24 }
  25. 25 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  26. 26○ raise @types.LoadError::DecodeFailed(
  27. 27 "good_features_to_track: 仅支持 channels=1/3/4",
  28. 28 )
  29. 29 }
  30. 30 let expected_size = img.width * img.height * img.channels
  31. 31● if img.data.length() != expected_size {
  32. 32○ raise @types.LoadError::DecodeFailed(
  33. 33 "good_features_to_track: 数据大小不匹配,期望 " +
  34. 34○ expected_size.to_string() +
  35. 35 ",实际 " +
  36. 36○ img.data.length().to_string(),
  37. 37 )
  38. 38 }
  39. 39 if max_corners <= 0 {
  40. 40○ raise @types.LoadError::DecodeFailed(
  41. 41 "good_features_to_track: max_corners必须大于0",
  42. 42 )
  43. 43 }
  44. 44 if quality_level <= 0.0F || quality_level > 1.0F {
  45. 45○ raise @types.LoadError::DecodeFailed(
  46. 46 "good_features_to_track: quality_level必须在(0, 1]范围内",
  47. 47 )
  48. 48 }
  49. 49 if min_distance < 0 {
  50. 50○ raise @types.LoadError::DecodeFailed(
  51. 51 "good_features_to_track: min_distance必须 >= 0",
  52. 52 )
  53. 53 }
  54. 54 let w = img.width
  55. 55 let h = img.height
  56. 56 let n = w * h
  57. 57 if n == 0 {
  58. 58○ return []
  59. 59 }
  60. 60 // 转灰度
  61. 61● let gray : Array[Float] = Array::make(n, 0.0F)
  62. 62 let ch = img.channels
  63. 63 for i in 0..<n {
  64. 64● let v = if ch == 1 {
  65. 65● img.data[i].to_int()
  66. 66 } else {
  67. 67● let r = img.data[i * ch].to_int()
  68. 68● let g = img.data[i * ch + 1].to_int()
  69. 69● let b = img.data[i * ch + 2].to_int()
  70. 70 (r * 299 + g * 587 + b * 114) / 1000
  71. 71 }
  72. 72● gray[i] = Float::from_int(v)
  73. 73 }
  74. 74 // Sobel 梯度
  75. 75● let ix : Array[Float] = Array::make(n, 0.0F)
  76. 76● let iy : Array[Float] = Array::make(n, 0.0F)
  77. 77 for y in 1..<(h - 1) {
  78. 78● for x in 1..<(w - 1) {
  79. 79● let idx = y * w + x
  80. 80 ix[idx] = (
  81. 81 gray[(y - 1) * w + (x + 1)] +
  82. 82 2.0F * gray[y * w + (x + 1)] +
  83. 83 gray[(y + 1) * w + (x + 1)] -
  84. 84 gray[(y - 1) * w + (x - 1)] -
  85. 85 2.0F * gray[y * w + (x - 1)] -
  86. 86 gray[(y + 1) * w + (x - 1)]
  87. 87 ) /
  88. 88 8.0F
  89. 89 iy[idx] = (
  90. 90 gray[(y + 1) * w + (x - 1)] +
  91. 91 2.0F * gray[(y + 1) * w + x] +
  92. 92 gray[(y + 1) * w + (x + 1)] -
  93. 93 gray[(y - 1) * w + (x - 1)] -
  94. 94 2.0F * gray[(y - 1) * w + x] -
  95. 95 gray[(y - 1) * w + (x + 1)]
  96. 96 ) /
  97. 97 8.0F
  98. 98 }
  99. 99 }
  100. 100 // 结构张量分量
  101. 101● let ixx : Array[Float] = Array::make(n, 0.0F)
  102. 102● let iyy : Array[Float] = Array::make(n, 0.0F)
  103. 103● let ixy : Array[Float] = Array::make(n, 0.0F)
  104. 104 for i in 0..<n {
  105. 105● ixx[i] = ix[i] * ix[i]
  106. 106 iyy[i] = iy[i] * iy[i]
  107. 107 ixy[i] = ix[i] * iy[i]
  108. 108 }
  109. 109 // 高斯加权
  110. 110● let sxx = shi_tomasi_gaussian_blur(ixx, w, h)
  111. 111● let syy = shi_tomasi_gaussian_blur(iyy, w, h)
  112. 112● let sxy = shi_tomasi_gaussian_blur(ixy, w, h)
  113. 113 // Shi-Tomasi 响应: R = min(λ1, λ2) = (trace - sqrt(trace² - 4*det)) / 2
  114. 114● let response : Array[Float] = Array::make(n, 0.0F)
  115. 115 for i in 0..<n {
  116. 116● let det = sxx[i] * syy[i] - sxy[i] * sxy[i]
  117. 117 let trace = sxx[i] + syy[i]
  118. 118 let disc = trace * trace - 4.0F * det
  119. 119 if disc < 0.0F {
  120. 120○ response[i] = 0.0F
  121. 121 } else {
  122. 122● response[i] = (trace - disc.sqrt()) / 2.0F
  123. 123 }
  124. 124 }
  125. 125 // 找最大响应值用于 quality threshold
  126. 126 let mut max_response = 0.0F
  127. 127 for i in 0..<n {
  128. 128● if response[i] > max_response {
  129. 129● max_response = response[i]
  130. 130 }
  131. 131 }
  132. 132 let threshold = max_response * quality_level
  133. 133 // 非极大值抑制 + 阈值
  134. 134 let candidates : Array[CornerPoint] = []
  135. 135 for y in 1..<(h - 1) {
  136. 136● for x in 1..<(w - 1) {
  137. 137● let idx = y * w + x
  138. 138 let r = response[idx]
  139. 139 if r <= threshold {
  140. 140● continue
  141. 141 }
  142. 142 let mut is_max = true
  143. 143 for dy in -1..<=1 {
  144. 144● for dx in -1..<=1 {
  145. 145● if dx == 0 && dy == 0 {
  146. 146● continue
  147. 147 }
  148. 148 if response[(y + dy) * w + (x + dx)] > r {
  149. 149● is_max = false
  150. 150 break
  151. 151 }
  152. 152 }
  153. 153 if !is_max {
  154. 154● break
  155. 155 }
  156. 156 }
  157. 157 if is_max {
  158. 158● candidates.push(CornerPoint::{ x, y, response: r, })
  159. 159 }
  160. 160 }
  161. 161 }
  162. 162 // 按响应值排序(降序)
  163. 163● candidates.sort_by(fn(a, b) { b.response.compare(a.response) })
  164. 164 // 距离过滤
  165. 165 let result : Array[CornerPoint] = []
  166. 166 for c in candidates {
  167. 167● if result.length() >= max_corners {
  168. 168● break
  169. 169 }
  170. 170 let mut ok = true
  171. 171 for r in result {
  172. 172● let dx = c.x - r.x
  173. 173 let dy = c.y - r.y
  174. 174 if dx * dx + dy * dy < min_distance * min_distance {
  175. 175● ok = false
  176. 176 break
  177. 177 }
  178. 178 }
  179. 179 if ok {
  180. 180● result.push(c)
  181. 181 }
  182. 182 }
  183. 183 result
  184. 184}
  185. 185
  186. 186///|
  187. 187fn shi_tomasi_gaussian_blur(
  188. 188 data : Array[Float],
  189. 189 w : Int32,
  190. 190 h : Int32,
  191. 191) -> Array[Float] {
  192. 192● let n = w * h
  193. 193● let out : Array[Float] = Array::make(n, 0.0F)
  194. 194 let kernel : Array[Float] = [
  195. 195 0.0625F, 0.125F, 0.0625F, 0.125F, 0.25F, 0.125F, 0.0625F, 0.125F, 0.0625F,
  196. 196 ]
  197. 197 for y in 1..<(h - 1) {
  198. 198● for x in 1..<(w - 1) {
  199. 199● let base = (y - 1) * w + (x - 1)
  200. 200 out[y * w + x] = data[base] * kernel[0] +
  201. 201 data[base + 1] * kernel[1] +
  202. 202 data[base + 2] * kernel[2] +
  203. 203 data[base + w] * kernel[3] +
  204. 204 data[base + w + 1] * kernel[4] +
  205. 205 data[base + w + 2] * kernel[5] +
  206. 206 data[base + 2 * w] * kernel[6] +
  207. 207 data[base + 2 * w + 1] * kernel[7] +
  208. 208 data[base + 2 * w + 2] * kernel[8]
  209. 209 }
  210. 210 }
  211. 211 for y in 0..<h {
  212. 212● let y_inner = y >= 1 && y < h - 1
  213. 213 for x in 0..<w {
  214. 214● if y_inner && x >= 1 && x < w - 1 {
  215. 215● continue
  216. 216 }
  217. 217 let mut sum = 0.0F
  218. 218 for ky in 0..<3 {
  219. 219● for kx in 0..<3 {
  220. 220● let px = (x + kx - 1).clamp(min=0, max=w - 1)
  221. 221● let py = (y + ky - 1).clamp(min=0, max=h - 1)
  222. 222 sum += data[py * w + px] * kernel[ky * 3 + kx]
  223. 223 }
  224. 224 }
  225. 225 out[y * w + x] = sum
  226. 226 }
  227. 227 }
  228. 228 out
  229. 229}