|
| 1 | +# --- |
| 2 | +# title: Gabor filter |
| 3 | +# id: demo_gabor_filter |
| 4 | +# cover: assets/gabor.png |
| 5 | +# author: Johnny Chen |
| 6 | +# date: 2021-11-01 |
| 7 | +# --- |
| 8 | + |
| 9 | +# This example shows how one can apply spatial space kernesl [`Gabor`](@ref Kernel.Gabor) |
| 10 | +# using fourier transformation and convolution theorem to extract image features. |
| 11 | + |
| 12 | +using ImageCore, ImageShow, ImageFiltering # or you could just `using Images` |
| 13 | +using FFTW |
| 14 | +using TestImages |
| 15 | + |
| 16 | +# ## Definition |
| 17 | +# |
| 18 | +# Mathematically, Gabor kernel is defined in spatial space: |
| 19 | +# |
| 20 | +# ```math |
| 21 | +# g(x, y) = \exp(-\frac{x'^2 + \gamma^2y'^2}{2\sigma^2})\exp(i(2\pi\frac{x'}{\lambda} + \psi)) |
| 22 | +# ``` |
| 23 | +# where ``i`` is imaginary unit `Complex(0, 1)`, and |
| 24 | +# ```math |
| 25 | +# x' = x\cos\theta + x\sin\theta \\ |
| 26 | +# y' = -x\sin\theta + y\cos\theta |
| 27 | +# ``` |
| 28 | +# |
| 29 | + |
| 30 | +# First of all, Gabor kernel is a complex-valued matrix: |
| 31 | + |
| 32 | +kern = Kernel.Gabor((10, 10), 2, 0.1) |
| 33 | + |
| 34 | +# !!! tip "Lazy array" |
| 35 | +# The `Gabor` type is a lazy array, which means when you build the Gabor kernel, you |
| 36 | +# actually don't need to allocate any memories. |
| 37 | +# |
| 38 | +# ```julia |
| 39 | +# using BenchmarkTools |
| 40 | +# kern = @btime Kernel.Gabor((64, 64), 5, 0); # 36.481 ns (0 allocations: 0 bytes) |
| 41 | +# @btime collect($kern); # 75.278 μs (2 allocations: 64.05 KiB) |
| 42 | +# ``` |
| 43 | + |
| 44 | +# To explain the parameters of Gabor filter, let's introduce some small helpers function to |
| 45 | +# display complex-valued kernels. |
| 46 | +show_phase(kern) = @. Gray(log(abs(imag(kern)) + 1)) |
| 47 | +show_mag(kern) = @. Gray(log(abs(real(kern)) + 1)) |
| 48 | +show_abs(kern) = @. Gray(log(abs(kern) + 1)) |
| 49 | +nothing #hide |
| 50 | + |
| 51 | +# ## Keywords |
| 52 | +# |
| 53 | +# ### `wavelength` (λ) |
| 54 | +# λ specifies the wavelength of the sinusoidal factor. |
| 55 | + |
| 56 | +bandwidth, orientation, phase_offset, aspect_ratio = 1, 0, 0, 0.5 |
| 57 | +f(wavelength) = show_abs(Kernel.Gabor((100, 100); wavelength, bandwidth, orientation, aspect_ratio, phase_offset)) |
| 58 | +mosaic(f.((5, 10, 15)), nrow=1) |
| 59 | + |
| 60 | +# ### `orientation` (θ) |
| 61 | +# θ specifies the orientation of the normal to the parallel stripes of a Gabor function. |
| 62 | + |
| 63 | +wavelength, bandwidth, phase_offset, aspect_ratio = 10, 1, 0, 0.5 |
| 64 | +f(orientation) = show_abs(Kernel.Gabor((100, 100); wavelength, bandwidth, orientation, aspect_ratio, phase_offset)) |
| 65 | +mosaic(f.((0, π/4, π/2)), nrow=1) |
| 66 | + |
| 67 | +# ### `phase_offset` (ψ) |
| 68 | + |
| 69 | +wavelength, bandwidth, orientation, aspect_ratio = 10, 1, 0, 0.5 |
| 70 | +f(phase_offset) = show_phase(Kernel.Gabor((100, 100); wavelength, bandwidth, orientation, aspect_ratio, phase_offset)) |
| 71 | +mosaic(f.((-π/2, 0, π/2, π)), nrow=1) |
| 72 | + |
| 73 | +# ### `aspect_ratio` (γ) |
| 74 | +# γ specifies the ellipticity of the support of the Gabor function. |
| 75 | + |
| 76 | +wavelength, bandwidth, orientation, phase_offset = 10, 1, 0, 0 |
| 77 | +f(aspect_ratio) = show_abs(Kernel.Gabor((100, 100); wavelength, bandwidth, orientation, aspect_ratio, phase_offset)) |
| 78 | +mosaic(f.((0.5, 1, 2)), nrow=1) |
| 79 | + |
| 80 | +# ### `bandwidth` (b) |
| 81 | +# The half-response spatial frequency bandwidth (b) of a Gabor filter is related to the |
| 82 | +# ratio σ / λ, where σ and λ are the standard deviation of the Gaussian factor of the Gabor |
| 83 | +# function and the preferred wavelength, respectively, as follows: |
| 84 | +# |
| 85 | +# ```math |
| 86 | +# b = \log_2\frac{\frac{\sigma}{\lambda}\pi + \sqrt{\frac{\ln 2}{2}}}{\frac{\sigma}{\lambda}\pi - \sqrt{\frac{\ln 2}{2}}} |
| 87 | +# ``` |
| 88 | + |
| 89 | +wavelength, orientation, phase_offset, aspect_ratio = 10, 0, 0, 0.5 |
| 90 | +f(bandwidth) = show_abs(Kernel.Gabor((100, 100); wavelength, bandwidth, orientation, aspect_ratio, phase_offset)) |
| 91 | +mosaic(f.((0.5, 1, 2)), nrow=1) |
| 92 | + |
| 93 | +# ## Examples |
| 94 | +# |
| 95 | +# There are two options to apply a spatial space kernel: 1) via `imfilter`, and 2) via the |
| 96 | +# convolution theorem. |
| 97 | + |
| 98 | +# ### `imfilter` |
| 99 | +# |
| 100 | +# [`imfilter`](@ref) does not require the kernel size to be the same as the image size. |
| 101 | +# Usually kernel size is at least 5 times larger than the wavelength. |
| 102 | + |
| 103 | +img = TestImages.shepp_logan(127) |
| 104 | +kern = Kernel.Gabor((19, 19), 3, 0) |
| 105 | +out = imfilter(img, real.(kern)) |
| 106 | +mosaic(img, show_abs(kern), show_mag(out); nrow=1) |
| 107 | + |
| 108 | +# ### convolution theorem |
| 109 | + |
| 110 | +# The [convolution theorem](https://en.wikipedia.org/wiki/Convolution_theorem) tells us |
| 111 | +# that `fft(conv(X, K))` is equivalent to `fft(X) .* fft(K)`. Because Gabor kernel is |
| 112 | +# defined with regard to its center point (0, 0), we need to do `ifftshift` first so that |
| 113 | +# the frequency centers of both `fft(X)` and `fft(kern)` align well. |
| 114 | + |
| 115 | +kern = Kernel.Gabor(size(img), 3, 0) |
| 116 | +out = ifft(fft(channelview(img)) .* ifftshift(fft(kern))) |
| 117 | +mosaic(img, show_abs(kern), show_mag(out); nrow=1) |
| 118 | + |
| 119 | +# As you may have notice, using convolution theorem generates different results. This is |
| 120 | +# simply because the kernel size are different. If we create a smaller kernel, we then need |
| 121 | +# to apply [`freqkernel`](@ref) first so that we can do element-wise multiplication. |
| 122 | + |
| 123 | +## freqkernel = zero padding + fftshift + fft |
| 124 | +kern = Kernel.Gabor((19, 19), 3, 0) |
| 125 | +kern_freq = freqkernel(real.(kern), size(img)) |
| 126 | +out = ifft(fft(channelview(img)) .* kern_freq) |
| 127 | +mosaic(img, show_abs(kern), show_mag(out); nrow=1) |
| 128 | + |
| 129 | +# !!! note "Performance on different kernel size" |
| 130 | +# When the kernel size is small, `imfilter` works more efficient than fft-based |
| 131 | +# convolution. This benchmark isn't backed by CI so the result might be outdated, but |
| 132 | +# you get the idea. |
| 133 | +# |
| 134 | +# ```julia |
| 135 | +# using BenchmarkTools |
| 136 | +# img = TestImages.shepp_logan(127); |
| 137 | +# |
| 138 | +# kern = Kernel.Gabor((19, 19), 3, 0); |
| 139 | +# fft_conv(img, kern) = ifft(fft(channelview(img)) .* freqkernel(real.(kern), size(img))) |
| 140 | +# @btime imfilter($img, real.($kern)); # 236.813 μs (118 allocations: 418.91 KiB) |
| 141 | +# @btime fft_conv($img, $kern) # 1.777 ms (127 allocations: 1.61 MiB) |
| 142 | +# |
| 143 | +# kern = Kernel.Gabor(size(img), 3, 0) |
| 144 | +# fft_conv(img, kern) = ifft(fft(channelview(img)) .* ifftshift(fft(kern))) |
| 145 | +# @btime imfilter($img, real.($kern)); # 5.318 ms (163 allocations: 5.28 MiB) |
| 146 | +# @btime fft_conv($img, $kern); # 2.218 ms (120 allocations: 1.73 MiB) |
| 147 | +# ``` |
| 148 | +# |
| 149 | + |
| 150 | +## Filter bank |
| 151 | + |
| 152 | +# A filter bank is just a list of filter kernels, applying the filter bank generates |
| 153 | +# multiple outputs: |
| 154 | + |
| 155 | +filters = [Kernel.Gabor(size(img), 3, θ) for θ in -π/2:π/4:π/2]; |
| 156 | +X_freq = fft(channelview(img)) |
| 157 | +out = map(filters) do kern |
| 158 | + ifft(X_freq .* ifftshift(fft(kern))) |
| 159 | +end |
| 160 | +mosaic( |
| 161 | + map(show_abs, filters)..., |
| 162 | + map(show_abs, out)...; |
| 163 | + nrow=2, rowmajor=true |
| 164 | +) |
| 165 | + |
| 166 | +## save covers #src |
| 167 | +using FileIO #src |
| 168 | +mkpath("assets") #src |
| 169 | +filters = [Kernel.Gabor((32, 32), 5, θ) for θ in range(-π/2, stop=π/2, length=9)] #src |
| 170 | +save("assets/gabor.png", mosaic(map(show_abs, filters); nrow=3, npad=2, fillvalue=Gray(1)); fps=2) #src |
| 171 | + |
| 172 | +# # References |
| 173 | +# |
| 174 | +# - [1] [Wikipedia: Gabor filter](https://en.wikipedia.org/wiki/Gabor_filter) |
| 175 | +# - [2] [Wikipedia: Gabor transformation](https://en.wikipedia.org/wiki/Gabor_transform) |
| 176 | +# - [3] [Wikipedia: Gabor atom](https://en.wikipedia.org/wiki/Gabor_atom) |
| 177 | +# - [4] [Gabor filter for image processing and computer vision](http://matlabserver.cs.rug.nl/edgedetectionweb/web/edgedetection_params.html) |
0 commit comments