# Understanding the Gabor filter function

I need to implement a script for generating features from an input image by using the Gabor filter. I have no past experience of wavelets and I'm just learning Fourier analysis (I understand the basic idea behind Fourier analysis and transform) so they can't help me to understand Gabor filter, because I need to have an implementation done in a week. I don't need to understand the foundations of the Gabor filter function, but I would want to understand to some extent of what it is and what does it do. What are the parameters? What do they mean? What is the output of the function? For example this is the formula I copied from Wikipedia:

$$g(x,y;\lambda, \theta, \psi, \sigma,\gamma) = \exp\left(-\frac{x'^2+\gamma^2 y'^2}{2\sigma^2}\right)\exp\left(i\left(2\pi\frac{x'}{\lambda}+\psi\right)\right)$$

Now my obvious question is: What does this mean? What does the variables mean? According to Wikipedia:

$x, y$: I assume these coordinates specify the pixel value of an image at coordinates $(x,y)$ (2. This is OK, understood)

$\lambda$: represents the wavelength of the sinusoidal factor (Sinusoidal factor, huh? 3a. How do you select it? 3b. Where does it come from? 3c. Is it an arbitrary number or what? Freely chosen?)

$\theta$: represents the orientation of the normal to the parallel stripes of a Gabor function (4. What does that mean?)

$\psi$: is the phase offset (5. offset of what? How is this value determined? Is it freely chosen?)

$\sigma$: is the sigma/standard deviation of the Gaussian envelope (6. Need more explanation...)

$\gamma$: is the spatial aspect ratio, and specifies the ellipticity of the support of the Gabor function (7. Again need more details and more explanation)

And most importantly:

$$g(x,y;\lambda, \theta, \psi, \sigma,\gamma) = X$$

1. What is the output value $X$? What does it mean?

As I mentioned I don't need thorough explanation of the theory, because I bet it is long and reading a 1000 page book on unknown subject is not an option for me right now. I need to have have a black-box understanding of this function so that I can implement it in code and most importantly understand what is the input and what is the output.

Thank you for any help!! =)

P.S.

https://math.stackexchange.com/questions/259877/value-of-x-y-in-computing-gabor-filter-function

but it doesn't answer my question thorough enough :)

• Looks like you're trying to learn a lot about image processing in just a week. Maybe you should start with a simpler filter first like the Haar wavelet. – Aaron Feb 28 '14 at 7:56
• Hi @anonymous! Can you enumerate what parameters are not properly explained? – visoft May 30 '16 at 11:42
• @visoft, Your answer is not a neat one. It involves a lot of extraneous discussions. I have added my answer. I think now it is clear. – user18425 May 30 '16 at 13:08
• You started a bounty because "In the current answer, meanings of various parameters are described in poorly worded form. To-the-point answer needed." I was curious what explanations need rephrasing. – visoft May 30 '16 at 13:10
• @visoft, Your answer is not a neat one. It involves a lot of extraneous discussions. I have added my answer. I think now it is clear. – user18425 May 30 '16 at 13:12

A matlab implementation is in this answer: https://dsp.stackexchange.com/a/14201/5737

1) The wikipedia formula is a little bit too general.

2) If you know the basics of the with Fourier transform then you empirically know that: The image is viewed as being formed by superimposing a series of sinusoidal waves of various frequencies oriented in all kind of directions. Each "pixel" in the transform tells us the "intensity" of such a wave. The position of the "pixel" tells us the frequency and orientation of the wave. In practice, one wants to select only certain waves, having a specific frequency and a specific orientation.

So there you have it: The Gabor transform is one of many so called band pass filters that allows you to "cut" the Fourier transform and isolate only specific information. Another important information is that each Fourier "pixel" is a complex value (real and imaginary part)

3) Parameters: Two parameters are already shown:

3.a) The tuning frequency $f_0$, or tuning period $P_0$, or $\lambda$ establish to what kind of sinus wave the filter will respond best. ( $f = 1/P_0 = 1/\lambda$ or $f=\pi/\lambda$ depending on the specific implementation ) Basically, a smaller $P_0$ means a denser sinus wave. A larger $P_0$ means larger waves. $P_0$ is in pixels (3, 5, 30, etc. pixels) Don't go under 3 pixels or beyond $W/2$ or you get nasty effects. $W$ is the width of the image, if the image is square. You specify this using P0 parameter in the matlab code.

3.b) Central angle. These waves can have any directions. You want to select only waves at specific angle. So, the second parameter is the tuning angle, $\theta_0$ or $\theta$ in your formula. Usually, in radians. Orient in matlab code.

One cannot isolate only a certain frequency or a certain orientation. (Search for uncertainty principle in the textbooks. Yes, is similar to the one in physics) But one can tune how much of the nearby frequencies will leak. The next two parameters specify that:

3.c) $\Delta F$, frequency bandwidth expressed in octaves. Usefull values, 1.5, 2, 3. Larger values means capturing a broader range of frequencies. There is a price for a tighter bandpass, a poorer spatial localization. Why, again, textbook. FBW in matlab code.

3.d) $\Delta \theta$ the angle bandwidth. Expressed in radians. $\pi/3$ or $\pi/2$ works just fine. ABW in matlab code.

The relation between $\Delta f$, $\Delta \theta$ from matlab code and $\sigma, \gamma$ from Wikipedia have a formula but is not essential for understanding Gabor. $\psi$ is again not important for basic understanding.

So you have it: $X$ from Wikipedia is a 2D matrix of numbers containing a convolution mask. You take the original image, filter it with the convolution mask and get another image. This new image is the "Gabor response" for the original image.

The matlab code does these two steps together. Constructs the Gabor filter with the specified parameters and performs convolution. The results ReConv,ImConv are the responses. Each response "pixel" have a real and an imaginary part. If you want to use this code you usually must compute the energy for the response: $E = \sqrt{a^2+b^2}$ where $a$ is the real part of the response (ReConv) and $b$ is the imaginary part (ImConv), for each pixel.

There you have it: 1) Build a Gabor filter specifying $P_0, \theta_0, \Delta f, \Delta \theta$ 2) Convolute your image with the filter. You will get two values per each pixel. 3) Compute the energy $E$ and get the intensity of the response for each pixel in the original image

Another intuition: Suppose you want to select edges stretching on an orientation perpendicular to $\pi/6$ and a certain width of 20 pixels. You can build a Gabor filter with $P_0 \approx 20, \theta_0 = \pi/6, \Delta f = 2, \Delta \theta = pi/2$. TAKE CARE that there is no equal relation between the dimension of your edge and the $P_0$ parameter. You should try various values and see what works best. The other parameters ($\Delta f, \Delta \theta$) touch them when you have some experience tuning the first two.

Hope it helps!

Cristi

Update

Here is a site which allows you to play a bit with Gabor parameters and note the results: http://www.cogsci.nl/pages/gabor-generator.php

After a quick look, both freqnency and angle bandwidths are tied together as "Standard Deviation in pixels ... to the Gauss envelope". The rest of the parameters are easily identified. However, note that there are numerical differences! Eg. the phase can be expressed in 0-1 interval or 0-$\pi$ interval.

• How does the kernel size depend on those parameters? It seems most implementations just use an arbitrary size. – jiggunjer Dec 6 '17 at 4:50
• Tl;dr: bandwidths control the widths of the kernel. It is an inverse proportional relation. Eg. narrower bandwidth -> larger filter. Also the central frequency have a role, too. – visoft Dec 6 '17 at 11:01
• When I'm using Gabor filters I first think about what are my parameters (bandwidths, central frequency, etc) and then "let" the formulas compute the dimensions. – visoft Dec 6 '17 at 11:05
• You can work it out the other way around, input the dimensions and compute the filter working parameters. – visoft Dec 6 '17 at 11:05
• Actually I see that the size relation is directly proportional to lambda (at least in matlab's gabor implementation). Their formula is a bit weird because it has a term (2^BW+1)/(2^BW-1) which is always equal to 4. They use 7*sigma (where sigma ~ lambda), wikipedia gabor example uses 3*sigma. – jiggunjer Dec 7 '17 at 7:27

If we see the definition of the Gabor filter, we will know that its impulse response is a product of a sinusoidal function and a Gaussian function, which is essentially what we see in the formula.

The parameters λ, θ, ψ are parameters for the sinusoidal part (or factor).

λ and ψ are basic parameters of a sine wave.

λ - controls the Wavelength (https://en.wikipedia.org/wiki/Wavelength) of this sinusoid. A higher λ would “widen” the ellipses you see in the image above, while a lower λ would have the opposite.

θ - controls the rotation of the ellipses you see in the image above. These ellipses are the “parallel stripes”. A value of θ = 0 indicates no rotation and would make the ellipses vertical.

ψ - is the phase shift of the sinusoid i.e., how much the ellipses need to be shifted with respect to the center. It’s a parameter that has to be specified.

σ - is the standard deviation (i.e., the spread) of the Gaussian part.

γ - controls the aspect ratio of the ellipses (roughly, how elongated the ellipses need to be).

The various parameters specified above are the inputs.

The output value of the calculation is simply the co-efficient (or weight) of the filter at the (x, y) location.

This is the meaning, and is the same in the context of any other linear filter used in image processing.

Someone made an outstanding video explaining the effects of different parameters of Gabor filter. You can find the video here.

Hope this helps.