Understanding Gabor Filter
This is an informal tutorial on the intuitive theory behind gabor filters used for image segmantation.
Objective:
We formulate the intuitive theory behind making a gabor filters from first principles. All the detailed derivation of the formulae is laid out in the appendix, so if you just want to take these formulae as granted, a firm grasp in fourier transforms is not essential. We also try out the results of these formulae and plot them in octave.
Notation:
The following notation is used in this post.
Formulate the formulae for gabor filter
Gaussian function and its fourier transform
The equation for a one dimensional gaussian function is
For a general function , the fourier transform is given by,
From \eqref{eqf}, fourier transform of in \eqref{eqa} is,
Introducing one dimensional gabor filter
Consider the functions and , which are gaussian functions modulated by a cosine and a sine function respectively, where is a fixed frequency.
The plots of the functions and are shown above.
The one dimensional complex gabor filter is given by
Please note that the fourier transform of the product of two functions, , , is the convolution of their respective fourier transforms, ie .
Let us do a fourier analysis of and .
Since and since convolution is commutative, and using \eqref{eql}, \eqref{eqf},
So by \eqref{eqq}, the real part of the fourier transform of is the sum of two gaussian functions separated by , and the imaginary part is the difference of two gaussian finctions separeted by .
Similarly,
Similarly,
Introducing two dimensional gabor filter kernel
In fig3, we have plotted the function and
A general 2D cosine function is given by , where are fixed spatial frequencies. This is shown in fig4.
Just as in the case of the 1D gabor filter kernel, we define the 2D gabor filter kernel by the following equations. The complex 2D gabor filter kernel is given by .
In fig5, we have plotted the function .
Note that in fig3, fig4 and fig5, the 3d perspective views are slightly rotated to accentuate their features for viewing decipherability.
Here is the octave code used for generating fig5.
% octave code for showing g_e(x, y) = gaussian times cosine function %author kgeorge clear all; clc; close all; pkg load image sigma_x = 0.5; sigma_y = 0.5; freq = 1.0; %kernel size = 7 x 7 N=7; NBY2 = floor(N/2); tx=ty=linspace(NBY2, NBY2, 50); [xx, yy] = meshgrid(tx, ty); %plot of a simple 2D gaussian I = exp(0.5 * ( (xx ./ sigma_x) .^2 + (yy ./ sigma_y) .^2 ) ); I = I .* (1.0/(2.0 * pi * sigma_x * sigma_y)); %plot of a 2D cosine wave J = cos((xx + yy) .* (2 * pi * freq) ); colormap(copper) s1 = subplot(2,1,2); imshow(I .* J) xlabel('x'); ylabel('y'); title(s1, sprintf("top view")); colormap(copper) s2 = subplot(2,1,1); mesh(xx, yy, I .* J); grid off; view(33, 24); xlabel('x'); ylabel('y'); title(s2, sprintf("perspective view, rotated for convenience")); ha = axes('Position',[0 0 1 1],'Xlim',[0 1],'Ylim',[0 1],'Box','off','Visible','off','Units','normalized', 'clipping' , 'off'); text(0.5, 0.05, sprintf("fig 5, 2d gaussian times cos function, \\sigma_x=%.1f,\\sigma_y=%.1f, u_1=%.1f, v_1=%.1f", sigma_x, sigma_y, freq, freq),'HorizontalAlignment','center','VerticalAlignment', 'top'); %save using a uility function %saveInGithubBlog("gabor_2D_gssn_times_cos");
Please see \eqref{eqs3} for a derivation of the fourier transform of a gabor function as stated in \eqref{eqs2}. Shown below the image of the absolute value of the fourier transform for a gabor 2D function with and . Note that it is an axially aligned elliptical gaussian whose origin is shifted to . Please disregard the axis lines drawn. If you look carefully, in the picture there is a black dot appearing exactly at the centre of the guassian. The black dot represents the frequency and was purposefully added to the image of the fft, just to verify the correctness of the shift.
We have now the equations for the most basic 2D gabor filter function \eqref{eqs2} and its fourier transform \eqref{eqs4}. But for using this filter function practically, we need to exploit the various ways by which this filter function can be parameterized.
First we will reformulate \eqref{eqs2} using a more intuitive set of parameters.
A better way of expressing the parameters would be to capture the ratio between the two parameters. Thus by setting and , \eqref{eqs2} will become
Also, consider the term . If we set , then we can introduce an angle parameter where, and . With this the equation, \eqref{eqs11} becomes,
The fourier transform of the above representation in \eqref{eqs12} is given by \eqref{eqs13a}, which is reproduced here.
A pictorial description of the fourier transform in \eqref{eqs16} is given by fig 6b, which is expectedly a gaussian shifted by . Note the black dot, which is deliberately added at , to verify the correctness of the shift. Also please note that, since , the transform is elliptical.
In fact if we plot the graph in the spatial domain, that also will be elliptical, as noted in the following figure, . You may wonder why, the eccentricity of the elliptical figure is reversed in the transform image when compared to the spatial image, this is because, the fourier transform of a gaussian function with a given , is a differently scaled gaussian function with whose value is .
A bank of 2D gabor filter functions
Where are we heading? We need to come up with a bank of gabor filter functions, so that, effectively they cover, the entire transform space. This is shown as a rough outline in fig 6d below.
In order to achieve this varied set of gabor functions, we need to start playing around with these existing set of parameters and see how the gabor function changes. Later we will add a few more parameters.
Since we are just studying the effects of these parameters, let us continue to focus on the real part of this filter.
In fig7, we have plotted the real part of the gabor 2D filter with varying values of and . Note that, as the values vary, the eccentricities of the gaussian ellipse, changes, but nevertheless remains axially aligned. Also, we have kept and the same.
Let us now try varying and . To illustrate this, just for the purpose of clarity, we are going to plot only the cosine function, ie . Its effect on the modulation with gaussian function is self explanatory.
You can see from fig8, the changes in the frequency , of the cosine wave.
Let us vary now.
In all the considerations till now, the gaussian function was elliptical, but axially aligned. Intuitively, it would help to design better filter banks, if the gaussian function is tilted in the same manner as . This can be accomplished by introducting a rotation transformation while computing the gaussian function.
The above rotation can be accomplished by the following coordinate transform,
Shown in figure 12, above is the real part of the 2D gabor function with the effect of the gaussian function rotated by the above transformation given in \eqref{eqt}.
Another parameter that we can introduce is a phase factor in the sinusoidal component, which becomes, . The cosine part of this sinusoidal function (ie the real part) is plotted below as fig 12.
Putting all these together, the gabor kernel is defined as
\eqref{eqs22} gives the fourier transform of such a gabor function described in \eqref{eqw}. Shown below is the absolute value of the fourier transform of such an example gabor function, with and . Note that the spectrum is still an axially aligned ellipse and is shifted to . Please disregard the axis lines drawn. Also, we have added a black dot to the transform image at the frequency , to signify and verify the correctness of the shift.
Appendix
Derivation of fourier transform of a 1D gaussian function.
How do we find the fourier transform of a one dimensional gaussian function ?
For a general function , the fourier transform is given by \eqref{eqv}.
On a simpler note, let us work on a simple function and try to find its fourier transform .
Please remember that for an odd function , and so .
Since is an even function and is an odd function, .
So the above equation simplifies to
Using [1], Abramovitz et. al., . So the above equation translates to,
Putting , in \eqref{eqe}, fourier transform of in \eqref{eqa} is,
Derivation of fourier transform of a 2D gaussian function.
A 2D gaussian function is given by \eqref{eqaa}
Note that \eqref{eqaa} can be written as,
Given any 2D function , its fourier transform is given by
A 2D function is separable, if it can be written as . If and are the fourier transforms of and respectively, then,
From \eqref{eqab}, \eqref{eqad}, and \eqref{eqf}, we derive the fourier transform of a gaussian as,
Derivation of fourier transform of sine and cosine functions
Consider a simple cosine and a sine function, and respectively, what would be their fourier transforms and ?
From \eqref{eqh},
Consider the integral , That is the fourier transform of the identity function .
The fourier inversion theorem states the following, if , then . For a proof, please see [2], Proof of Fourier inversion theorem
Now considering, the dirac delta function with the property , the integral below, evaluates to or the identity function .
Applying the fourier inversion theorem, \eqref{eqj} translates to
So \eqref{eqi} can be continued as,
Similarly, from \eqref{eqg}, fourier transform of sine function is given by,
Fourier transform of a rotated 2D signal
Let . Consider the rotation given in \eqref{eqt}, and shown in fig 10.
Another way to express this relationship is ad are functions of given by,
Similarly, ad are functions of given by,
Please note the following identity for taking derivatives for transformations,
If we set,
By \eqref{eqac}, \eqref{eqm4} and \eqref{eqm5}, and if we denote , the the corresponding fourier transform is given by,
This means that, fourier transform of a rotated signal , is the fourier transform which will be the fourier transform after a rotation of , as shown in the figure below.
Derivation of fourier transform of a 2D gabor function.
Since there are several variants of the 2D functions mentioned in this article, let us first focus on the most basic of these, as described in \eqref{eqs2}.
fourier transform of simple 2D gabor function given in \eqref{eqs2}
From \eqref{eqs2},
So by the separability feature of fourier transform as shown in \eqref{eqad} and by \eqref{eqp2}, the fourier transform of a 2D gabor function given in \eqref{eqs3} is,
Note that is of the gaussian , translated to the frequency .
Now let us move to more general and parametric forms of the 2D gabor function.
fourier transform of 2D gabor function (with and parameters) as described in \eqref{eqs12}
We will be working with \eqref{eqs12}.
The fourier transform of the 1D gaussian function , can be expressed moe accurately with the parameter as .
So we can rewrite \eqref{eqf} as
From \eqref{eqs12}
So again by the separability feature of fourier transform as shown in \eqref{eqad} and by \eqref{eqp2} , will translate to
fourier transform of 2D gabor function (with , and rotated gaussian)
The most generalized form of the 2D gabor function was given by \eqref{eqw}, which is reproduced below,
Let us try to come up with a formula for its fourier transform .
Consider the function, in \eqref{eqs17},
Note that . Here we used the notation in \eqref{eqm2a} that , and are functions of and .
The fourier transform derived below.
Due to the separability feature of the fourier transform, we have,
Finally we have,
References

Abramowitz, M. and Stegun, I. A. (Eds.). Handbook of Mathematical Functions with Formulas, Graphs, and Mathematical Tables, 9th printing. New York: Dover, p. 302,

[Proof of Fourier inversion theorem] (http://math.columbia.edu/~nsnyder/tutorial/lecture3.pdf)
blog comments powered by Disqus