Search code examples
cbinary2dgradientedge-detection

Sobel Operator C - Edge detection gone wrong


I ve been trying hard to calculate the gradient (and dips) of a binary image in C using the Sobel operator. I ve already checked the operators several times and went through tons of internet sites. Nevertheless, I have to admit that I have no experience with image processing and I am quite a rookie in C coding. I dont get any error messages, but the result does not show the desired gradients on the edges. Somehow the gradient in the x-direction is not calculated - but why?

Thanks for the help!

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

FILE *infile, *outfile;


int main(int argc, char *argv[])
{
    int nx,nz,k,i,nu;
    int j,m,l;
    int km, kp, im, ip, kpp, ipp;
    float Gx, Gz, G, Gmax, Gmin;
    float T;
    float **dip;
    float pi;
    float *tmp;                                      
    float *bufz;                                      
    float **temp1;

    char *velfile_in = "vel";//"vel_315_273";
    char *velfile_out = "dip";


    void *alloc1 (size_t n1, size_t size);            
    void **alloc2(size_t n1, size_t n2, size_t size); 
    void ***alloc3(size_t n1, size_t n2, size_t n3, size_t size);


    pi = 4. * atan(1.);

    // Initiate constants
    T = atof(argv[1]);
    nx = atoi(argv[2]);
    nz = atoi(argv[3]);

    Gmax = 0.;
    Gmin = 10e8;

    // border handling (cyclic) 
    km = (k+nz-1) % nz;
    kp = (k+1) % nz;
    kpp = (k+2) % nz;
    im = (i+nx-1) % nx;
    ip = (i+1) % nx;
    ipp = (i+2) % nx;


    // allocate 1D, 2D and 3D arrays
    tmp = (float *)alloc1(nz,sizeof(float));
    bufz = (float *)alloc1(nz,sizeof(float));
    temp1 = (float **)alloc2(nx,nz,sizeof(float));
    dip = (float **)alloc2(nx,nz,sizeof(float));


    //READ FILE
    //***********************************************************
    infile = fopen(velfile_in, "r");
    if (infile == NULL) err("Error: could not open file.");
    for (i=0; i<nx; i++) {
        nu = fread(tmp,sizeof(float),nz,infile);
        for (k=0; k<nz; k++) {
            temp1[k][i] = tmp[k];
        }
    }
    fclose(infile);


    // APPLY SOBEL****************************************

    for (i = 0; i < nx; i++)
    {
        for (k = 0; k < nz; k++)
        {

            Gx = (temp1[km][im] - temp1[km][ip] + 2 * temp1[k][im] - 2 * temp1[k][ip] + temp1[kp][im] - temp1[kp][ip]); 
            Gz = (temp1[km][im] - temp1[kp][im] + 2 * temp1[km][i] - 2 * temp1[kp][i] + temp1[km][ip] - temp1[kp][ip]); 


            G = sqrtf(Gx * Gx + Gz * Gz);
            Gmax =  (Gmax > G ? Gmax : G);
            Gmin =  (Gmin < G ? Gmin : G);  

            dip[k][i] = abs(atan(Gz/Gx) * 180. / pi);

            printf("(%d,%d)\tGx:%5.3f\tGz%5.3f\tG%5.3f\n",i,k,Gx,Gz,G);

        }
    }

    printf("Gmax:%5.3f\tGmin:%5.3f\n",Gmax,Gmin);



    // write file ********************************************************
    outfile = fopen(velfile_out,"w");
    for (i=0; i<nx; i++) {
        for (k=0; k<nz; k++) bufz[k] = dip[k][i];
        fwrite(bufz,sizeof(float),nz,outfile);
    }

    fclose(outfile);

    return 0;
}

Input Image Result of Dip


Solution

  • The obvious problem is that your convolution kernel isn't moving - the array indices are all separate variables and need updating to stay in sync with i and j. Moving the assignments inside the loops should fix that:

    ...
    for (i = 0; i < nx; i++)
    {
        im = (i+nx-1) % nx;
        ip = (i+1) % nx;
        ipp = (i+2) % nx;
    
        for (k = 0; k < nz; k++)
        {
            km = (k+nz-1) % nz;
            kp = (k+1) % nz;
            kpp = (k+2) % nz;
    ...