Computer vision

# Feature Extraction – Gabor Filters

If you have read any article on texture segmentation, then you might have come across Gabor filters. Yes, they are used for other applications too, like enhancement, edge detection, document analysis etc. So, what are Gabor filters? Do they really work like a regular filter that reduces noise present in a signal?

Since this blog focuses on Computer Vision, we will consider only 2D Gabor filters. But, if you are interested in 1D Gabor filters you may find this article useful (Images are easier to visualize than 1D signals.)

What makes Gabor filters special? As you know, when it comes to vision systems, human visual system remains undefeated. Gabor filters kind of mimics the human visual system. Thus a set (yes; a set. We usually use more than one Gabor filter) of Gabor filters can be used to determine or preserve the orientation or scale of edges in an image

Since we know that Gabor filters are amazing, let’s touch upon the mathematical side of these filters. Gabor filter consists of two components,

• Complex sinusoid (s(x,y)), known as carrier.
• Gaussian function,(g(x,y)) known as envelope.

The complex sinusoid is defined as,

$c(x,y) = exp(-j(2\pi F(xcos(\theta) + ysin(\theta)))$

Where, is the orientation of the complex sinusoid, F is the magnitude of the sinusoid and P denotes phase and the Gaussian function is defined as:

$g_1(x,y) = \frac{1}{\sqrt{2\pi} \sigma}exp(-0.5(\frac{x^2}{\sigma _x ^2} + \frac{y^2}{\sigma _y ^2}))$

In order to rotate the Gaussian function in the direction of the complex sinusoid, we have to modify the Gaussian function as:

$g(x,y) = \frac{1}{\sqrt{2\pi} \sigma}exp(-0.5(\frac{x_t}{\sigma _x ^2} + \frac{y_t}{\sigma _y ^2}))$

where,

$x_t = (x*cos(\theta) + y*sin(\theta))^2$

$y_t = (-x*sin(\theta) + y*cos(\theta))^2$

Thus, the 2D Gabor filter is given by,

$gabor(x,y) = c(x,y)*g(x,y)$

$gabor(x,y) = exp(-j(2\pi F(xcos(\theta) + ysin(\theta))+P)*\frac{1}{\sqrt{2\pi} \sigma}exp(-0.5(\frac{x_t}{\sigma _x ^2} + \frac{y_t}{\sigma _y ^2}))$

Now, let’s visualize the above equations. Start MATLAB and run the following code:

%Defining parameters
img_size = 90;          %Size of the image
theta = 90;             %Orientation of the signal
F = 1/20;               %Magnitude of the signal
phase = 0;              %Phase of the signal

%Generating X and Y coordinates
[y x]=meshgrid(-round(img_size/2):round(img_size/2),round(img_size/2):-1:round(-img_size/2));
%Carrier signal
carrier_signal = real(exp(1i*(2*pi*F*(x*cosd(theta) + y*sind(theta))+phase)));
%Displaying the signal
imshow(carrier_signal,[]);
title('Carrier Signal');


I ran the above code for theta = {0,90,45,65} degrees. The result is shown in Fig 1

Now to visualize the Gaussian envelope, run the following code

%Defining parameters&amp;lt;/pre&amp;gt;
&amp;lt;pre&amp;gt;img_size = 90;          %Size of the image
theta = 0;              %Orientation of the signal
sigma_x = 5;            %Standard deviation along x-axis
sigma_y = 20;           %Standard deviation along y-axis

%Generating X and Y coordinates
[y x]=meshgrid(-round(img_size/2):round(img_size/2),round(img_size/2):-1:round(-img_size/2));
%Carrier signal
x_t = x*cosd(theta) + y*sind(theta);
y_t = -x*sind(theta) + y*cosd(theta);
gaussian_env = exp(-0.5*(x_t.^2./sigma_x^2 + y_t.^2./sigma_y^2));
%Displaying the signal
imshow(gaussian_env,[]);
title('Gaussian envelope');


The result of the above code is shown in Fig 2

When we multiply the carrier signal and the gaussian envelope, the obtain Gabor filters.

%Multiply gaussian envelope and carrier signal
Gabor_kernel = gaussian_env.*carrier_signal;

%Display the image
imshow(Gabor_kernel,[]);
title('Gabor kernel');


The Gabor kernel for different values of theta is shown in Fig 3.

In the image shown in Fig 4, we have lines oriented at 0, 45, 90 and 135 degrees.These Gabor kernels are used to find edges oriented in any given direction i.e. if we convolve an image with a Gabor kernel with theta = 45 deg,we will obtain strong response if there are edges oriented at 45 deg.

It is evident from Fig 5, that in the convolved images edges oriented in the direction of the Gabor kernel has produced a strong response. This can be used to detect lines in any orientation or analyse texture. How? Say if you wanted to find whether any line inclined at 45 degrees in Fig 4. When you multiply the image with Gabor kernel with theta = 45 deg, you will get a high response as shown in Fig 5. This is used to detected line inclined at 45 deg.When we convolve the above image with Gabor kernels with theta = (0, 45, 90, 135) we obtain responses as shown below:

Seems like a simple concept – gabor filters, but it is widely used in computer vision to detect textures. I’ll soon write a post on texture classification using gabor filters.
C++ code

Have a great day.. 🙂