I tried to apply a Gabor filter with a specific scale (according to my values of lambda and sigma, so it is (7x7) and for 4 orientations (0, $\frac{\pi}{4}$, $\frac{\pi}{2}$ and $\frac{3\pi}{4}$) to a input gray scale image.

In my code, three steps are achieved:

  1. Create a Gabor Filter

  2. Read an RGB image, then convert it to gray scale and finaly to double.

  3. Apply the created gabor to the input image (here, i am not sure if my code is true, that's why i need your opinion )

1) -------------- create the Gabor filter (size=7x7 and 4 orientations)

%define the five parameters
theta=....; %either 0 or pi/4 or pi/2 or 3pi/4

sigma_x = sigma;
sigma_y = sigma/gamma;

nstds = 5;
xmax = max(abs(nstds*sigma_x*cos(theta)),abs(nstds*sigma_y*sin(theta)));
xmax = ceil(max(1,xmax));
ymax = max(abs(nstds*sigma_x*sin(theta)),abs(nstds*sigma_y*cos(theta)));
ymax = ceil(max(1,ymax));
xmin = -xmax; ymin = -ymax;
[x,y] = meshgrid(xmin:xmax,ymin:ymax);


gb= exp(-.5*(x_theta.^2/sigma_x^2+y_theta.^2/sigma_y^2)).*cos(2*pi/lambda*x_theta+psi);


enter image description here

2)------------Read the input image

image_resize=imresize(I, [160,160]);

enter image description here

3) -----apply the created above gabor to the input image (recall that i am not sure if the code in this step is 100% true, that's why i need your opinion and your help if you have the correct answer.)

filtered = conv2(image_double,gb);

enter image description here

  • $\begingroup$ Also, if you are using conv2 please use it with 'same' option. $\endgroup$ – Tolga Birdal Jan 4 '14 at 15:14
  • $\begingroup$ Error: File: practise1.m Line: 3 Column: 7 The expression to the left of the equals sign is not a valid target for an assignment. I found this error when i run this code $\endgroup$ – Engr Rao Zaka Jun 14 '17 at 7:41
  • $\begingroup$ this filter isn't 7x7? $\endgroup$ – jiggunjer Dec 6 '17 at 8:55

Your code is correct and the results are consistent. You may be surprised by them due to some 'hidden features'.

First, conv2 returns by default the full convolution, such that the result is the size of the image plus a border of half the size of the kernel (that is, the total size if the size of the image plus that of the kernel). When you interpret your results, be aware of it!

Second, the results represent coefficients that are stronger for a higher correlation between your kernel and your local image patch: as expected you also extract the borders of the image. See in particular your leftmost result showing strong vertical line.

Last, imagesc scales by default the scale between the highest to the lowest coefficient. That's why in the leftmost result, you mainly see the border.

There are different options to conv2described in help conv2 which allow to control this behavior.

Be aware also that there many different definitions of kernels for detecting edges, such as log-Gabors

different types of filters.

If you are interested in a full implementation (in python) you may have a look at: https://pythonhosted.org/LogGabor/ (shameless self-plug 😇).


Your code is correct. You simply need to do the 2-D convolution with the filter kernel which you are doing very well.

Good Luck

  • $\begingroup$ I am not able to understand you. You are doing 2-D Convolution (as you are using conv2 function). Another way is to go for multiplication in the frequency domain (because convolution in time or space-domain is equivalent to multiplication in the frequency domain). However you need not do that, since conv2 essentially does the same thing (internally!!!) $\endgroup$ – Ujjwal Aryan Nov 5 '13 at 14:45
  • $\begingroup$ Exactly :) ,, do you know HMAX model? (the S1, C1, S2 , C2 layers ...). I am trying now to compute the C1 layer (max operation between S1 units), do you know some ideas about that (matlab code)? $\endgroup$ – Liszt Nov 5 '13 at 15:12

How you have justified that the filtered image is correct by applying Gabor filter to the input image.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.