|
| 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 frequency kernesl |
| 10 | +# [Gabor](https://en.wikipedia.org/wiki/Gabor_filter) and [Log |
| 11 | +# Gabor](https://en.wikipedia.org/wiki/Log_Gabor_filter) to extract image features. |
| 12 | + |
| 13 | +using ImageCore, ImageShow, ImageFiltering # or you could just `using Images` |
| 14 | +using FFTW |
| 15 | +using TestImages |
| 16 | + |
| 17 | +# ## Definition |
| 18 | +# |
| 19 | +# Mathematically, Gabor kernel is defined in frequency space: |
| 20 | +# |
| 21 | +# ```math |
| 22 | +# g(x, y) = \exp(-\frac{x'^2 + \gamma^2y'^2}{2\sigma^2})\exp(i(2\pi\frac{x'}{\lambda} + \psi)) |
| 23 | +# ``` |
| 24 | +# where ``i`` is imaginary unit `Complex(0, 1)`, and |
| 25 | +# ```math |
| 26 | +# x' = x\cos\theta + x\sin\theta \\ |
| 27 | +# y' = -x\sin\theta + y\cos\theta |
| 28 | +# ``` |
| 29 | +# |
| 30 | + |
| 31 | +# First of all, Gabor kernel is defined in frequency space so it is a complex-valued matrix: |
| 32 | + |
| 33 | +bandwidth, orientation, wavelength, phase_offset = 0.1, 0, 2, 0 |
| 34 | +kern = Kernel.Gabor((10, 10); bandwidth, orientation, wavelength, phase_offset) |
| 35 | + |
| 36 | +# !!! tip "Lazy array" |
| 37 | +# The `Gabor` type is a lazy array, which means when you build the Gabor kernel, you |
| 38 | +# actually don't need to allocate any memories. |
| 39 | +# |
| 40 | +# ```julia |
| 41 | +# using BenchmarkTools |
| 42 | +# @btime Kernel.Gabor((64, 64); σ=5, θ=0, λ=1); # 1.705 ns (0 allocations: 0 bytes) |
| 43 | +# ``` |
| 44 | + |
| 45 | +# To explain the parameters of Gabor filter, let's introduce one small helper function to |
| 46 | +# display frequency kernels. |
| 47 | +## You can also try display the real part: `@. Gray(log(abs(real(kern)) + 1))` |
| 48 | +show_phase(kern) = @. Gray(log(abs(imag(kern)) + 1)) |
| 49 | +show_mag(kern) = @. Gray(log(abs(real(kern)) + 1)) |
| 50 | +show_abs(kern) = @. Gray(log(abs(kern) + 1)) |
| 51 | +nothing #hide |
| 52 | + |
| 53 | +# ## Keywords |
| 54 | +# |
| 55 | +# ### `wavelength` (λ) |
| 56 | +# λ specifies the wavelength of the sinusoidal factor. |
| 57 | + |
| 58 | +bandwidth, orientation, phase_offset, aspect_ratio = 1, 0, 0, 0.5 |
| 59 | +f(wavelength) = show_abs(Kernel.Gabor((100, 100); wavelength, bandwidth, orientation, aspect_ratio, phase_offset)) |
| 60 | +mosaic(f.((5, 10, 15)), nrow=1) |
| 61 | + |
| 62 | +# ### `orientation` (θ) |
| 63 | +# θ specifies the orientation of the normal to the parallel stripes of a Gabor function. |
| 64 | + |
| 65 | +wavelength, bandwidth, phase_offset, aspect_ratio = 10, 1, 0, 0.5 |
| 66 | +f(orientation) = show_abs(Kernel.Gabor((100, 100); wavelength, bandwidth, orientation, aspect_ratio, phase_offset)) |
| 67 | +mosaic(f.((0, π/4, π/2)), nrow=1) |
| 68 | + |
| 69 | +# ### `phase_offset` (ψ) |
| 70 | + |
| 71 | +wavelength, bandwidth, orientation, aspect_ratio = 10, 1, 0, 0.5 |
| 72 | +f(phase_offset) = show_phase(Kernel.Gabor((100, 100); wavelength, bandwidth, orientation, aspect_ratio, phase_offset)) |
| 73 | +mosaic(f.((-π/2, 0, π/2, π)), nrow=1) |
| 74 | + |
| 75 | +# ### `aspect_ratio` (γ) |
| 76 | +# γ specifies the ellipticity of the support of the Gabor function. |
| 77 | + |
| 78 | +wavelength, bandwidth, orientation, phase_offset = 10, 1, 0, 0 |
| 79 | +f(aspect_ratio) = show_abs(Kernel.Gabor((100, 100); wavelength, bandwidth, orientation, aspect_ratio, phase_offset)) |
| 80 | +mosaic(f.((0.5, 1, 2)), nrow=1) |
| 81 | + |
| 82 | +# ### `bandwidth` (b) |
| 83 | +# The half-response spatial frequency bandwidth (b) of a Gabor filter is related to the |
| 84 | +# ratio σ / λ, where σ and λ are the standard deviation of the Gaussian factor of the Gabor |
| 85 | +# function and the preferred wavelength, respectively, as follows: |
| 86 | +# |
| 87 | +# ```math |
| 88 | +# b = \log_2\frac{\frac{\sigma}{\lambda}\pi + \sqrt{\frac{\ln 2}{2}}}{\frac{\sigma}{\lambda}\pi - \sqrt{\frac{\ln 2}{2}}} |
| 89 | +# ``` |
| 90 | + |
| 91 | +wavelength, orientation, phase_offset, aspect_ratio = 10, 0, 0, 0.5 |
| 92 | +f(bandwidth) = show_abs(Kernel.Gabor((100, 100); wavelength, bandwidth, orientation, aspect_ratio, phase_offset)) |
| 93 | +mosaic(f.((0.5, 1, 2)), nrow=1) |
| 94 | + |
| 95 | +# ## Examples |
| 96 | +# |
| 97 | + |
| 98 | +# To apply the filter, we need to use [the convolution |
| 99 | +# theorem](https://en.wikipedia.org/wiki/Convolution_theorem): |
| 100 | +# |
| 101 | +# ```math |
| 102 | +# \mathcal{F}(x \circledast k) = \mathcal{F}(x) \odot \mathcal{F}(k) |
| 103 | +# ``` |
| 104 | +# where ``\circledast`` is convolution, ``\odot`` is pointwise-multiplication, and |
| 105 | +# ``\mathcal{F}`` is the fourier transformation. |
| 106 | +# |
| 107 | +# Because Gabor kernel is defined around center point (0, 0), we have to apply `fftshift` |
| 108 | +# first before we do pointwise-multiplication. Because `fftshift(fftshift(x)) == x`, this |
| 109 | +# can be applied to either `fftshift(fft(x))` or `fftshift(kern)`. |
| 110 | + |
| 111 | +img = TestImages.shepp_logan(127) |
| 112 | +kern = Kernel.Gabor(size(img); orientation=π/4, wavelength=20, bandwidth=2, phase_offset=0) |
| 113 | +out = ifft(centered(fft(channelview(img))) .* fftshift(kern)) |
| 114 | +mosaic(img, show_abs(kern), show_abs(out); nrow=1) |
| 115 | + |
| 116 | +# A filter bank is just a list of filter kernels, applying the filter bank generates |
| 117 | +# multiple outputs: |
| 118 | + |
| 119 | +filters = [Kernel.Gabor(size(img); |
| 120 | + orientation, |
| 121 | + wavelength=20, |
| 122 | + bandwidth=50, |
| 123 | + phase_offset=0, |
| 124 | + ) |
| 125 | + for orientation in -1.3:π/4:1.3 |
| 126 | +]; |
| 127 | +f(X, kern) = ifft(centered(fft(channelview(X))) .* fftshift(kern)) |
| 128 | +mosaic( |
| 129 | + map(show_abs, filters)..., |
| 130 | + map(kern->show_abs(f(img, kern)), filters)...; |
| 131 | + nrow=2, rowmajor=true |
| 132 | +) |
| 133 | + |
| 134 | +## save covers #src |
| 135 | +using FileIO #src |
| 136 | +mkpath("assets") #src |
| 137 | +filters = [Kernel.Gabor((32, 32); #src |
| 138 | + orientation, #src |
| 139 | + wavelength=5, #src |
| 140 | + bandwidth=2, #src |
| 141 | + phase_offset=0, #src |
| 142 | + ) #src |
| 143 | + for orientation in range(-π/2, stop=π/2, length=9) #src |
| 144 | +]; #src |
| 145 | +save("assets/gabor.png", mosaic(map(show_mag, filters); nrow=3, npad=2, fillvalue=Gray(1)); fps=2) #src |
0 commit comments