Gabor filter¶
| Fact | Value |
|---|---|
| Also called | Gabor kernel, Gabor wavelet |
| Status | Implemented and tested |
| Family | Linear spatial filters |
| Engine | Browser |
| Type | Linear, spatial, complex correlation |
| Output | Image |
| Operator shown as | dynamic-kernel (exact) |
| Support radius | ceil(3 max(sigma, sigma / aspect)) |
| Code | web/src/core/filters/linear/gabor.ts, gaborFilter |
| Operator code | web/src/core/filters/linear/gabor.ts, gaborKernels |
| Channels | Brightness only, RGB |
Overview¶
A Gabor filter is a wave under a Gaussian window. It responds strongly where the image has stripes or edges with the wavelength and direction of the wave, and weakly elsewhere. Its complex response has two parts: the real part, a cosine wave, finds thin lines, and the imaginary part, a sine wave, finds edges. The magnitude combines both.
How it works¶
- Build a cosine and a sine wave with the chosen wavelength and direction.
- Multiply both by a Gaussian window.
- Correlate the image with both kernels, and keep the real part, the imaginary part or the magnitude.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(\lambda\): The wavelength in pixels
- \(\theta\): The angle
- \(\sigma_x, \sigma_y\): The Gaussian sizes along the wave and along the stripes
Here x' = x cos theta - y sin theta and y' = x sin theta + y cos theta. The y axis points down, so theta turns counterclockwise on screen. sigma_x is Sigma and sigma_y is Sigma divided by the aspect ratio. The real part of g is a cosine wave and the imaginary part a sine wave. The image is correlated with both, and the magnitude is the length of the complex result. The kernel size and scale follow scikit-image, which convolves, so its imaginary part has the opposite sign.
Gabor showed in 1946 that a wave under a Gaussian window is as compact as a signal can be in time and frequency at once. Daugman extended this to two dimensions and showed that such filters fit the simple cells of the visual cortex. In practice, the Gaussian sets where the filter looks and the wave sets what it looks for. A larger sigma fits more stripes in the window, so the filter becomes more selective about wavelength and less precise about position.
Kernel or operator¶
The Inspector shows the real and the imaginary kernels with the current parameters. They are computed by the same function the filter runs.
Algorithm¶
- Build the real and imaginary kernels for the wavelength, angle, sigma and aspect ratio.
- Correlate the image with both, through the FFT because the kernels are large.
- Return the real part, the imaginary part, or the magnitude.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Wavelength wavelength |
8 | 2 to 64 px | The distance from one stripe of the wave to the next. |
Angle angle |
0 | 0 to 180 deg | The direction in which the wave changes. 0 changes along x and finds vertical stripes. Angles turn counterclockwise. |
Sigma sigma |
4 | 0.5 to 20 px | The size of the Gaussian window along the wave. The window ends at 3 sigma. |
Aspect ratio aspect |
1 | 0.2 to 5 | Sigma along the wave divided by sigma along the stripes. Below 1 the window is long along the stripes. |
Output output |
Magnitude | Magnitude, Real part (cosine), Imaginary part (sine) | Which part of the complex response to show. |
Output mapping outputMapping |
Stretch to full range | Absolute value, Offset by 128, Stretch to full range, Clip to 0 to 255 | How signed results, such as gradients, become 8-bit pixel values. |
Border mode border |
Reflect 101 (default) | Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap | How pixels outside the image are filled when the window reaches past the edge. |
Channels channelScope |
Brightness only | RGB, Brightness only | Which part of the color image the filter changes. |
Implementation¶
gaborKernels in web/src/core/filters/linear/gabor.ts builds both kernels with the size rule and scale of skimage.filters.gabor_kernel. The kernels are large, so planeSpectrum in web/src/core/filters/convolve.ts correlates through the FFT of the extended tile, which gives the same values as direct correlation up to rounding. The filter runs in the browser engine, in Web Workers, through the shared filter framework in web/src/core/filters/run.ts, which handles the border mode, the channel scope, alpha and the output mapping. By default it works on luma.
Worked example¶
| x | -3 | -2 | -1 | 0 | 1 | 2 | 3 |
|---|---|---|---|---|---|---|---|
| Envelope | 0.0018 | 0.0215 | 0.0965 | 0.1592 | 0.0965 | 0.0215 | 0.0018 |
| cos(2 pi x / 4) | 0 | -1 | 0 | 1 | 0 | -1 | 0 |
| sin(2 pi x / 4) | 1 | 0 | -1 | 0 | 1 | 0 | -1 |
| Real kernel | 0 | -0.0215 | 0 | 0.1592 | 0 | -0.0215 | 0 |
| Imaginary kernel | 0.0018 | 0 | -0.0965 | 0 | 0.0965 | 0 | -0.0018 |
The middle row of the kernels for wavelength 4, angle 0, sigma 1 and aspect ratio 1. The kernels are 7 x 7, because the window ends at 3 sigma. On this row y = 0, so x' = x and y' = 0. Each weight is the envelope exp(-x^2 / 2) / (2 pi) times the cosine or the sine. The real kernel is even and finds thin lines. The imaginary kernel is odd and finds edges.
Complexity¶
- Time: \(O(N log N)\)
- Memory: \(O(N)\)
N is the number of pixels in a tile with its halo. The FFT makes the cost nearly independent of the kernel size.
Artifacts and limitations¶
Limitations
- One filter sees one scale and one direction. Use a Gabor bank for more.
- The real part has a small response to flat areas, unless sigma is large compared to the wavelength.
Artifacts
- Ringing next to strong edges, from the side lobes of the wave.
Tests¶
The reference builds the same kernels with NumPy and runs scipy.ndimage.correlate. Before the cases are written, the script checks them against skimage.filters.gabor at the opposite angle, because scikit-image turns the other way. The real parts agree. scikit-image convolves, so the imaginary parts agree after a change of sign. A separate test checks the FFT correlation against direct correlation for all five border modes. The cases cover four settings and the three outputs for all five border modes.
web/src/core/filters/filters.test.tsweb/src/core/filters/run.test.tsweb/src/core/filters/convolve.test.ts
References¶
- Gabor, D. Theory of communication. Part 1, The analysis of information. Journal of the Institution of Electrical Engineers, Part III 93(26), 429 to 441, 1946. DOI 10.1049/ji-3-2.1946.0074.
- Daugman, J. G. Uncertainty relation for resolution in space, spatial frequency, and orientation optimized by two-dimensional visual cortical filters. Journal of the Optical Society of America A 2(7), 1160 to 1169, 1985. DOI 10.1364/JOSAA.2.001160.
- scikit-image documentation, skimage.filters.gabor_kernel. https://scikit-image.org/docs/stable/api/skimage.filters.html#skimage.filters.gabor_kernel