image.process\frequency\freq_filter.mbt

77.5% · 55 / 71 points · 216 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/// # 频域滤波 freq filter
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 频域滤波(基于 FFT)
  7. 7
  8. 8///|
  9. 9/// 频域滤波类型
  10. 10pub(all) enum FreqFilterType {
  11. 11 LowPass // 低通滤波(保留低频,去除高频)
  12. 12 HighPass // 高通滤波(保留高频,去除低频)
  13. 13 BandPass // 带通滤波(保留中频)
  14. 14 BandStop // 带阻滤波(去除中频)
  15. 15}
  16. 16
  17. 17///|
  18. 18/// 频域滤波。对灰度图做 FFT,应用指定滤波器,再做 IFFT 回空域。
  19. 19/// cutoff 为截止频率(像素数,从中心算起),band_width 为带宽(仅 BandPass/BandStop 使用)。
  20. 20pub fn freq_filter(
  21. 21 img : @types.Image,
  22. 22 filter_type : FreqFilterType,
  23. 23 cutoff : Float,
  24. 24 band_width~ : Float = 0.0F,
  25. 25) -> @types.Image raise @types.LoadError {
  26. 26● if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  27. 27● raise @types.LoadError::DecodeFailed("freq_filter 仅支持 channels=1/3/4")
  28. 28 }
  29. 29 let ch = img.channels
  30. 30● let color_ch = if ch >= 3 { 3 } else { 1 }
  31. 31● let ffts = fft_2d(img)
  32. 32 let filtered : Array[FFTResult] = []
  33. 33 for c in 0..<color_ch {
  34. 34● fft_shift_inplace(ffts[c])
  35. 35 let shifted = ffts[c]
  36. 36 let w = shifted.width
  37. 37 let h = shifted.height
  38. 38 let cx = w / 2
  39. 39 let cy = h / 2
  40. 40 let n = w * h
  41. 41● let out_data : Array[Complex] = Array::make(n, Complex::{
  42. 42 re: 0.0F,
  43. 43 im: 0.0F,
  44. 44 })
  45. 45 let data = shifted.data
  46. 46 for y in 0..<h {
  47. 47● let dy = y - cy
  48. 48 let row = y * w
  49. 49 for x in 0..<w {
  50. 50● let dx = x - cx
  51. 51● let dist = Float::from_int(dx * dx + dy * dy).sqrt()
  52. 52● let gain = filter_gain(filter_type, dist, cutoff, band_width)
  53. 53 let src = data[row + x]
  54. 54 out_data[row + x] = Complex::{ re: src.re * gain, im: src.im * gain, }
  55. 55 }
  56. 56 }
  57. 57 let filtered_fft = FFTResult::{ width: w, height: h, data: out_data, }
  58. 58● fft_shift_inplace(filtered_fft)
  59. 59● filtered.push(filtered_fft)
  60. 60 }
  61. 61 if color_ch == 1 {
  62. 62● return ifft_2d(filtered[0])
  63. 63 }
  64. 64 let pw = filtered[0].width
  65. 65 let ph = filtered[0].height
  66. 66 let n = pw * ph
  67. 67● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  68. 68 for c in 0..<color_ch {
  69. 69● let channel_img = ifft_2d(filtered[c])
  70. 70 for i in 0..<n {
  71. 71● out[i * ch + c] = channel_img.data[i]
  72. 72 }
  73. 73 }
  74. 74 if ch == 4 {
  75. 75○ for i in 0..<n {
  76. 76○ out[i * 4 + 3] = img.data[i * 4 + 3]
  77. 77 }
  78. 78 }
  79. 79 @types.Image::{
  80. 80 width: pw,
  81. 81 height: ph,
  82. 82 channels: ch,
  83. 83● data: Bytes::from_array(out),
  84. 84 }
  85. 85}
  86. 86
  87. 87///|
  88. 88/// 理想滤波器增益
  89. 89fn filter_gain(
  90. 90 filter_type : FreqFilterType,
  91. 91 dist : Float,
  92. 92 cutoff : Float,
  93. 93 band_width : Float,
  94. 94) -> Float {
  95. 95● match filter_type {
  96. 96● LowPass => if dist <= cutoff { 1.0F } else { 0.0F }
  97. 97● HighPass => if dist >= cutoff { 1.0F } else { 0.0F }
  98. 98● BandPass => {
  99. 99 let lo = cutoff - band_width / 2.0F
  100. 100 let hi = cutoff + band_width / 2.0F
  101. 101 if dist >= lo && dist <= hi {
  102. 102● 1.0F
  103. 103 } else {
  104. 104● 0.0F
  105. 105 }
  106. 106 }
  107. 107● BandStop => {
  108. 108 let lo = cutoff - band_width / 2.0F
  109. 109 let hi = cutoff + band_width / 2.0F
  110. 110 if dist >= lo && dist <= hi {
  111. 111● 0.0F
  112. 112 } else {
  113. 113● 1.0F
  114. 114 }
  115. 115 }
  116. 116 }
  117. 117}
  118. 118
  119. 119///|
  120. 120/// 高斯频域滤波。使用高斯传递函数而非理想滤波器,更平滑。
  121. 121pub fn freq_filter_gaussian(
  122. 122 img : @types.Image,
  123. 123 filter_type : FreqFilterType,
  124. 124 cutoff : Float,
  125. 125 band_width~ : Float = 0.0F,
  126. 126) -> @types.Image raise @types.LoadError {
  127. 127● if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  128. 128● raise @types.LoadError::DecodeFailed(
  129. 129 "freq_filter_gaussian 仅支持 channels=1/3/4",
  130. 130 )
  131. 131 }
  132. 132 let ch = img.channels
  133. 133◐ let color_ch = if ch >= 3 { 3 } else { 1 }
  134. 134● let ffts = fft_2d(img)
  135. 135 let filtered : Array[FFTResult] = []
  136. 136 for c in 0..<color_ch {
  137. 137● fft_shift_inplace(ffts[c])
  138. 138 let shifted = ffts[c]
  139. 139 let w = shifted.width
  140. 140 let h = shifted.height
  141. 141 let cx = w / 2
  142. 142 let cy = h / 2
  143. 143 let n = w * h
  144. 144● let out_data : Array[Complex] = Array::make(n, Complex::{
  145. 145 re: 0.0F,
  146. 146 im: 0.0F,
  147. 147 })
  148. 148 let data = shifted.data
  149. 149 for y in 0..<h {
  150. 150● let dy = y - cy
  151. 151 let row = y * w
  152. 152 for x in 0..<w {
  153. 153● let dx = x - cx
  154. 154● let dist = Float::from_int(dx * dx + dy * dy).sqrt()
  155. 155● let gain = gaussian_gain(filter_type, dist, cutoff, band_width)
  156. 156 let src = data[row + x]
  157. 157 out_data[row + x] = Complex::{ re: src.re * gain, im: src.im * gain, }
  158. 158 }
  159. 159 }
  160. 160 let filtered_fft = FFTResult::{ width: w, height: h, data: out_data, }
  161. 161● fft_shift_inplace(filtered_fft)
  162. 162● filtered.push(filtered_fft)
  163. 163 }
  164. 164 if color_ch == 1 {
  165. 165● return ifft_2d(filtered[0])
  166. 166 }
  167. 167 let pw = filtered[0].width
  168. 168 let ph = filtered[0].height
  169. 169 let n = pw * ph
  170. 170○ let out : Array[Byte] = Array::make(n * ch, b'\x00')
  171. 171 for c in 0..<color_ch {
  172. 172○ let channel_img = ifft_2d(filtered[c])
  173. 173 for i in 0..<n {
  174. 174○ out[i * ch + c] = channel_img.data[i]
  175. 175 }
  176. 176 }
  177. 177 if ch == 4 {
  178. 178○ for i in 0..<n {
  179. 179○ out[i * 4 + 3] = img.data[i * 4 + 3]
  180. 180 }
  181. 181 }
  182. 182 @types.Image::{
  183. 183 width: pw,
  184. 184 height: ph,
  185. 185 channels: ch,
  186. 186○ data: Bytes::from_array(out),
  187. 187 }
  188. 188}
  189. 189
  190. 190///|
  191. 191/// 高斯传递函数增益
  192. 192fn gaussian_gain(
  193. 193 filter_type : FreqFilterType,
  194. 194 dist : Float,
  195. 195 cutoff : Float,
  196. 196 band_width : Float,
  197. 197) -> Float {
  198. 198● let eps = 0.0001F
  199. 199 let c2 = cutoff * cutoff
  200. 200● match filter_type {
  201. 201● LowPass => @math.expf(-(dist * dist) / (c2 + eps))
  202. 202● HighPass => 1.0F - @math.expf(-(dist * dist) / (c2 + eps))
  203. 203○ BandPass => {
  204. 204○ let lp = @math.expf(-(dist * dist) / (c2 + eps))
  205. 205 let lo = cutoff - band_width / 2.0F
  206. 206○ let lp2 = @math.expf(-(dist * dist) / (lo * lo + eps))
  207. 207 lp2 - lp
  208. 208 }
  209. 209○ BandStop => {
  210. 210○ let lp = @math.expf(-(dist * dist) / (c2 + eps))
  211. 211 let lo = cutoff - band_width / 2.0F
  212. 212○ let lp2 = @math.expf(-(dist * dist) / (lo * lo + eps))
  213. 213 1.0F - (lp2 - lp)
  214. 214 }
  215. 215 }
  216. 216}