# (Tool) GaborFilter

GaborFilter is a tool to perform Gabor filtering on images.

The source code is hosted at the GaborFilter GitHub.

I learned about Gabor filters while working on my Bochum Project.

## The Gabor filter

A Gabor filter can have any number of dimension. Below the formula (and a bit of derivation) for the one-dimensional and two-dimensional Gabor filters are described.

## The one-dimensional Gabor filter formula

The function of a one-dimensional Gabor filter is:

 ``` f_Gabor(x) = cos(frequency*x) * exp(-(x*x)/(sigma*sigma)) ```

In the range [-5,5] a nice combination of values is frequency = 4.0 and sigma = 2.0:

 ``` f_Gabor = cos(4.0*x) * exp(-(x*x)/(2.0*2.0)) ```

The one-dimensional Gabor filter formula consists out of three parts:

1. the cosine part
2. the Gaussian function part
3. a constant

These three parts are described in detail below.

### 1. the cosine part

The cosine part of a Gabor function is dependent on distance and frequency

### 2. the Gaussian function part

the Gaussian function part of a Gabor function is dependent on distance and sigma.

A Gaussian function has the form

 ``` F_Gauss(x)   = a * exp(-((x-b)*(x-b))/(2*c*c)) ```

The Gaussian function used in the one-dimensional Gabor filter has

 ``` a = 1 b = 0 c = sigma ```

which results in

 ``` f_Gabor_part2   = 1.0 * exp(-((x-0.0) * (x-0.0))/(2.0 * sigma * sigma))   = exp(-(x * x)/(2.0 * sigma * sigma)) ```

### 3. a constant

The third term of a Gabor function is a constant that might be used to make the Gaussian integral (that is, the surface underneath the Gaussian function) equal to 1.0.

I have set this term to one, because in my case, the Gaussian integral does not matter, due to rescaling.

Continue reading to find out how to make the Gaussian integral (that is, the surface underneath the Gaussian function) equal to 1.0:

A Gaussian function has the form

 ``` f_Gauss(x)   = a * exp(-((x-b)*(x-b))/(2*c*c)) ```

and has the Gaussian integral of

 ``` F_Gauss(x) = a * c * sqrt(pi) ```

Because in the Gabor filter

 ``` a = 1 b = 0 c = sigma ```

the integral becomes

 ``` F_Gabor_part2(x)   = 1 * sigma * sqrt(pi)   = sigma * sqrt(pi) ```

This integral denotes the surface underneath a one-dimensional Gaussian function. To make the surface underneath the one-dimensional Gaussian function 1.0, one needs multiply the Gaussian function by the reciprocal of the integral.

## The two-dimensional Gabor filter formula

The function of a two-dimensional Gabor filter is:

 ``` f_Gabor(x)   = cos(frequency*x)   * exp(-(x*x)/(sigma*sigma)) ```

In the range [-5,5] a nice combination of values is frequency = 4.0 and sigma = 1.5:

 ``` F_Gabor   = cos(4*x) * exp(-(x*x)/(1.5*1.5)) * (1 / (1.5 * sqrt(3.14159265))) ```

The two-dimensional Gabor filter formula consists out of three parts:

1. the cosine part
2. the Gaussian function part
3. a constant

These three parts are described in detail below.

### 1. the cosine part

The cosine part of the two-dimensional Gabor filter formula is dependent on distance and frequency.

### 2. the Gaussian function part

The Gaussian function part of the two-dimensional Gabor filter formula is dependent on distance and sigma.

A Gaussian function has the form

 ``` F_Gauss(x,y)   = a * exp(-   ( ( (x-mid_x)*(x-mid_x))/(2*sigma_x * sigma_x) )   + ( (y-mid_y)*(y-mid_y))/(2*sigma_y * sigma_y) ) ) ```

The Gaussian function used in the two-dimensional Gabor filter has

 ``` a = 1 sigma_x = sigma_y = sigma mid_x = midy = 0 ```

which results in

 ``` F_Gabor_part2(x,y)   = exp(-   ( ( (x*x)/(2 * sigma * sigma) )   + ( (y*y)/(2 * sigma * sigma) ) ) ```

### 3. a constant

The third term is a constant that might make the two-dimensional Gaussian integral (that is, the volume underneath the the Gaussian function) equal to 1.0.

I have set this term to one, because in my case, the Gaussian integral does not matter, due to rescaling.

Furthermore, there is no formula (known to me) that allows the calculate the two-dimensional Gaussian integral. Therefore, only by approximation this value's constant can be found.

## Calculating sigma

For the one-dimensional and two-dimensional Gabor filters I calculate a value of sigma for the filter's size. Sigma determines how fast the Gaussian function starts approaching zero.

If a filter has a size of s pixels, it would be inefficient to have the Gaussian function already approaching zero at s/2. It would be strange that at the edges of a filter, the Gaussian function still have high values.

In this part, I calculate sigma from the filter's size, that the Gaussian function yields a value of 1/510 at the edges. I choose the value of 1/510, because in viewing a filter as a bitmap (with grey values from [0,255], these values will appear as black/zero due to rounding off.

## The one-dimensional Gabor filter formula

The Gaussian function part (part 2, as described above) of a Gabor filter, has the following formula:

 ``` f_Gabor_part2(x) = exp(-(x * x)/(2.0 * sigma * sigma)) ```

Assume a filter size and halve it to obtain s (s is like a radius), then

 ``` x = s f_Gabor_part2(x = s)   = exp(-(s * s)/(2.0 * sigma * sigma))   = 1/510 exp(-(s * s)/(2.0 * sigma * sigma)) = 1/510 -(s * s)/(2.0 * sigma * sigma) = ln(1/510) -(s * s)/ln(1/510) = 2.0 * sigma * sigma -(s * s)/(2.0*ln(1/510)) = sigma * sigma sigma = sqrt(-(s * s) / (2.0*ln(1/510))) ```

Which, by approximation is:

 ``` sigma = sqrt(-(s * s) / -12.46882145143674291132554261896) sigma = sqrt( (s * s) / 12.46882145143674291132554261896) ```

## External links This page has been created by the tool CodeToHtml