CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2011
    Posts
    4

    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.

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Gabor filter in c

    Quote Originally Posted by Bibhukalyana View Post
    Can any one told me how to reduce the executation time ?
    Not without seeing your code, of course.
    Quote Originally Posted by Bibhukalyana View Post
    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

  3. #3
    Join Date
    May 2011
    Posts
    4

    Arrow 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 ......"

    Please explain me in details.


    bibhu.

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Gabor filter in c

    Quote Originally Posted by Bibhukalyana View Post
    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.

    Quote Originally Posted by Bibhukalyana View Post
    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 View Post
    "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 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.
    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

  5. #5
    Join Date
    May 2011
    Posts
    4

    Re: Gabor filter in c

    Thanks D_Drmmr

    Now it is working faster.

    bibhu.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Gabor filter in c

    Quote Originally Posted by Bibhukalyana View Post
    "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

  7. #7
    Join Date
    Apr 2013
    Posts
    1

    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

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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]
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Jan 2018
    Posts
    1

    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

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured