Gabor Filter-based Edge detection in Image Processing

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'/λ + ψ))


  • 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')
Original Image
def create_gaborfilter():
    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()
    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)
    return newimg

gfilters = create_gaborfilter()
image_g = apply_filter(image,gfilters)
Image after applying Gabor filter

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.

Leave a Comment