# Gabor filter in c

• February 21st, 2012, 06:29 AM
Bibhukalyana
Gabor filter in c
Hi everyone,
I am trying to apply gabor filter to a fingerprint for fingerprint enhancement using c.
My image size is 280x340.Gabor filter equation is :

F(x,y) = exp(-0.5*[x^2/sigma_x^2 + y^2/sigma_y^2]) * cos(2*PI*f*x);

I am able to execute this equation.But my problem is i am calculating the equation for 280x340 times and it is taking time(4 sec 2.93GHz 3GB ram).

Can any one told me how to reduce the executation time ?
What I know exp function is taking time. So is there any equivalent of exp which will take less time to execute ?

bibhu.
• February 21st, 2012, 08:07 AM
D_Drmmr
Re: Gabor filter in c
Quote:

Originally Posted by Bibhukalyana
Can any one told me how to reduce the executation time ?

Not without seeing your code, of course.
Quote:

Originally Posted by Bibhukalyana
What I know exp function is taking time. So is there any equivalent of exp which will take less time to execute ?

You could try using a lookup table if you don't mind loosing some precision and the input values are within a reasonably small range.
• February 21st, 2012, 11:46 PM
Bibhukalyana
Re: Gabor filter in c
Thanks D_Drmmr,

I am sorry for code.
Here is my code.The orient array contain orientation(theta) of fingerprint.

Code:

``` int Gabor_filter(unsigned char **ImageData, int width, int height, int **orient,unsigned char **OPData) {     int i,j,m,n,u,v,wx = 11,wy = 11;     double G_Filter ;     double sigma_x,sigma_y,x_p,y_p,f,x,y;     double sum =0;     double cos_val[8],sin_val[8];     cos_val[0] = 1;cos_val[1] = 0.923879;cos_val[2] = 0.707106;cos_val[3] = 0.382683;     cos_val[4] = 0;cos_val[5] = -0.382683;cos_val[6] = -0.707106;cos_val[7] = -0.923879;     sin_val[0] = 0;sin_val[1] = 0.382683;sin_val[2] = 0.707106;sin_val[3] = 0.923879;     sin_val[4] = 1;sin_val[5] = 0.923879;sin_val[6] = 0.707106;sin_val[7] = 0.382683;       for(i = wy/2; i < height - wy/2; i++)     {         for(j = wx/2; j < width - wx/2; j++)         {             sigma_x = 0.04;             sigma_y = 0.04;             f = 0.01;                         sum = 0;             for(u = 0; u < wy; u++)             {                 for(v = 0; v < wx; v++)                 {                     x = x_p = v*fabs(cos_val[orient[i][j]]) + u*fabs(sin_val[orient[i][j]]);                     y = y_p = -v*fabs(sin_val[orient[i][j]]) + u*fabs(cos_val[orient[i][j]]);                     x_p = x_p*x_p;                     y_p = y_p*y_p;                     G_Filter = exp(-0.5*((x_p/sigma_x) + (y_p/sigma_y)))*cos(2*PI*f*x);                     sum += G_Filter * (double)ImageData[i-wy/2+u][j-wx/2+v];                 }             }             OPData[i][j] = sum;         }     }     return 0; }```

"You could try using a lookup table ......"

bibhu.
• February 22nd, 2012, 04:30 AM
D_Drmmr
Re: Gabor filter in c
Quote:

Originally Posted by Bibhukalyana
Thanks D_Drmmr,

I am sorry for code.
Here is my code.The orient array contain orientation(theta) of fingerprint.

1. Being consistent in the use of spaces around operators. It's normal to always put a space between a binary operator and its operands.
2. Placing assignments where they belong. The value of sigma_x is independent of i and j, so you should place it outside the loop. Also, you should make it a const variable if possible.

Quote:

Originally Posted by Bibhukalyana
Code:

```    for(i = wy/2; i < height - wy/2; i++)     {         for(j = wx/2; j < width - wx/2; j++)         {             sigma_x = 0.04;             sigma_y = 0.04;             f = 0.01;                         sum = 0;             for(u = 0; u < wy; u++)             {                 for(v = 0; v < wx; v++)                 {                     x = x_p = v*fabs(cos_val[orient[i][j]]) + u*fabs(sin_val[orient[i][j]]);                     y = y_p = -v*fabs(sin_val[orient[i][j]]) + u*fabs(cos_val[orient[i][j]]);                     x_p = x_p*x_p;                     y_p = y_p*y_p;                     G_Filter = exp(-0.5*((x_p/sigma_x) + (y_p/sigma_y)))*cos(2*PI*f*x);                     sum += G_Filter * (double)ImageData[i-wy/2+u][j-wx/2+v];                 }             }             OPData[i][j] = sum;         }     }     return 0; }```

You are recalculating a lot of things in the inner loop that don't change. The first thing you should do is take fabs(cos_val[orient[i][j]]) out of these loops, even if it's just for readability.
Second, you can use a pointer instead of ImageData[i-wy/2+u][j-wx/2+v]. Something like
Code:

```//... for(u = 0; u < wy; u++) {     unsigned char* pImageData = &ImageData[i-wy/2+u][j-wx/2];     for(v = 0; v < wx; v++, ++pImageData) {         //...         sum += G_Filter * (*pImageData);     } }```
Such changes could give a small to medium performance improvement. A more substantial speed up would require to rethink the problem, such that a lot of work is taken outside of the innermost loop. This may be possible by refactoring the sum you are calculating for each i and j.
Quote:

Originally Posted by Bibhukalyana
"You could try using a lookup table ......"

bibhu.

Putting in a little effort to search the internet doesn't hurt you, you know. ;) http://en.wikipedia.org/wiki/Lookup_table

In the code you posted, there are only 8 * 11 * 11 < 1000 possible different inputs for the value of G_Filter. You can easily pre-calculate those before the outermost loop and store them in an array. Then the body of the innermost loop becomes nothing more than two lookups, a multiplication and an addition. That'll likely make a big difference.

Some general remarks:
- Are you timing a fully optimized build?
- Since it seems that you don't care about precision very much (given the amount of decimals in the hard coded values), you should try using floats instead of doubles.
• February 22nd, 2012, 07:48 AM
Bibhukalyana
Re: Gabor filter in c
Thanks D_Drmmr

Now it is working faster.

bibhu.
• February 23rd, 2012, 04:27 AM
Paul McKenzie
Re: Gabor filter in c
Quote:

Originally Posted by Bibhukalyana
"You could try using a lookup table ......"

bibhu.

You are using a lookup table right here:
Code:

``` cos_val[0] = 1;cos_val[1] = 0.923879;cos_val[2] = 0.707106;cos_val[3] = 0.382683;     cos_val[4] = 0;cos_val[5] = -0.382683;cos_val[6] = -0.707106;cos_val[7] = -0.923879;     sin_val[0] = 0;sin_val[1] = 0.382683;sin_val[2] = 0.707106;sin_val[3] = 0.923879;     sin_val[4] = 1;sin_val[5] = 0.923879;sin_val[6] = 0.707106;sin_val[7] = 0.382683;```
Where did those values for sin and cos come from? You didn't compute them in your program, right? So there is a lookup table right there in your own program.

Regards,

Paul McKenzie
• April 18th, 2013, 10:34 AM
jagz
Re: Gabor filter in c
Hi Bibhukalyana,

I am interested in Gabor Filter for a Image. I would like to know what would (int ** orient) contain for a mxn image. I would wait for your response.

Regards,
Jagmohan
• January 31st, 2018, 05:37 AM
2kaud
Re: Gabor filter in c
Hi Bibhukalyana,

I am using a gabor filter to enhance the fingerprint image. I applied your code listed above to the fingerprint image, but the resulting image looks exactly like the fingerprint image I originally entered, the gabor filter does not seem to work. I do not know where the problem is, I need someone to help me because this problem has bothered me for three weeks and I can not solve it.
Can you give me your complete source code ? thank you for your help, appreciated.

[Post from report sent to moderators]
• January 31st, 2018, 05:37 AM
simon69
Re: Gabor filter in c
Hi Bibhukalyana,

I am using a gabor filter to enhance the fingerprint image. I applied your code listed above to the fingerprint image,
but the resulting image looks exactly like the fingerprint image I originally entered, the gabor filter does not seem to work.
I do not know where the problem is, I need someone to help me, because this problem has bothered me for three weeks and I can not solve it. Can you give me your complete source code ? thank you for your help, appreciated.

Regards,
Simon
• January 31st, 2018, 05:39 AM
2kaud
Re: Gabor filter in c
I don't think Bibhukalyana is active on these forums now - and re-activating a 5-year old thread with a question is not likely to help.

If you have an issue and want advice, it would be better to start a new thread, explain the issue and post your current code.

Cheers!