image.process\feature\harris.mbt

82.6% · 71 / 86 points · 287 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/// # Harris 角点 harris
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// Harris 角点检测(纯 MoonBit)
  7. 7
  8. 8///|
  9. 9/// 角点信息
  10. 10pub(all) struct CornerPoint {
  11. 11 x : Int32
  12. 12 y : Int32
  13. 13 response : Float // Harris 响应值
  14. 14}
  15. 15
  16. 16///|
  17. 17/// Harris 角点检测。
  18. 18/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4,自动转灰度)。
  19. 19/// k 为 Harris 参数(默认 0.04),threshold 为响应阈值,min_distance 为角点间最小距离。
  20. 20pub fn harris_corners(
  21. 21 img : @types.Image,
  22. 22 k~ : Float = 0.04F,
  23. 23 threshold~ : Float = 1000.0F,
  24. 24 min_distance~ : Int32 = 5,
  25. 25) -> Array[CornerPoint] raise @types.LoadError {
  26. 26 // 输入验证
  27. 27● if img.width <= 0 || img.height <= 0 {
  28. 28● raise @types.LoadError::DecodeFailed(
  29. 29 "harris_corners: 图像尺寸必须大于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 "harris_corners: 仅支持 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 "harris_corners: 数据大小不匹配,期望 " +
  41. 41○ expected_size.to_string() +
  42. 42 ",实际 " +
  43. 43○ img.data.length().to_string(),
  44. 44 )
  45. 45 }
  46. 46 if k <= 0.0F {
  47. 47● raise @types.LoadError::DecodeFailed("harris_corners: k必须大于0")
  48. 48 }
  49. 49 if threshold < 0.0F {
  50. 50● raise @types.LoadError::DecodeFailed("harris_corners: threshold必须 >= 0")
  51. 51 }
  52. 52 if min_distance < 0 {
  53. 53○ raise @types.LoadError::DecodeFailed(
  54. 54 "harris_corners: min_distance必须 >= 0",
  55. 55 )
  56. 56 }
  57. 57 let w = img.width
  58. 58 let h = img.height
  59. 59 let n = w * h
  60. 60 if n == 0 {
  61. 61○ return []
  62. 62 }
  63. 63 // 转灰度
  64. 64● let gray : Array[Float] = Array::make(n, 0.0F)
  65. 65 let ch = img.channels
  66. 66 for i in 0..<n {
  67. 67● let v = if ch == 1 {
  68. 68● img.data[i].to_int()
  69. 69 } else {
  70. 70● let r = img.data[i * ch].to_int()
  71. 71● let g = img.data[i * ch + 1].to_int()
  72. 72● let b = img.data[i * ch + 2].to_int()
  73. 73 (r * 299 + g * 587 + b * 114) / 1000
  74. 74 }
  75. 75● gray[i] = Float::from_int(v)
  76. 76 }
  77. 77 // Sobel 梯度
  78. 78● let ix : Array[Float] = Array::make(n, 0.0F)
  79. 79● let iy : Array[Float] = Array::make(n, 0.0F)
  80. 80 for y in 1..<(h - 1) {
  81. 81● for x in 1..<(w - 1) {
  82. 82● let idx = y * w + x
  83. 83 ix[idx] = (
  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 gray[(y - 1) * w + (x - 1)] -
  88. 88 2.0F * gray[y * w + (x - 1)] -
  89. 89 gray[(y + 1) * w + (x - 1)]
  90. 90 ) /
  91. 91 8.0F
  92. 92 iy[idx] = (
  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 gray[(y - 1) * w + (x - 1)] -
  97. 97 2.0F * gray[(y - 1) * w + x] -
  98. 98 gray[(y - 1) * w + (x + 1)]
  99. 99 ) /
  100. 100 8.0F
  101. 101 }
  102. 102 }
  103. 103 // 计算 Ix², Iy², IxIy
  104. 104● let ixx : Array[Float] = Array::make(n, 0.0F)
  105. 105● let iyy : Array[Float] = Array::make(n, 0.0F)
  106. 106● let ixy : Array[Float] = Array::make(n, 0.0F)
  107. 107 for i in 0..<n {
  108. 108● ixx[i] = ix[i] * ix[i]
  109. 109 iyy[i] = iy[i] * iy[i]
  110. 110 ixy[i] = ix[i] * iy[i]
  111. 111 }
  112. 112 // 高斯加权(sigma=1, 3x3 核)
  113. 113● let sxx = harris_gaussian_blur(ixx, w, h)
  114. 114● let syy = harris_gaussian_blur(iyy, w, h)
  115. 115● let sxy = harris_gaussian_blur(ixy, w, h)
  116. 116 // Harris 响应: R = det(M) - k * trace(M)²
  117. 117● let response : Array[Float] = Array::make(n, 0.0F)
  118. 118 for i in 0..<n {
  119. 119● let det = sxx[i] * syy[i] - sxy[i] * sxy[i]
  120. 120 let trace = sxx[i] + syy[i]
  121. 121 response[i] = det - k * trace * trace
  122. 122 }
  123. 123 // 非极大值抑制 + 阈值
  124. 124 let candidates : Array[CornerPoint] = []
  125. 125 for y in 1..<(h - 1) {
  126. 126● for x in 1..<(w - 1) {
  127. 127● let idx = y * w + x
  128. 128 let r = response[idx]
  129. 129 if r < threshold {
  130. 130● continue
  131. 131 }
  132. 132 // 检查是否为局部最大值
  133. 133 let mut is_max = true
  134. 134 for dy in -1..<=1 {
  135. 135● for dx in -1..<=1 {
  136. 136● if dx == 0 && dy == 0 {
  137. 137● continue
  138. 138 }
  139. 139 if response[(y + dy) * w + (x + dx)] > r {
  140. 140● is_max = false
  141. 141 break
  142. 142 }
  143. 143 }
  144. 144 if !is_max {
  145. 145● break
  146. 146 }
  147. 147 }
  148. 148 if is_max {
  149. 149● candidates.push(CornerPoint::{ x, y, response: r, })
  150. 150 }
  151. 151 }
  152. 152 }
  153. 153 // 按响应值排序(降序)
  154. 154● candidates.sort_by(fn(a, b) { b.response.compare(a.response) })
  155. 155 // 距离过滤
  156. 156 let result : Array[CornerPoint] = []
  157. 157 for c in candidates {
  158. 158● let mut ok = true
  159. 159 for r in result {
  160. 160● let dx = c.x - r.x
  161. 161 let dy = c.y - r.y
  162. 162 if dx * dx + dy * dy < min_distance * min_distance {
  163. 163● ok = false
  164. 164 break
  165. 165 }
  166. 166 }
  167. 167 if ok {
  168. 168● result.push(c)
  169. 169 }
  170. 170 }
  171. 171 result
  172. 172}
  173. 173
  174. 174///|
  175. 175/// 3x3 高斯模糊(sigma=1.0),分离内部/边界区域以消除热点 clamp 调用
  176. 176fn harris_gaussian_blur(
  177. 177 data : Array[Float],
  178. 178 w : Int32,
  179. 179 h : Int32,
  180. 180) -> Array[Float] {
  181. 181● let n = w * h
  182. 182● let out : Array[Float] = Array::make(n, 0.0F)
  183. 183 let kernel : Array[Float] = [
  184. 184 0.0625F, 0.125F, 0.0625F, 0.125F, 0.25F, 0.125F, 0.0625F, 0.125F, 0.0625F,
  185. 185 ]
  186. 186 // 内部区域:x in [1, w-1), y in [1, h-1),3x3 核完全在图像内
  187. 187 for y in 1..<(h - 1) {
  188. 188● for x in 1..<(w - 1) {
  189. 189● let base = (y - 1) * w + (x - 1)
  190. 190 out[y * w + x] = data[base] * kernel[0] +
  191. 191 data[base + 1] * kernel[1] +
  192. 192 data[base + 2] * kernel[2] +
  193. 193 data[base + w] * kernel[3] +
  194. 194 data[base + w + 1] * kernel[4] +
  195. 195 data[base + w + 2] * kernel[5] +
  196. 196 data[base + 2 * w] * kernel[6] +
  197. 197 data[base + 2 * w + 1] * kernel[7] +
  198. 198 data[base + 2 * w + 2] * kernel[8]
  199. 199 }
  200. 200 }
  201. 201 // 边界区域:需要 clamp
  202. 202 for y in 0..<h {
  203. 203● let y_inner = y >= 1 && y < h - 1
  204. 204 for x in 0..<w {
  205. 205● if y_inner && x >= 1 && x < w - 1 {
  206. 206● continue
  207. 207 }
  208. 208 let mut sum = 0.0F
  209. 209 for ky in 0..<3 {
  210. 210● for kx in 0..<3 {
  211. 211● let px = (x + kx - 1).clamp(min=0, max=w - 1)
  212. 212● let py = (y + ky - 1).clamp(min=0, max=h - 1)
  213. 213 sum += data[py * w + px] * kernel[ky * 3 + kx]
  214. 214 }
  215. 215 }
  216. 216 out[y * w + x] = sum
  217. 217 }
  218. 218 }
  219. 219 out
  220. 220}
  221. 221
  222. 222///|
  223. 223/// 在图像上绘制角点。
  224. 224pub fn draw_corners(
  225. 225 img : @types.Image,
  226. 226 corners : Array[CornerPoint],
  227. 227 color~ : Array[Byte] = [b'\xFF', b'\x00', b'\x00'],
  228. 228 radius~ : Int32 = 2,
  229. 229) -> @types.Image raise @types.LoadError {
  230. 230 // 输入验证
  231. 231● if img.width <= 0 || img.height <= 0 {
  232. 232○ raise @types.LoadError::DecodeFailed(
  233. 233 "draw_corners: 图像尺寸必须大于0",
  234. 234 )
  235. 235 }
  236. 236 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  237. 237○ raise @types.LoadError::DecodeFailed(
  238. 238 "draw_corners: 仅支持 channels=1/3/4",
  239. 239 )
  240. 240 }
  241. 241 let expected_size = img.width * img.height * img.channels
  242. 242● if img.data.length() != expected_size {
  243. 243○ raise @types.LoadError::DecodeFailed(
  244. 244 "draw_corners: 数据大小不匹配,期望 " +
  245. 245○ expected_size.to_string() +
  246. 246 ",实际 " +
  247. 247○ img.data.length().to_string(),
  248. 248 )
  249. 249 }
  250. 250● if color.length() == 0 {
  251. 251○ raise @types.LoadError::DecodeFailed(
  252. 252 "draw_corners: color数组不能为空",
  253. 253 )
  254. 254 }
  255. 255 if radius < 0 {
  256. 256○ raise @types.LoadError::DecodeFailed("draw_corners: radius必须 >= 0")
  257. 257 }
  258. 258 let ch = img.channels
  259. 259 let w = img.width
  260. 260 let h = img.height
  261. 261● let out : Array[Byte] = Array::make(w * h * ch, b'\x00')
  262. 262 for i in 0..<(w * h * ch) {
  263. 263● out[i] = img.data[i]
  264. 264 }
  265. 265● let draw_ch = if ch >= 3 { 3 } else { 1 }
  266. 266 for corner in corners {
  267. 267● for dy in -radius..<=radius {
  268. 268● for dx in -radius..<=radius {
  269. 269● let nx = corner.x + dx
  270. 270 let ny = corner.y + dy
  271. 271 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  272. 272● for c in 0..<draw_ch {
  273. 273● if c < color.length() {
  274. 274● out[(ny * w + nx) * ch + c] = color[c]
  275. 275 }
  276. 276 }
  277. 277 }
  278. 278 }
  279. 279 }
  280. 280 }
  281. 281 @types.Image::{
  282. 282 width: w,
  283. 283 height: h,
  284. 284 channels: ch,
  285. 285● data: Bytes::from_array(out),
  286. 286 }
  287. 287}