Sobel Operator C - 边缘检测出错了

时间:2014-01-10 22:16:44

标签: c binary 2d gradient edge-detection

我一直在努力使用Sobel算子计算C中二进制图像的梯度(和倾角)。我已经多次检查过操作员并经历了大量的互联网站点。不过,我不得不承认我没有图像处理的经验,而且我是C编码的新手。我没有收到任何错误消息,但结果没有显示边缘上所需的渐变。 不知怎的,没有计算x方向的梯度 - 但为什么呢?

感谢您的帮助!

#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

1 个答案:

答案 0 :(得分:3)

显而易见的问题是你的卷积内核没有移动 - 数组索引都是单独的变量,需要更新才能与ij保持同步。在循环内移动赋值应该可以解决:

...
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;
...