繁体   English   中英

C中的边缘检测

[英]Edge Detection in C

从事边缘检测 function。回顾我的代码,我认为我有概念/逻辑。 但结果并没有以应有的方式出现。

typedef struct {
    int Red;
    int Green;
    int Blue;
} GTOTALS;

// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
    const int MAX = 3;

    // Copy Image
    RGBTRIPLE Copy[height][width];
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            Copy[i][j] = image[i][j];
        }
    }

    // Gx and Gy Grids 3 x 3
    int Gx[MAX][MAX] = {
        {-1, 0, 1},
        {-2, 0, 2},
        {-1, 0, 1}
    };
    int Gy[MAX][MAX] = {
        {-1, -2, -1},
        {0, 0, 0},
        {1, 2, 1}
    };

    // Loop through each pixel
    for (int Rows = 0; Rows < height; Rows++)
    {
        for (int Cols = 0; Cols < width; Cols++)
        {
            // Hold RGB Values + Refresh Current Pixel RGB
            int CRed = 0, CGreen = 0, CBlue = 0;
            // Store Gx and Gy RGB Values
            GTOTALS X;
            GTOTALS Y;

            // Loop through surrouding pixels
            for (int S_Rows = Rows - 1, R = 0; S_Rows <= Rows + 1; S_Rows++, R++)
            {
                for (int S_Cols = Cols - 1, C = 0; S_Cols <= Cols + 1; S_Cols++, C++)
                {
                    // Check Pixel Validity
                    if ((S_Rows >= 0) && (S_Rows < height) && (S_Cols >= 0) && (S_Cols < width))
                    {
                        // RGB Gx Total Values
                        X.Red += Copy[S_Rows][S_Cols].rgbtRed * Gx[R][C];     // Current Pixel Red * Gx[N][N]
                        X.Green += Copy[S_Rows][S_Cols].rgbtGreen * Gx[R][C]; // Current Pixel Green * Gx[N][N]
                        X.Blue += Copy[S_Rows][S_Cols].rgbtBlue * Gx[R][C];   // Current Pixel Blue * Gx[N][N]

                        // RGB Gy Total Values
                        Y.Red += Copy[S_Rows][S_Cols].rgbtRed * Gy[R][C];     // Current Pixel Red * Gy[N][N]
                        Y.Green += Copy[S_Rows][S_Cols].rgbtGreen * Gy[R][C]; // Current Pixel Green * Gy[N][N]
                        Y.Blue += Copy[S_Rows][S_Cols].rgbtBlue * Gy[R][C];   // Current Pixel Blue * Gy[N][N]
                    }
                }
            }

            // Value = Square Root(Gx^2 + Gx^2)
            CRed = round( sqrt( pow(X.Red, 2.0) + pow(Y.Red, 2.0) ) );
            CGreen = round( sqrt( pow(X.Green, 2.0) + pow(Y.Green, 2.0) ) );
            CBlue = round( sqrt( pow(X.Blue, 2.0) + pow(Y.Blue, 2.0) ) );

            // MAX 255
            Cap(&CRed);
            Cap(&CGreen);
            Cap(&CBlue);

            // Update Target Pixel
            image[Rows][Cols].rgbtRed = CRed;
            image[Rows][Cols].rgbtGreen = CGreen;
            image[Rows][Cols].rgbtBlue = CBlue;
        }
    }

    return;
}


void Cap(int *Value)
{
    if (*Value > 255)
    {
        *Value = 255;
    }
}

当我运行该程序时,大多数 RGB 值变成了 255。我试过使用不同的数据类型并在创建变量时四处移动,但这似乎没有帮助。 我也试过代码的微型版本,一切似乎都按预期工作,但不确定为什么当我把它加在一起时似乎没有给出正确的结果

这是Sobel过滤器的描述

// 从源到目标 int ComputeBoundaries(unsigned char S[], unsigned char D[]) {

无符号整型 iX,iY; /* 二维虚拟数组(图像)的索引 = integer 坐标/ unsigned int i; /一维数组的索引// sobel 过滤器 */ unsigned char G, Gh, Gv; // 边界在 D 数组中(全局变量)

// 清除 D 数组 memset(D, iColorOfBasin1, iSize*sizeof(*D)); // 对于堆分配的 arrays,其中 N 是元素的数量 = FillArrayWithColor(D, iColorOfBasin1);

// printf("使用 Sobel 过滤器在 S 数组中查找边界\n");
#pragma omp parallel for schedule(dynamic) private(i,iY,iX,Gv,Gh,G) shared(iyMax,ixMax) for(iY=1;iY<iyMax-1;++iY){ for(iX= 1;iX<ixMax-1;++iX){ Gv= S[Give_i(iX-1,iY+1)] + 2 S[Give_i(iX,iY+1)] + S[Give_i(iX-1, iY+1)] - S[Give_i(iX-1,iY-1)] - 2 S[Give_i(iX-1,iY)] - S[Give_i(iX+1,iY-1)]; Gh= S[Give_i(iX+1,iY+1)] + 2 S[Give_i(iX+1,iY)] + S[Give_i(iX-1,iY-1)] - S[Give_i(iX+1 ,iY-1)] - 2 S[Give_i(iX-1,iY)] - S[Give_i(iX-1,iY-1)]; G = sqrt(Gh Gh + Gv Gv); i= 给_i(iX,iY); /* 根据二维数组的索引计算一维数组的索引/ if (G==0) {D[i]=255;} / background / else {D[i]=0;} / boundary */ } }

返回 0; }

// 从源复制到目标 int CopyBoundaries(unsigned char S[], unsigned char D[]) {

无符号整型 iX,iY; /* 二维虚拟数组(图像)的索引 = integer 坐标/ unsigned int i; /一维数组索引 */

//printf("将边界从 S 数组复制到 D 数组\n"); for(iY=1;iY<iyMax-1;++iY) for(iX=1;iX<ixMax-1;++iX) {i= Give_i(iX,iY); 如果 (S[i]==0) D[i]=0;}

返回 0; }

这是图像和完整的程序

结果: 在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM