
February 21st, 2012, 06:29 AM
#1
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 ?
tanks in advance.
bibhu.

February 21st, 2012, 08:07 AM
#2
Re: Gabor filter in c
Originally Posted by Bibhukalyana
Can any one told me how to reduce the executation time ?
Not without seeing your code, of course.
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.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it.  P. D. Ouspensky

February 21st, 2012, 11:46 PM
#3
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[iwy/2+u][jwx/2+v];
}
}
OPData[i][j] = sum;
}
}
return 0;
}
"You could try using a lookup table ......"
Please explain me in details.
bibhu.

February 22nd, 2012, 04:30 AM
#4
Re: Gabor filter in c
Originally Posted by Bibhukalyana
Thanks D_Drmmr,
I am sorry for code.
Here is my code.The orient array contain orientation(theta) of fingerprint.
You can make your code a lot more readable by:
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.
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[iwy/2+u][jwx/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[iwy/2+u][jwx/2+v]. Something like
Code:
//...
for(u = 0; u < wy; u++) {
unsigned char* pImageData = &ImageData[iwy/2+u][jwx/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.
Originally Posted by Bibhukalyana
"You could try using a lookup table ......"
Please explain me in details.
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 precalculate 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.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it.  P. D. Ouspensky

February 22nd, 2012, 07:48 AM
#5
Re: Gabor filter in c
Thanks D_Drmmr
Now it is working faster.
bibhu.

February 23rd, 2012, 04:27 AM
#6
Re: Gabor filter in c
Originally Posted by Bibhukalyana
"You could try using a lookup table ......"
Please explain me in details.
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
#7
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
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!
