image.process\feature\gabor.mbt

82.1% · 92 / 112 points · 318 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/// # Gabor 滤波 gabor
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// Gabor 滤波(纯 MoonBit)
  7. 7
  8. 8///|
  9. 9/// Gabor 滤波器。
  10. 10/// Gabor 核函数 g(x,y) = exp(-(x'²+γ²y'²)/(2σ²)) * cos(2πx'/λ + φ)
  11. 11/// 其中 x' = x*cos(θ) + y*sin(θ), y' = -x*sin(θ) + y*cos(θ)
  12. 12/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4)。
  13. 13/// ksize 为核大小(奇数),sigma 为标准差,theta 为方向(弧度),
  14. 14/// lambda 为波长,gamma 为空间纵横比(默认 0.5),phi 为相位偏移(默认 0)。
  15. 15pub fn gabor_filter(
  16. 16 img : @types.Image,
  17. 17 ksize : Int32,
  18. 18 sigma : Float,
  19. 19 theta : Float,
  20. 20 lambda~ : Float = 10.0F,
  21. 21 gamma~ : Float = 0.5F,
  22. 22 phi~ : Float = 0.0F,
  23. 23) -> @types.Image raise @types.LoadError {
  24. 24 // 输入验证
  25. 25● if img.width <= 0 || img.height <= 0 {
  26. 26● raise @types.LoadError::DecodeFailed(
  27. 27 "gabor_filter: 图像尺寸必须大于0",
  28. 28 )
  29. 29 }
  30. 30 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  31. 31○ raise @types.LoadError::DecodeFailed(
  32. 32 "gabor_filter: 仅支持 channels=1/3/4",
  33. 33 )
  34. 34 }
  35. 35 let expected_size = img.width * img.height * img.channels
  36. 36● if img.data.length() != expected_size {
  37. 37○ raise @types.LoadError::DecodeFailed(
  38. 38 "gabor_filter: 数据大小不匹配,期望 " +
  39. 39○ expected_size.to_string() +
  40. 40 ",实际 " +
  41. 41○ img.data.length().to_string(),
  42. 42 )
  43. 43 }
  44. 44 if gamma <= 0.0F {
  45. 45○ raise @types.LoadError::DecodeFailed("gabor_filter: gamma必须大于0")
  46. 46 }
  47. 47 if ksize < 3 || ksize > 31 || ksize % 2 == 0 {
  48. 48● raise @types.LoadError::DecodeFailed(
  49. 49 "ksize 必须为 3..31 之间的奇数",
  50. 50 )
  51. 51 }
  52. 52 if sigma <= 0.0F {
  53. 53● raise @types.LoadError::DecodeFailed("sigma 必须 > 0")
  54. 54 }
  55. 55 if lambda <= 0.0F {
  56. 56● raise @types.LoadError::DecodeFailed("lambda 必须 > 0")
  57. 57 }
  58. 58 let ch = img.channels
  59. 59 let w = img.width
  60. 60 let h = img.height
  61. 61 let n = w * h
  62. 62 if n == 0 {
  63. 63○ return img
  64. 64 }
  65. 65 // 生成 Gabor 核
  66. 66 let half = ksize / 2
  67. 67● let kernel : Array[Float] = Array::make(ksize * ksize, 0.0F)
  68. 68● let cos_t = @math.cosf(theta)
  69. 69● let sin_t = @math.sinf(theta)
  70. 70 for ky in 0..<ksize {
  71. 71● for kx in 0..<ksize {
  72. 72● let x = Float::from_int(kx - half)
  73. 73● let y = Float::from_int(ky - half)
  74. 74 let x_prime = x * cos_t + y * sin_t
  75. 75 let y_prime = -x * sin_t + y * cos_t
  76. 76● let gaussian = @math.expf(
  77. 77 -(x_prime * x_prime + gamma * gamma * y_prime * y_prime) /
  78. 78 (2.0F * sigma * sigma),
  79. 79 )
  80. 80● let wave = @math.cosf(2.0F * 3.14159265F * x_prime / lambda + phi)
  81. 81 let val = gaussian * wave
  82. 82 kernel[ky * ksize + kx] = val
  83. 83 }
  84. 84 }
  85. 85 // 预计算像素 Float 数组,避免内层循环重复类型转换
  86. 86● let src : Array[Float] = Array::make(n * ch, 0.0F)
  87. 87 for i in 0..<(n * ch) {
  88. 88● src[i] = Float::from_int(img.data[i].to_int())
  89. 89 }
  90. 90 // 卷积,分离内部/边界区域,ch=1 特化消除内层通道循环
  91. 91● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  92. 92 if ch == 1 {
  93. 93 // 内部区域
  94. 94● for y in half..<(h - half) {
  95. 95● for x in half..<(w - half) {
  96. 96● let mut sum = 0.0F
  97. 97 let base = (y - half) * w + (x - half)
  98. 98 for ky in 0..<ksize {
  99. 99● let row = base + ky * w
  100. 100 let krow = ky * ksize
  101. 101 for kx in 0..<ksize {
  102. 102● sum += src[row + kx] * kernel[krow + kx]
  103. 103 }
  104. 104 }
  105. 105● let val = sum.to_int()
  106. 106● out[y * w + x] = val.clamp(min=0, max=255).to_byte()
  107. 107 }
  108. 108 }
  109. 109 // 边界区域
  110. 110 for y in 0..<h {
  111. 111● let y_inner = y >= half && y < h - half
  112. 112 for x in 0..<w {
  113. 113● if y_inner && x >= half && x < w - half {
  114. 114● continue
  115. 115 }
  116. 116 let mut sum = 0.0F
  117. 117 for ky in 0..<ksize {
  118. 118● for kx in 0..<ksize {
  119. 119● let px = (x + kx - half).clamp(min=0, max=w - 1)
  120. 120● let py = (y + ky - half).clamp(min=0, max=h - 1)
  121. 121 sum += src[py * w + px] * kernel[ky * ksize + kx]
  122. 122 }
  123. 123 }
  124. 124● let val = sum.to_int()
  125. 125● out[y * w + x] = val.clamp(min=0, max=255).to_byte()
  126. 126 }
  127. 127 }
  128. 128 } else {
  129. 129● for c in 0..<ch {
  130. 130● if ch == 4 && c == 3 {
  131. 131● for i in 0..<n {
  132. 132● out[i * ch + 3] = img.data[i * ch + 3]
  133. 133 }
  134. 134 continue
  135. 135 }
  136. 136 // 内部区域
  137. 137 for y in half..<(h - half) {
  138. 138● for x in half..<(w - half) {
  139. 139● let mut sum = 0.0F
  140. 140 let base = (y - half) * w + (x - half)
  141. 141 for ky in 0..<ksize {
  142. 142● let row = (base + ky * w) * ch + c
  143. 143 let krow = ky * ksize
  144. 144 for kx in 0..<ksize {
  145. 145● sum += src[row + kx * ch] * kernel[krow + kx]
  146. 146 }
  147. 147 }
  148. 148● let val = sum.to_int()
  149. 149● out[(y * w + x) * ch + c] = val.clamp(min=0, max=255).to_byte()
  150. 150 }
  151. 151 }
  152. 152 // 边界区域
  153. 153 for y in 0..<h {
  154. 154● let y_inner = y >= half && y < h - half
  155. 155 for x in 0..<w {
  156. 156● if y_inner && x >= half && x < w - half {
  157. 157● continue
  158. 158 }
  159. 159 let mut sum = 0.0F
  160. 160 for ky in 0..<ksize {
  161. 161● for kx in 0..<ksize {
  162. 162● let px = (x + kx - half).clamp(min=0, max=w - 1)
  163. 163● let py = (y + ky - half).clamp(min=0, max=h - 1)
  164. 164 sum += src[(py * w + px) * ch + c] * kernel[ky * ksize + kx]
  165. 165 }
  166. 166 }
  167. 167● let val = sum.to_int()
  168. 168● out[(y * w + x) * ch + c] = val.clamp(min=0, max=255).to_byte()
  169. 169 }
  170. 170 }
  171. 171 }
  172. 172 }
  173. 173 @types.Image::{
  174. 174 width: w,
  175. 175 height: h,
  176. 176 channels: ch,
  177. 177● data: Bytes::from_array(out),
  178. 178 }
  179. 179}
  180. 180
  181. 181///|
  182. 182/// Gabor 滤波器组(多方向)。
  183. 183/// 对图像应用多个方向的 Gabor 滤波器,返回每个方向的响应幅值之和。
  184. 184/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4)。
  185. 185/// num_orientations 为方向数(默认 4,即 0°/45°/90°/135°)。
  186. 186pub fn gabor_filter_bank(
  187. 187 img : @types.Image,
  188. 188 ksize : Int32,
  189. 189 sigma : Float,
  190. 190 num_orientations~ : Int32 = 4,
  191. 191 lambda~ : Float = 10.0F,
  192. 192 gamma~ : Float = 0.5F,
  193. 193) -> @types.Image raise @types.LoadError {
  194. 194 // 输入验证
  195. 195● if img.width <= 0 || img.height <= 0 {
  196. 196○ raise @types.LoadError::DecodeFailed(
  197. 197 "gabor_filter_bank: 图像尺寸必须大于0",
  198. 198 )
  199. 199 }
  200. 200 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  201. 201○ raise @types.LoadError::DecodeFailed(
  202. 202 "gabor_filter_bank: 仅支持 channels=1/3/4",
  203. 203 )
  204. 204 }
  205. 205 let expected_size = img.width * img.height * img.channels
  206. 206● if img.data.length() != expected_size {
  207. 207○ raise @types.LoadError::DecodeFailed(
  208. 208 "gabor_filter_bank: 数据大小不匹配,期望 " +
  209. 209○ expected_size.to_string() +
  210. 210 ",实际 " +
  211. 211○ img.data.length().to_string(),
  212. 212 )
  213. 213 }
  214. 214 if ksize < 3 || ksize > 31 || ksize % 2 == 0 {
  215. 215○ raise @types.LoadError::DecodeFailed(
  216. 216 "gabor_filter_bank: ksize必须为3..31之间的奇数",
  217. 217 )
  218. 218 }
  219. 219 if sigma <= 0.0F {
  220. 220○ raise @types.LoadError::DecodeFailed(
  221. 221 "gabor_filter_bank: sigma必须大于0",
  222. 222 )
  223. 223 }
  224. 224 if lambda <= 0.0F {
  225. 225○ raise @types.LoadError::DecodeFailed(
  226. 226 "gabor_filter_bank: lambda必须大于0",
  227. 227 )
  228. 228 }
  229. 229 if gamma <= 0.0F {
  230. 230○ raise @types.LoadError::DecodeFailed(
  231. 231 "gabor_filter_bank: gamma必须大于0",
  232. 232 )
  233. 233 }
  234. 234 if num_orientations < 1 || num_orientations > 16 {
  235. 235● raise @types.LoadError::DecodeFailed(
  236. 236 "num_orientations 必须在 1..16 之间",
  237. 237 )
  238. 238 }
  239. 239 let ch = img.channels
  240. 240 let w = img.width
  241. 241 let h = img.height
  242. 242 let n = w * h
  243. 243 if n == 0 {
  244. 244○ return img
  245. 245 }
  246. 246 // 累加各方向响应
  247. 247● let accum : Array[Float] = Array::make(n * ch, 0.0F)
  248. 248 for i in 0..<num_orientations {
  249. 249● let theta = Float::from_int(i) *
  250. 250 3.14159265F /
  251. 251● Float::from_int(num_orientations)
  252. 252● let filtered = gabor_filter(img, ksize, sigma, theta, lambda~, gamma~)
  253. 253 for j in 0..<(n * ch) {
  254. 254● accum[j] += Float::from_int(filtered.data[j].to_int())
  255. 255 }
  256. 256 }
  257. 257 // 归一化
  258. 258● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  259. 259 for j in 0..<(n * ch) {
  260. 260● let val = (accum[j] / Float::from_int(num_orientations)).to_int()
  261. 261● out[j] = val.clamp(min=0, max=255).to_byte()
  262. 262 }
  263. 263 @types.Image::{
  264. 264 width: w,
  265. 265 height: h,
  266. 266 channels: ch,
  267. 267● data: Bytes::from_array(out),
  268. 268 }
  269. 269}
  270. 270
  271. 271///|
  272. 272/// 生成 Gabor 核(返回核矩阵,用于可视化或调试)。
  273. 273pub fn gabor_kernel(
  274. 274 ksize : Int32,
  275. 275 sigma : Float,
  276. 276 theta : Float,
  277. 277 lambda~ : Float = 10.0F,
  278. 278 gamma~ : Float = 0.5F,
  279. 279 phi~ : Float = 0.0F,
  280. 280) -> Array[Array[Float]] raise @types.LoadError {
  281. 281 // 参数验证
  282. 282● if ksize < 3 || ksize > 31 || ksize % 2 == 0 {
  283. 283● raise @types.LoadError::DecodeFailed(
  284. 284 "gabor_kernel: ksize必须为3..31之间的奇数",
  285. 285 )
  286. 286 }
  287. 287 if sigma <= 0.0F {
  288. 288● raise @types.LoadError::DecodeFailed("gabor_kernel: sigma必须大于0")
  289. 289 }
  290. 290 if lambda <= 0.0F {
  291. 291○ raise @types.LoadError::DecodeFailed("gabor_kernel: lambda必须大于0")
  292. 292 }
  293. 293 if gamma <= 0.0F {
  294. 294○ raise @types.LoadError::DecodeFailed("gabor_kernel: gamma必须大于0")
  295. 295 }
  296. 296 let half = ksize / 2
  297. 297● let kernel : Array[Array[Float]] = Array::make(ksize, [])
  298. 298 for i in 0..<ksize {
  299. 299● kernel[i] = Array::make(ksize, 0.0F)
  300. 300 }
  301. 301● let cos_t = @math.cosf(theta)
  302. 302● let sin_t = @math.sinf(theta)
  303. 303 for ky in 0..<ksize {
  304. 304● for kx in 0..<ksize {
  305. 305● let x = Float::from_int(kx - half)
  306. 306● let y = Float::from_int(ky - half)
  307. 307 let x_prime = x * cos_t + y * sin_t
  308. 308 let y_prime = -x * sin_t + y * cos_t
  309. 309● let gaussian = @math.expf(
  310. 310 -(x_prime * x_prime + gamma * gamma * y_prime * y_prime) /
  311. 311 (2.0F * sigma * sigma),
  312. 312 )
  313. 313● let wave = @math.cosf(2.0F * 3.14159265F * x_prime / lambda + phi)
  314. 314 kernel[ky][kx] = gaussian * wave
  315. 315 }
  316. 316 }
  317. 317 kernel
  318. 318}