Question

I'm trying to write an algorithm in C that computes a convolution matrix of a gabor filter to be applied to an image. Right now, I just need the convolution matrix.

So, I'm using this equation: http://i.stack.imgur.com/APMC7.png

I mapped it to this little piece of code:

#include <stdio.h>
#include <math.h>
#define PI 3.14159265

int main(){
    int kernelSize = 5;
    float kernel[kernelSize][kernelSize];
    int x, y;
    //gamma = aspect ratio
    //lambda = wavelength
    //theta = orientation
    float xz, yz, theta, sigma, gamma, lambda;
    theta = 0;
    sigma = 1;
    lambda = 1;
    gamma = 1;

    for(x = 0; x < kernelSize; x++){
        for(y = 0; y < kernelSize; y++){
            xz = x * cos(theta) + y * sin(theta);
            yz = -x * sin(theta) + y * cos(theta);
            kernel[x][y] = exp(-(xz*xz + gamma*gamma*yz*yz)/(2*sigma*sigma)) * cos(2*PI*xz/lambda);
        }
    }
    for(x = 0; x < kernelSize; x++){
        for(y = 0; y < kernelSize; y++){
            printf("%.2f ", kernel[x][y]);
        }
        printf("\n)");
    }
    return(0);

}

The output with these parameters are:

1.00 0.61 0.14 0.01 0.00
0.61 0.37 0.08 0.01 0.00 
0.14 0.08 0.02 0.00 0.00 
0.01 0.01 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 0.00

My questions are: 1 - is it a valid convolution matrix for a gabor filter? If I convolute it with an image, will it apply the gabor filter? 2 - Should I sum all matrix values and normalize it? 3 - Is there anything I should fix in it?

Thanks in advance!

Was it helpful?

Solution

I don't know C so I'm not able to comment on your code, but:

  1. By definition, a gabor filter should have a real part and an imaginary part - i.e. complex valued.
  2. Your kernel doesn't look anything like a gabor kernel. It should look like a sinusoid multiplied by a gaussian - yours looks like a gaussian centred in the top left corner. No need to normalise it.

I would also tweak your parameters so they aren't unitary. Plot your filter and see if it looks like what you get when you search for 'Gabor wavelet' on Google.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top