# How to visualize and understand Gabor filter using MATLAB

I am reading a paper, in which the author defines gabor kernels as :

$$\psi_{\mu,\nu}(x,y)=\frac{\lVert \mathbf{k}_{\mu,\nu} \rVert}{\sigma^{2}}\exp \left(-\frac{\lVert \mathbf{k}_{\mu,\nu}\rVert^{2}\lVert \mathbf{z}\rVert^{2}}{2\sigma^{2}}\right)\times\left[\exp\left(i\mathbf{k}_{\mu,\nu}^{T}\mathbf{z}\right)-\exp\left(-\frac{\sigma^{2}}{2}\right)\right]$$ where $\mu$ and $\nu$ define the orientation and scale of tghe Gabor kernels, respectively, $\mathbf{z}=(x,y)^{T}$, and the wave vector is defined as

$$\mathbf{k}_{\mu,\nu} = \left(k_{\nu}\cos{\phi_{\mu}},k_{\nu}\sin{\phi_{\mu}}\right)^{T} \quad\text{with}\quad k_{\nu} = \frac{k_{\rm max}}{f^{\nu}}, k_{\rm max}=\frac{\pi}2,f=\sqrt{2},\quad\text{and}\quad\phi_{\mu}=\frac{\pi\mu}{8}$$

Now, I want to plot the graph of this function using matlab to visually see and understand the Gabor filter. I have very basic knowledge in using matlab.

Specifically, I am interested in viewing these Gabor Kernels at single scale $(\nu=0)$ and four orientations $(\mu\in\{0,2,4,6\})$ with $\sigma=1$

Right now I am trying to understand Gabor filters. Any help will be greatly appreciated.

The following is my implementation, of the above problem :

In my_gabor_filter.m, I have the following code :

function psi = my_gabor_filter(x,y,mu,nu,sigma)
phi = pi*mu/8;
f = sqrt(2);
k_max = pi/2;
k_nu = k_max/(f^nu);

% This is the wave vector
k_vec = [k_nu*cos(phi),k_nu*sin(phi)]';
z_vec = [x,y]';

k_vec_norm = norm(k_vec);
z_vec_norm = norm(z_vec);

exp1 = (k_vec_norm/(sigma^2));
exp2 = (exp(-((k_vec_norm^2)*(z_vec_norm^2)/(2*sigma^2))));
exp3 = (exp(complex(0,k_vec'*z_vec))-exp(-(sigma^2/2)));

psi = exp1*exp2*exp3;
end


and, in plot_gabor_filter.m I did the following :

function plot_gabor_filter()
mu = 0;
nu = 0;
sigma = 1;

[X,Y] = meshgrid(-2:0.1:2,-2:0.1:2);

Z = zeros(41,41);

for i = 1:1:41
for j = 1:1:41
Z(i,j) = imag(my_gabor_filter(X(i,j),Y(i,j),mu,nu,sigma));
end
end

surf(X,Y,Z,'EdgeColor','black')

xlabel('x axis');
ylabel('y axis');
zlabel('g(x,y)');
end


and when I run plot_gabor_filter, I get the following output, which helped me in visualizing the gabor function : • according to another formula I found the k_vec_norm in exp1 should be squared. My math isn't great but that seems like it would be more consistent with the gaussian term on wikipedia Dec 6 '17 at 8:01

I checked out the code. I think in the exp3 line, while using the dot product there is a problem. The correct format for dot product of a and b is: a.*b - you have written it as a*b.

Also while taking dot product you have arranged it as 2x1 with a 1x2 vector. Try it in the other orders, i.e.: a 1x2 with a 2x1 or a 2x1 with a 2x1 or a 1x2 with a 1x2 vector product. I'm not very sure about which order is correct.

Make sure that your title reflects the content of the question to get better visibility in these websites. Your title is about concepts but the question is about correcting the MATLAB code.

• The problem was, that, I was passing X and Y as matrices, which my_gabor_filter(..) cannot handle correctly, so instead I evaluated my_gabor_filter at each point (x,y) with x in X and y in Y, and so the modified version of my_gabor_filter is as follows : NOTE : I also adjusted the range for X and Y so that the mesh-grid is clearly seen Jul 27 '14 at 7:19
• Thanks to you : I learnt the difference bertween ab and a.*b in MATLAB. The first one is for matrix multiplication and the second one is to multiply matrices index-wise. Also I learnt the difference between inner product and outer product. Here ab works because you've arranged it as a matrix multiplication. One suggestion would be to utilize the inner product and dot product functions directly bcoz' the inner product definition may sometimes involve complex conjugation. Jul 27 '14 at 14:36