The Gabor filter is a linear filter used in image processing for feature extraction, particularly for edge detection. It is named after the physicist Dennis Gabor, who proposed it in 1946 as a model of the receptive field of the human visual system.
The Gabor filter is designed to mimic the response of the human visual system to patterns of varying spatial frequency and orientation. It is based on a sinusoidal plane wave (which represents the pattern) modulated by a Gaussian envelope (which represents the visual sensitivity of the eye).
The Gabor filter can be represented mathematically as:
g(x,y;λ,θ,ψ,σ,γ) = exp(-(x'² + γ²y'²) / 2σ²) * exp(i(2πx'/λ + ψ))
where:
x' = x cos(θ) + y sin(θ)
y' = -x sin(θ) + y cos(θ)
λ
is the wavelength (spatial frequency) of the sinusoidal patternθ
is the orientation of the patternψ
is the phase offset of the patternσ
is the standard deviation of the Gaussian envelopeγ
is the aspect ratio of the Gaussian envelope
To apply the Gabor filter to an image, we convolve it with the input image. The result is a filtered image that highlights edges and other features that match the frequency and orientation of the Gabor filter.
Here’s an example of how to apply a Gabor filter in Python using the OPENCV
library:
import cv2 import matplotlib.pyplot as plt import numpy as np %matplotlib inline image = cv2.imread('natural.jpg') plt.imshow(image)
def create_gaborfilter(): filters=[] num_filters=16 ksize=30 sigma=3.0 lambd=10.0 gamma=0.5 psi=0 for theta in np.arange(0, np.pi, np.pi / num_filters): kern = cv2.getGaborKernel((ksize,ksize), sigma, theta, lambd, gamma, psi, ktype=cv2.CV_64F) kern/=1.0* kern.sum() filters.append(kern) return filters def apply_filter(img, filters): newimg = np.zeros_like(img) depth = -1 for kern in filters: img_filter = cv2.filter2D(img, depth, kern) np.maximum(newimg,img_filter,newimg) return newimg gfilters = create_gaborfilter() image_g = apply_filter(image,gfilters) plt.imshow(image_g)
As we can see from the above image, after applying the Gabor filter, edges are properly visible with core image features, which makes this edge detection better than other methods.