4.1 | 81 ratings Rate this file 172 Downloads (last 30 days) File Size: 2.49 KB File ID: #5237

2D Gabor Filter(Ver1,2,3)



13 Jun 2004 (Updated )

To design 2D Gabor filter and apply it to image.

| Watch this File

File Information

Recent studies on Mathematical modeling of visual cortical cells [Kulikowski/Marcelja/Bishop:1982] suggest a tuned band pass filter bank structure. These filters are found to have Gaussian transfer functions in the frequency domain. Thus, taking the Inverse Fourier Transform of this transfer function we get a filter characteristics closely resembling to the Gabor filters. The Gabor filter is basically a Gaussian (with variances sx and sy along x and y-axes respectively) modulated by a complex sinusoid (with centre frequencies U and V along x and y-axes respectively).

Gabor filters are used mostly in shape detectin and feature extractin in image processing.

function [G,gabout] = gaborfilter1(I,Sx,Sy,f,theta);
from 'gaborfilter1' with different f(Frequency) and theta(Angle).
for example

theta = 0,pi/3,pi/6,pi/2,3pi/4

then for any input image like(eg. stereo.jpg)

you have 6x5 = 30 filtered images.

You can choose your desired angles or frequencies.

You can put nominaly Sx & Sy = 2,4 or some one else.

For instance I tested above example on ('cameraman.tif')(in MATLAB pictures)

I = imread('cameraman.tif');
[G,gabout] = gaborfilter1(I,2,4,16,pi/3);


This file inspired Gabor Filter.

Required Products Filter Design Toolbox
Image Processing Toolbox
MATLAB release MATLAB 6.5 (R13)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (110)
23 Sep 2014 bruce

bruce (view profile)


07 Jul 2014 yang wang

Well, I think it is not so good. For the result image is just a black, whole black without any information.

08 Dec 2013 anum eman  
24 Oct 2013 Dennis

Dennis (view profile)

15 Feb 2013 ENG

ENG (view profile)


Comment only
15 Feb 2013 ENG

ENG (view profile)

thanks alot

10 Jul 2012 Omid

Omid (view profile)

26 Apr 2012 Stefan

Stefan (view profile)

I also recognized the missing imaginary part:
In General:
r*e^(i*phi) == r*(cos(phi)+i*sin(phi))

If you simplify r*e^(i*phi) to r*cos(phi) the you leave out the imaginary part. For this reason you needn't use 'conv2(imag(image))', because it's zero anyway.

So either add the imaginary part or leave out the imaginary part convolution to improve computation costs.

Or do I misapply your code?

Comment only
15 Mar 2012 Shikha

Shikha (view profile)

hello i am new to Matlab and i need Gabor filter code for edge detection ??? i am not getting how to do ......anyone help me

Comment only
08 Aug 2011 Thendral

??? Undefined function or method 'gaborfilter' for input arguments of
type 'double'.
I am getting this error.
I tried with different input but cant fix it.
Anyone help me..

Comment only
29 Jul 2011 Virginia

Thank you!
I would like to ask you if there is anyway to plot the Gabor filter created with gabor1

Comment only
02 May 2011 Liu Shangwang

a good job!

04 Mar 2011 Giri

Giri (view profile)

oru mannum velangala

Comment only
15 Jan 2011 sandhya salve

Hi all,
i have tried this gabor filter
I got the some error:
if isa(I,'double')~=1

i m working on gabor filter
will u please send me the details
n if possible the code for fingerprint image
enhancement using it
Thank you

09 Nov 2010 Keyon C  
09 Nov 2010 Keyon C

thaks a lot

Comment only
05 Oct 2010 James Kelly

does someone know how to implement this in c#? im kinda new to matlab so its difficult to know how this exactly loops over pixel arrays in bitmaps etc... if someone could convert this to c# or explain it would be great

Comment only
18 Sep 2010 Wang

Wang (view profile)

Thanks a lot! This is really helpful.

06 Sep 2010 György Kovács

It seems, you can't use clickable thumbnails here, so here are the proper links:

Comment only
06 Sep 2010 György Kovács

Seemingly I was wrong, and after [G,gabout] = ... the filter is in G.
But this raises an other question (I hope someone can answer):

The parameters I used were the following:
Sx = 16;
Sy = 32;
theta = pi/2
I is a 100x100 matrix consisting of ones

For the parameter F, I used the following numbers: 0,2,4,32

And here are the pictures of the log amplitudos of the resulting G matrixes:

And no, I did not upload the same picture four times, they just look a lot alike, but I can't seem to find out why. Could anyone please help?
Thanks in advance.


Comment only
02 Sep 2010 György Kovács

The function seems useful.
I'd like to ask something, risking to look stupid: Is it correct to assume, that for getting the actual filter (to see how it looks, also I need to use the same filter on thousands of images), it is enough to choose I as a matrix consisting of ones?
Thanks in advance for the help.

Comment only
10 Aug 2010 Francinei Morais

very good script

Comment only
10 Aug 2010 Francinei Morais  
08 Aug 2010 spectral

Can anyone help me to correct the gabor filter below to produce the response image in order to analyse the texture of the brain image.

%i is input image
%fourier transform of the input image
i2 = fft2(i);

%gabor filter in polar coordinates
%sinusoidal carrier
[M, N] = size(i2);
F0 = 64*sqrt(2); %spatial frequency of sinusoidal carrier
w0 = 135; %direction of sinusoidal carrier
P = 0; %phase of sinusoidal carrier

%gaussian envelop
x0 = N/2; %peak of the function
y0 = M/2; %peak of the function
a = 1/128; %axis of the gaussian envelop
b = 1/128; %axis of the gaussian envelop
theta = 135; %orientation of gaussian envelop
K = 2*pi*(1/a*b);

%complex gabor function
carrier = zeros(size(i));
envelop = zeros(size(i));
gabor = zeros(size(i));
gtransform = zeros(size(i));

for y = 1:M
for x = 1:N
carrier(y,x) = exp(1i*(2*pi*F0*((x*cosd(w0))+(y*sind(w0))) + P));

x1 = (x-x0)*cosd(theta) + (y-y0)*sind(theta);
y1 = -(x-x0)*sind(theta) + (y-y0)*cosd(theta);
envelop(y,x) = K*exp(-pi*((a^2*x1^2)+(b^2*y1^2)));

%gabor filter
gabor(y,x) = conv2(carrier(y,x),envelop(y,x));

%convolution between gabor n input (filtered image)
gtransform(y,x) = conv2(gabor(y,x),i2(y,x));

%response image
i3 = ifft2(gtransform);

Comment only
29 Jul 2010 shi ?

shi ? (view profile)

thanks a lot

12 May 2010 Kumar

Kumar (view profile)

When The Filter is created in the Frequency Domain why it is converted into spatial domain For Convolving with the Image .Is It essential or can we perform convolution in frequency domain

Comment only
25 Jan 2010 Dwi Aryani

it is very useful thanks

Comment only
29 Sep 2009 kevin

kevin (view profile)

07 Apr 2009 Varsha Mhaske  
02 Mar 2009 ganesh kumar

can u just brief me on the parameters of the filter?On what basis do i select the parameter values??

Comment only
27 Jan 2009 Anton

Anton (view profile)

By looking over the code and the description, it seems the filters are being crated in the frequency domain and then applied in the spatial domain. Please correct me if I am wrong but this is WRONG. There are two RIGHT approaches. The first is that one needs to take the inverse FT of the filter and then convolve it with the image. The second it that one needs to convert the image into the frequency domain, multiply the spectra and then take the inverse of the result.

Comment only
11 Nov 2008 sidd singh


16 Oct 2008 vinayak jagzap

i m working on gabor filter
will u please send me the details
n if possible the code for fingerprint image
enhancement using it

15 Oct 2008 akshay aggarwal

@Geovanny Giorgana: the filter works only for a 2-D immage. Try checking the size of the input image.

14 Oct 2008 mohammad poshideh  
14 Oct 2008 mohammad poshideh  
13 Oct 2008 Geovanny Giorgana

Hi all, I got the same error: Function 'conv2' is not defined for values of class 'double' and attributes 'full 3d real'.

Can someone explain me how to solve it. Thank you.

14 Sep 2008 sandeep gupta

it is efficiently working

22 Jul 2008 vishal wankhede

Very efficient

09 Jul 2008 AKROUT Belhassen

good job

28 May 2008 Pushkin Alexander

Ican't fix "Undefined function or method 'conv2' for input arguments of type 'double' and attributes 'full 3d real'." Please somebody tell how fix it by "reshape". Thanks

13 May 2008 jimmy wang

So helpful!TKS

12 May 2008 Abd Elghany mohamed

Aslamo alekm
are you hove this code using IDL programming

12 Mar 2008 jun wu

thanks for your Gabor filter.

27 Feb 2008 Hedieh Sajedi  
22 Feb 2008 ksibi amel


Comment only
18 Feb 2008 Jose Armando Fernández Gallego

Muy buen Codigo !

04 Feb 2008 Richa Agarwal

Its very good.

12 Dec 2007 Feng Liu

very good!

16 Nov 2007 Ratnakar Dash  
30 Oct 2007 Protik Maitra

A few images with corresponding parameters would have helped understand this better.

23 Oct 2007 Selvan Selvanidhyananthan

Gabor filter is a very interesting topic in wavelets.

17 Oct 2007 lorenzo rossi

Why I have this error?
Function 'conv2' is not defined for values of class 'double' and attributes 'full 3d real'.

Comment only
15 Jul 2007 au diesel


Comment only
13 May 2007 Emin Orhan

The discrepancy between the results obtained by filtering with this implementation and the with the one on the matlabserver.cs.rug.nl is, I think, related to the size of the filter, G, instead of fix(Sx) (and fix(Sy)), try something like fix(3*Sx) etc.-i.e.enlarge the size of the filter- and the code works just fine (in corformity with matlabserver.cs.rug.nl), nothing wrong with the code.

10 May 2007 ujjal bhowmik  
05 May 2007 Liang Zhao  
23 Feb 2007 hamed nozari  
31 Jan 2007 Jyotirmoy Banerjee

The formula used in this code doesn't have parameters like aspect ratio and phase. so it is not fair to compare the results with some other code. All depends on the parameters you set.

19 Jan 2007 paulhac ludo

I don't know if there is a mistake in this code. In fact the imaginary part is always null but it is normal if we look the formula used. In the formula there is not the imaginary part.
The general gabor formula has an imaginary part, but usually , when I read article, there is not this imaginary part and I don't know why.
Is there someone to answer ???

I implement gabor with the imaginary part and when I try with this link http://matlabserver.cs.rug.nl/edgedetectionweb/web/index.html I don't find the same things. But the result depends how we set parameters.

Comment only
29 Dec 2006 T G

i think there is a mistake in this code. because the results shown in this link http://matlabserver.cs.rug.nl/edgedetectionweb/web/index.html are obtained by using the equation in this link, http://matlabserver.cs.rug.nl/edgedetectionweb/web/edgedetection_params.html

but unfortunately, these results are not as same as yours.

isn't it???

please, tell me if i'm wrong.
thanks a lot..

Comment only
07 Dec 2006 J. Colineau

The output seems to be the modulus of the complex response to your filter. In many cases, the interesting part is the real component, and not the modulus, which is a non-linear operation !

Comment only
29 Nov 2006 hamed kowsari  
28 Oct 2006 Makoto Sanada

I am reading the gaborfilter1.m (version2), and I think there is a mistake.
In the source codes, there is a code line to retrive imaginary parts of filters as imag(G).
However, the math equation of G is originally calculating only real responses (cos). There is no jsin or exp(j...).
Don't you need imaginary responses?

Comment only
10 Oct 2006 hari bashkarraj

very nice

Comment only
28 Sep 2006 Dirkjan Krijnders

Functionality is good, but code does not depends on any toolbox and documentation is not in standard matlab format.

25 Aug 2006 Weijun Zhao

Thanks, I have been searching for it.

08 Aug 2006 hamed kowsari  
21 Jul 2006 bendahmane zakaria  
18 Jul 2006 Udai Kiran


01 Jun 2006 Mayank Raj


28 May 2006 Bobby Jan

Excellent. Has some one tried recursive Gabor filtering & its usefulness? Can some one share?

28 May 2006 Piyorot Khongchuay


16 May 2006 cintia monti  
03 May 2006 deepak singh

great work

02 May 2006 Engy Cruel Heart  
30 Apr 2006 Piyorot Khongchuay


29 Apr 2006 Sam Darvishi  
16 Apr 2006 he yi

very good,I once program a c++ version about gabor filters. now I find this code and use it to verify mine. It is very convenient and thanks for u.

Comment only
02 Apr 2006 Hyeong-yong Jeon


23 Mar 2006 Deepti Singh

Cn anyone tell me how to calculate optimal sx,sy,theta and f for an image.so that this function cn be used irrespective of any image.How to implement it i mean how to use the output G and gabout.Thx in advance.urgetntly in need of reply

Comment only
20 Mar 2006 vijay anandh  
13 Mar 2006 sriram pothuri  
08 Mar 2006 yunchu ZHANG

This helps me a lot!

05 Mar 2006 saratchandra k

I found this little description very useful. Wee bit'o more info about the cortical cell study .. or maybe where I can find it would be useful. And yeah im working on fingerprints and would like to get more info in that regard

02 Mar 2006 Chenna Krishna Varri  
17 Feb 2006 wise wise

"Gabor filters are used mostly in shape detectin and feature extractin in image processing"

Can somebody give me some help on how to use gabor filter for fingerprint image feature extraction?


05 Jan 2006 Liu Xianru

It is very good and suit for new researchers!
Thank you !

09 Dec 2005 A.H. S

A Good Job done

01 Dec 2005 Alok Whig

Q1. What is the difference between GaborFilter,
GaborFilter1 and GaborFilter2 ?

Q2. Is R13 realease same as MATLAB version 7.0 ?

I tried the files.They work fine.

09 Nov 2005 wang yue  
27 Sep 2005 yu qian  
23 Sep 2005 Peng Zhangping

i want to process the image of fingerprints,i hope this can help me to enhance the image.

01 Sep 2005 Fiona Yu

The mask matrix is excellent in approximation to the Gaussian envelope.

31 Aug 2005 Basabdatta Bhattacharya  
08 Aug 2005 gopal krishnan  
11 Jul 2005 ahmad vazvani  
03 Jul 2005 adek yudi

after I tried, I think it's great...!!!

27 Jun 2005 abdel oranov

les deux filtre ne sont pas symetrique.
pour verifier tu fais sum(G1(:)) tu va trouver que ce n'est pas tjr symetrique;

20 May 2005 Pablo Suau

It doesn't work when using pi/2

28 Apr 2005 Luigi Rosa

Excellent work. In order to improve filtering performances a Recursive Gabor filtering for 1D and 2D signals is required.

For more information please visit:


21 Apr 2005 Natalia Zabrydowa

thank you:)

Comment only
04 Dec 2004 Alex Leykin

The normalization doesn't work. The repsonse jumps out of bounds at pi/2

09 Nov 2004 Daniel Garcia

It is good

02 Aug 2004 Viara Van Raad

Usually 2D design of Gabor filters require DESIGN in 2D Frequency ( because it can easily get out of boounds!). This is related to several issues:
a) not obeying the Nyquist criteria
b) phase distortions
c) not forming a '' frame'' see 10 lectures on wavelets
d) mack up in 2D frequencies
and some others. The design in space is naive.

Comment only
05 Jul 2004 Jesmin Khan  
05 Jul 2004 ali adhami

very powerfull!

02 Jul 2004 khaled sultan


21 Jun 2004 robert kelly

I need this for my project very very much.You do it well:))

19 Jun 2004 mana sh

thx,very complete!!

Contact us