# Apply a Gabor filter to an input image

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
lambda=3.5;
gamma=0.3;
sigma=2.8;
psi=0;

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);

x_theta=x*cos(theta)+y*sin(theta);
y_theta=-x*sin(theta)+y*cos(theta);

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

figure(2);
imshow(gb);
title('theta=...');
%imagesc(gb);
%colormap(gray);
%title('theta=...');


I=imread('piano.jpg');
image_resize=imresize(I, [160,160]);
image_gray=rgb2gray(image_resize);
image_double=im2double(image_gray);
figure(1);
imshow(image_double);


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.)

figure(3);
filtered = conv2(image_double,gb);
imagesc(filtered);
colormap(gray);
title('theta=....');


• Also, if you are using conv2 please use it with 'same' option. Jan 4 '14 at 15:14
• 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 Jun 14 '17 at 7:41
• this filter isn't 7x7? Dec 6 '17 at 8:55
• How you have justified that the filtered image is correct by applying Gabor filter to the input image. Nov 13 '20 at 3:38

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

.

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

• 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!!!) Nov 5 '13 at 14:45
• 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)? Nov 5 '13 at 15:12