Hi to all. It's my first post here. I want to ask about 2D Gabor filter implementation. I have some code but it is not working. So let me present my work first:

signed int sumX, sumY;
    long int SUM = 0;
    int I = 0, J = 0;
    double theta = 0;

    unsigned char *pixels;
    unsigned char *myEdgeDetectedImage;
    float *thetaArray;
    float *gaborArray;
    signed char *imageAfterGabor;

    // Memory allocation
    pixels = malloc(width * height);
    myEdgeDetectedImage = malloc(width * height);
    thetaArray = malloc(sizeof(float *) * width * height);
    gaborArray = malloc(sizeof(float *) * width * height);
    imageAfterGabor = malloc(width * height);

    /*
    
    (...) some code
    
    */


    /*-----------------------------------
            Sobel Mask Algorithm
    -----------------------------------*/
    for(row=1; row<height-1; row++) {
        for(col=1; col<width-1; col++) {
            sumX = 0;
            sumY = 0;
            SUM = 0;
            
            for(I=-1; I<=1; I++) {
                for(J=-1; J<=1; J++) {
                    // Couting gradients
                    sumX = sumX + pixels[(row+I)*width + (col+J)]*Gx[I+1][J+1];
                    sumY = sumY + pixels[(row+I)*width + (col+J)]*Gy[I+1][J+1];
                }
            }

            theta=0;
            theta = atan2((double)sumY, (double)sumX);
            
            // Making an array of counted arrays
            // that i will use later
            thetaArray[row*width + col] = theta;
        }
    }
    
    /*-----------------------------------
                Gabor Filter
     -----------------------------------*/
    for(row=0; row<height; row++) {
        for(col=0; col<width; col++) {
            x_theta = row*sin(thetaArray[row*width + col]) + col*cos(thetaArray[row*width + col]);
            y_theta = col*sin(thetaArray[row*width + col]) - row*cos(thetaArray[row*width + col]);
            
            exp_temp = -0.5*(((x_theta*x_theta)/((double)sigmaX*(double)sigmaX)) + ((y_theta*y_theta)/((double)sigmaY*(double)sigmaY)));
            
            gaborArray[row*width + col] = exp(exp_temp) * cos(2.0*M_PI*(1/ridgeFrequency)*x_theta);
        }
    }
    
    /*-----------------------------------
                Convolution
     -----------------------------------*/
    for(row=2; row<height-2; row++) {
        for(col=2; col<width-2; col++) {
            gaborTemp = 0;
            for (I=-2; I<=2; I++) {
                for (J=-2; J<=2; J++) {
                    gaborTemp += pixels[(row+I) + (col+J)*width] * gaborArray[(row+I) + (col+J)*width];
                }
            }
            imageAfterGabor[row*width + col] = gaborTemp;
        }
    }

So i have this code but it doesn't work. Some strange values are coming after calculations. Values stored in gaborArray looks strange:

gabor=0.882496902584595455110161310586
gabor=0.754839601989007347171423134569
gabor=0.606530659712633424263117376540
gabor=0.457833361771614266721996955312
gabor=0.324652467358349738901779346634
gabor=0.216265166829887306443325201144
gabor=0.135335283236612702317813727859
gabor=0.079559508718227686663304609738
gabor=0.043936933623407420368423004220
gabor=0.022794180883612343707644853907

This values are only samples. As you can see all values are decreasing, but what is worst - the are goning to 0. This numbers should be point values of gray-scale image. I don't know where is error so please someone to help. Any hint is appreciated :)

Leave the code which you have tested to be working and restart from there testing additions to program properly. Refactor code to functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.