CS50 PSET4 Sobel滤波器-边缘(边界像素)

时间:2020-10-18 06:38:11

标签: c image-processing cs50 imagefilter sobel

我为边缘(Sobel滤波器)编写了CS50 Pset4的以下代码

以下是CS50 Pset4问题描述的链接(仅边缘部分):https://cs50.harvard.edu/x/2020/psets/4/filter/more/

我编写的代码显示了sobel滤镜图像,但是我的问题仅在于边框像素。

这是我的代码:

// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE imgCopy[height][width];

    float gxRed;
    float gxGreen;
    float gxBlue;

    float gyRed;
    float gyGreen;
    float gyBlue;

    float gRed;
    float gGreen;
    float gBlue;



    for (int h = 0; h < height; h++)
    {
        for (int w = 0; w < width; w++)
        {
            imgCopy[h][w].rgbtRed = image[h][w].rgbtRed;
            imgCopy[h][w].rgbtGreen = image[h][w].rgbtGreen;
            imgCopy[h][w].rgbtBlue = image[h][w].rgbtBlue;
        }
    }

    for (int h = 0; h < height; h++)
    {
        for (int w = 0; w < width; w++)
        {


            // Top left Corner
            if (h == 0 && w == 0)
            {
                // For Gx and Gy values imagine a 3x3 matrix at the corner and any pixel outside the corner is considered to be equal zero
                //(The corner pixels does not equal 0 you treat the corner pixel as a 3x3 matrix same as the middle pixels and get the Gx and Gy values for them)
                gxRed = 2 * imgCopy[h][w + 1].rgbtRed + imgCopy[h + 1][w + 1].rgbtRed;
                gxGreen = 2 * imgCopy[h][w + 1].rgbtGreen + imgCopy[h + 1][w + 1].rgbtGreen;
                gxBlue = 2 * imgCopy[h][w + 1].rgbtBlue + imgCopy[h + 1][w + 1].rgbtBlue;

                gyRed = 2 * imgCopy[h + 1][w].rgbtRed + imgCopy[h + 1][w + 1].rgbtRed ;
                gyGreen = 2 * imgCopy[h + 1][w].rgbtGreen + imgCopy[h + 1][w + 1].rgbtGreen;
                gyBlue = 2 * imgCopy[h + 1][w].rgbtBlue + imgCopy[h + 1][w + 1].rgbtBlue;

                gRed = sqrt((gxRed * gxRed) + (gyRed * gyRed));
                gGreen = sqrt((gxGreen * gxGreen) + (gyGreen * gyGreen));
                gBlue = sqrt((gxBlue * gxBlue) + (gyBlue * gyBlue));

                gRed = round(gRed);
                gGreen = round(gGreen);
                gBlue = round(gBlue);

                if(gRed > 255)
                {
                    gRed = 255;
                }
                if(gGreen > 255)
                {
                    gGreen = 255;
                }
                if(gBlue > 255)
                {
                    gBlue = 255;
                }
                image[h][w].rgbtRed = gRed;
                image[h][w].rgbtGreen = gGreen;
                image[h][w].rgbtBlue = gBlue;
                }

            // Top-right corner
            else if ( h == 0 && w == width - 1 )
            {
                // For Gx and Gy values imagine a 3x3 matrix at the corner and any pixel outside the corner is considered to be equal zero
                //(The corner pixels does not equal 0 you treat the corner pixel as a 3x3 matrix same as the middle pixels and get the Gx and Gy values for them)
                gxRed = -2 * imgCopy[h][w - 1].rgbtRed + -1 * imgCopy[h + 1][w - 1].rgbtRed;
                gxGreen = -2 * imgCopy[h][w - 1].rgbtGreen + -1 * imgCopy[h + 1][w - 1].rgbtGreen;
                gxBlue = -2 * imgCopy[h][w - 1].rgbtBlue + -1 * imgCopy[h + 1][w - 1].rgbtBlue;

                gyRed = 2 * imgCopy[h + 1][w].rgbtRed + imgCopy[h + 1][w - 1].rgbtRed;
                gyGreen = 2 * imgCopy[h + 1][w].rgbtGreen + imgCopy[h + 1][w - 1].rgbtGreen;
                gyBlue = 2 * imgCopy[h + 1][w].rgbtBlue + imgCopy[h + 1][w - 1].rgbtBlue;

                gRed = sqrt((gxRed * gxRed) + (gyRed * gyRed));
                gGreen = sqrt((gxGreen * gxGreen) + (gyGreen * gyGreen));
                gBlue = sqrt((gxBlue * gxBlue) + (gyBlue * gyBlue));

                gRed = round(gRed);
                gGreen = round(gGreen);
                gBlue = round(gBlue);

                if(gRed > 255)
                {
                    gRed = 255;
                }
                if(gGreen > 255)
                {
                    gGreen = 255;
                }
                if(gBlue > 255)
                {
                    gBlue = 255;
                }
                image[h][w].rgbtRed = gRed;
                image[h][w].rgbtGreen = gGreen;
                image[h][w].rgbtBlue = gBlue;
            }

            // Bottom left corner
            else if (h == height - 1 && w == 0 )
            {
                // For Gx and Gy values imagine a 3x3 matrix at the corner and any pixel outside the corner is considered to be equal zero
                //(The corner pixels does not equal 0 you treat the corner pixel as a 3x3 matrix same as the middle pixels and get the Gx and Gy values for them)
                gxRed = 1 * imgCopy[h - 1][w + 1].rgbtRed + 2 * imgCopy[h][w + 1].rgbtRed;
                gxGreen = 1 * imgCopy[h - 1][w + 1].rgbtGreen + 2 * imgCopy[h][w + 1].rgbtGreen;
                gxBlue = 1 * imgCopy[h - 1][w + 1].rgbtBlue + 2 * imgCopy[h][w + 1].rgbtBlue;

                gyRed = -1 * imgCopy[h - 1][w + 1].rgbtRed + -2 * imgCopy[h - 1][w].rgbtRed;
                gyGreen = -1 * imgCopy[h - 1][w + 1].rgbtGreen + -2 * imgCopy[h - 1][w].rgbtGreen;
                gyBlue = -1 * imgCopy[h - 1][w + 1].rgbtBlue + -2 * imgCopy[h - 1][w].rgbtBlue;

                gRed = sqrt((gxRed * gxRed) + (gyRed * gyRed));
                gGreen = sqrt((gxGreen * gxGreen) + (gyGreen * gyGreen));
                gBlue = sqrt((gxBlue * gxBlue) + (gyBlue * gyBlue));

                gRed = round(gRed);
                gGreen = round(gGreen);
                gBlue = round(gBlue);

                if(gRed > 255)
                {
                    gRed = 255;
                }
                if(gGreen > 255)
                {
                    gGreen = 255;
                }
                if(gBlue > 255)
                {
                    gBlue = 255;
                }
                image[h][w].rgbtRed = gRed;
                image[h][w].rgbtGreen = gGreen;
                image[h][w].rgbtBlue = gBlue;
            }

            // Bottom-right corner
            else if ( h == height - 1 && w == width - 1 )
            {
                // For Gx and Gy values imagine a 3x3 matrix at the corner and any pixel outside the corner is considered to be equal zero
                //(The corner pixels does not equal 0 you treat the corner pixel as a 3x3 matrix same as the middle pixels and get the Gx and Gy values for them)
                gxRed = -1 * imgCopy[h - 1][w - 1].rgbtRed + -2 * imgCopy[h][w - 1].rgbtRed;
                gxGreen = -1 * imgCopy[h - 1][w - 1].rgbtGreen + -2 * imgCopy[h][w - 1].rgbtGreen;
                gxBlue = -1 * imgCopy[h - 1][w - 1].rgbtBlue + -2 * imgCopy[h][w - 1].rgbtBlue;

                gyRed = -1 * imgCopy[h - 1][w - 1].rgbtRed + -2 * imgCopy[h - 1][w].rgbtRed;
                gyGreen = -1 * imgCopy[h - 1][w - 1].rgbtGreen + -2 * imgCopy[h - 1][w].rgbtGreen;
                gyBlue = -1 * imgCopy[h - 1][w - 1].rgbtBlue + -2 * imgCopy[h - 1][w].rgbtBlue;

                gRed = sqrt((gxRed * gxRed) + (gyRed * gyRed));
                gGreen = sqrt((gxGreen * gxGreen) + (gyGreen * gyGreen));
                gBlue = sqrt((gxBlue * gxBlue) + (gyBlue * gyBlue));

                gRed = round(gRed);
                gGreen = round(gGreen);
                gBlue = round(gBlue);

                if(gRed > 255)
                {
                    gRed = 255;
                }
                if(gGreen > 255)
                {
                    gGreen = 255;
                }
                if(gBlue > 255)
                {
                    gBlue = 255;
                }
                image[h][w].rgbtRed = gRed;
                image[h][w].rgbtGreen = gGreen;
                image[h][w].rgbtBlue = gBlue;
            }

            // Top border pixels
            else if ( h == 0 && (w > 0 && w < width - 1) )
            {
                // For Gx and Gy values imagine a 3x3 matrix at the border and any pixel outside the border is considered to be equal zero
                //(The border pixels does not equal 0 you treat the border pixel as a 3x3 matrix same as the middle pixels and get the Gx and Gy values for them)
                gxRed = -1 * imgCopy[h][w - 1].rgbtRed + 1 * imgCopy[h][w + 1].rgbtRed;
                gxGreen = -1 * imgCopy[h][w - 1].rgbtGreen + 1 * imgCopy[h][w + 1].rgbtGreen;
                gxBlue = -1 * imgCopy[h][w - 1].rgbtBlue + 1 * imgCopy[h][w + 1].rgbtBlue;

                gyRed = 1 * imgCopy[h][w - 1].rgbtRed + 2 * imgCopy[h][w].rgbtRed + 1 * imgCopy[h][w + 1].rgbtRed;
                gyGreen = 1 * imgCopy[h][w - 1].rgbtGreen + 2 * imgCopy[h][w].rgbtGreen + 1 * imgCopy[h][w + 1].rgbtGreen;
                gyBlue = 1 * imgCopy[h][w - 1].rgbtBlue + 2 * imgCopy[h][w].rgbtBlue + 1 * imgCopy[h][w + 1].rgbtBlue;

                gRed = sqrt((gxRed * gxRed) + (gyRed * gyRed));
                gGreen = sqrt((gxGreen * gxGreen) + (gyGreen * gyGreen));
                gBlue = sqrt((gxBlue * gxBlue) + (gyBlue * gyBlue));

                gRed = round(gRed);
                gGreen = round(gGreen);
                gBlue = round(gBlue);

                if(gRed > 255)
                {
                    gRed = 255;
                }
                if(gGreen > 255)
                {
                    gGreen = 255;
                }
                if(gBlue > 255)
                {
                    gBlue = 255;
                }
                image[h][w].rgbtRed = gRed;
                image[h][w].rgbtGreen = gGreen;
                image[h][w].rgbtBlue = gBlue;
            }

            // Bottom border pixels
            else if ( h == height - 1 && (w > 0 && w < width - 1) )
            {
                // For Gx and Gy values imagine a 3x3 matrix at the border and any pixel outside the border is considered to be equal zero
                //(The border pixels does not equal 0 you treat the border pixel as a 3x3 matrix same as the middle pixels and get the Gx and Gy values for them)
                gxRed = -1 * imgCopy[h][w - 1].rgbtRed + 1 * imgCopy[h][w + 1].rgbtRed;
                gxGreen = -1 * imgCopy[h][w - 1].rgbtGreen + 1 * imgCopy[h][w + 1].rgbtGreen;
                gxBlue = -1 * imgCopy[h][w - 1].rgbtBlue + 1 * imgCopy[h][w + 1].rgbtBlue;

                gyRed = -1 * imgCopy[h][w - 1].rgbtRed + -2 * imgCopy[h][w].rgbtRed + -1 * imgCopy[h][w + 1].rgbtRed;
                gyGreen = -1 * imgCopy[h][w - 1].rgbtGreen + -2 * imgCopy[h][w].rgbtGreen + -1 * imgCopy[h][w + 1].rgbtGreen;
                gyBlue = -1 * imgCopy[h][w - 1].rgbtBlue + -2 * imgCopy[h][w].rgbtBlue + -1 * imgCopy[h][w + 1].rgbtBlue;

                gRed = sqrt((gxRed * gxRed) + (gyRed * gyRed));
                gGreen = sqrt((gxGreen * gxGreen) + (gyGreen * gyGreen));
                gBlue = sqrt((gxBlue * gxBlue) + (gyBlue * gyBlue));

                gRed = round(gRed);
                gGreen = round(gGreen);
                gBlue = round(gBlue);

                if(gRed > 255)
                {
                    gRed = 255;
                }
                if(gGreen > 255)
                {
                    gGreen = 255;
                }
                if(gBlue > 255)
                {
                    gBlue = 255;
                }
                image[h][w].rgbtRed = gRed;
                image[h][w].rgbtGreen = gGreen;
                image[h][w].rgbtBlue = gBlue;
            }

            // Left-side border pixels
            else if ( (h > 0 && h < height - 1) && w == 0 )
            {
                // For Gx and Gy values imagine a 3x3 matrix at the border and any pixel outside the border is considered to be equal zero
                //(The border pixels does not equal 0 you treat the border pixel as a 3x3 matrix same as the middle pixels and get the Gx and Gy values for them)
                gxRed = 1 * imgCopy[h - 1][w].rgbtRed + 2 * imgCopy[h][w].rgbtRed + 1 * imgCopy[h + 1][w].rgbtRed;
                gxGreen = 1 * imgCopy[h - 1][w].rgbtGreen + 2 * imgCopy[h][w].rgbtGreen + 1 * imgCopy[h + 1][w].rgbtGreen;
                gxBlue = 1 * imgCopy[h - 1][w].rgbtBlue + 2 * imgCopy[h][w].rgbtBlue + 1 * imgCopy[h + 1][w].rgbtBlue;

                gyRed = -1 * imgCopy[h - 1][w].rgbtRed + 1 * imgCopy[h + 1][w].rgbtRed;
                gyGreen = -1 * imgCopy[h - 1][w].rgbtGreen + 1 * imgCopy[h + 1][w].rgbtGreen;
                gyBlue = -1 * imgCopy[h - 1][w].rgbtBlue + 1 * imgCopy[h + 1][w].rgbtBlue;

                gRed = sqrt((gxRed * gxRed) + (gyRed * gyRed));
                gGreen = sqrt((gxGreen * gxGreen) + (gyGreen * gyGreen));
                gBlue = sqrt((gxBlue * gxBlue) + (gyBlue * gyBlue));

                gRed = round(gRed);
                gGreen = round(gGreen);
                gBlue = round(gBlue);

                if(gRed > 255)
                {
                    gRed = 255;
                }
                if(gGreen > 255)
                {
                    gGreen = 255;
                }
                if(gBlue > 255)
                {
                    gBlue = 255;
                }
                image[h][w].rgbtRed = gRed;
                image[h][w].rgbtGreen = gGreen;
                image[h][w].rgbtBlue = gBlue;
            }

            // Right-side border pixels
            else if ( (h > 0 && h < height - 1) && w == width - 1 )
            {
                // For Gx and Gy values imagine a 3x3 matrix at the border and any pixel outside the border is considered to be equal zero
                //(The border pixels does not equal 0 you treat the border pixel as a 3x3 matrix same as the middle pixels and get the Gx and Gy values for them)
                gxRed = -1 * imgCopy[h - 1][w].rgbtRed + -2 * imgCopy[h][w].rgbtRed + -1 * imgCopy[h + 1][w].rgbtRed;
                gxGreen = -1 * imgCopy[h - 1][w].rgbtGreen + -2 * imgCopy[h][w].rgbtGreen + -1 * imgCopy[h + 1][w].rgbtGreen;
                gxBlue = -1 * imgCopy[h - 1][w].rgbtBlue + -2 * imgCopy[h][w].rgbtBlue + -1 * imgCopy[h + 1][w].rgbtBlue;

                gyRed = -1 * imgCopy[h - 1][w].rgbtRed + 1 * imgCopy[h + 1][w].rgbtRed;
                gyGreen = -1 * imgCopy[h - 1][w].rgbtGreen + 1 * imgCopy[h + 1][w].rgbtGreen;
                gyBlue = -1 * imgCopy[h - 1][w].rgbtBlue + 1 * imgCopy[h + 1][w].rgbtBlue;

                gRed = sqrt((gxRed * gxRed) + (gyRed * gyRed));
                gGreen = sqrt((gxGreen * gxGreen) + (gyGreen * gyGreen));
                gBlue = sqrt((gxBlue * gxBlue) + (gyBlue * gyBlue));

                gRed = round(gRed);
                gGreen = round(gGreen);
                gBlue = round(gBlue);

                if(gRed > 255)
                {
                    gRed = 255;
                }
                if(gGreen > 255)
                {
                    gGreen = 255;
                }
                if(gBlue > 255)
                {
                    gBlue = 255;
                }
                image[h][w].rgbtRed = gRed;
                image[h][w].rgbtGreen = gGreen;
                image[h][w].rgbtBlue = gBlue;
            }

            // Middle 3x3 pixels
            else
            {
            gxRed =   (float)((-1 * imgCopy[h - 1][w - 1].rgbtRed) + (0 * imgCopy[h - 1][w].rgbtRed) + (1 * imgCopy[h - 1][w + 1].rgbtRed) +
                              (-2 * imgCopy[h][w - 1].rgbtRed)     + (0 * imgCopy[h][w].rgbtRed)     + (2 * imgCopy[h][w + 1].rgbtRed) +
                              (-1 * imgCopy[h + 1][w - 1].rgbtRed) + (0 * imgCopy[h + 1][w].rgbtRed) + (1 * imgCopy[h + 1][w + 1].rgbtRed));

            gxGreen = (float)((-1 * imgCopy[h - 1][w - 1].rgbtGreen) + (0 * imgCopy[h - 1][w].rgbtGreen) + (1 * imgCopy[h - 1][w + 1].rgbtGreen) +
                              (-2 * imgCopy[h][w - 1].rgbtGreen)     + (0 * imgCopy[h][w].rgbtGreen)     + (2 * imgCopy[h][w + 1].rgbtGreen) +
                              (-1 * imgCopy[h + 1][w - 1].rgbtGreen) + (0 * imgCopy[h + 1][w].rgbtGreen) + (1 * imgCopy[h + 1][w + 1].rgbtGreen));

            gxBlue =  (float)((-1 * imgCopy[h - 1][w - 1].rgbtBlue) + (0 * imgCopy[h - 1][w].rgbtBlue) + (1 * imgCopy[h - 1][w + 1].rgbtBlue) +
                              (-2 * imgCopy[h][w - 1].rgbtBlue)     + (0 * imgCopy[h][w].rgbtBlue)     + (2 * imgCopy[h][w + 1].rgbtBlue) +
                              (-1 * imgCopy[h + 1][w - 1].rgbtBlue) + (0 * imgCopy[h + 1][w].rgbtBlue) + (1 * imgCopy[h + 1][w + 1].rgbtBlue));

            gyRed =   (float)((-1 * imgCopy[h - 1][w - 1].rgbtRed) + (-2 * imgCopy[h - 1][w].rgbtRed) + (-1 * imgCopy[h - 1][w + 1].rgbtRed) +
                              (0 * imgCopy[h][w - 1].rgbtRed)      + (0 * imgCopy[h][w].rgbtRed)      + (0 * imgCopy[h][w + 1].rgbtRed) +
                              (1 * imgCopy[h + 1][w - 1].rgbtRed)  + (2 * imgCopy[h + 1][w].rgbtRed)  + (1 * imgCopy[h + 1][w + 1].rgbtRed));

            gyGreen = (float)((-1 * imgCopy[h - 1][w - 1].rgbtGreen) + (-2 * imgCopy[h - 1][w].rgbtGreen)  + (-1 * imgCopy[h - 1][w + 1].rgbtGreen) +
                              (0 * imgCopy[h][w - 1].rgbtGreen)      + (0 * imgCopy[h][w].rgbtGreen)       + (0 * imgCopy[h][w + 1].rgbtGreen) +
                              (1 * imgCopy[h + 1][w - 1].rgbtGreen)  + (2 * imgCopy[h + 1][w].rgbtGreen)   + (1 * imgCopy[h + 1][w + 1].rgbtGreen));

            gyBlue =  (float)((-1 * imgCopy[h - 1][w - 1].rgbtBlue) + (-2 * imgCopy[h - 1][w].rgbtBlue)  + (-1 * imgCopy[h - 1][w + 1].rgbtBlue) +
                              (0 * imgCopy[h][w - 1].rgbtBlue)      + (0 * imgCopy[h][w].rgbtBlue)       + (0 * imgCopy[h][w + 1].rgbtBlue) +
                              (1 * imgCopy[h + 1][w - 1].rgbtBlue)  + (2 * imgCopy[h + 1][w].rgbtBlue)   + (1 * imgCopy[h + 1][w + 1].rgbtBlue));

            gRed = sqrt((gxRed * gxRed) + (gyRed * gyRed));
            gGreen = sqrt((gxGreen * gxGreen) + (gyGreen * gyGreen));
            gBlue = sqrt((gxBlue * gxBlue) + (gyBlue * gyBlue));

            gRed = round(gRed);
            gGreen = round(gGreen);
            gBlue = round(gBlue);

            if(gRed > 255)
            {
                gRed = 255;
            }
            if(gGreen > 255)
            {
                gGreen = 255;
            }
            if(gBlue > 255)
            {
                gBlue = 255;
            }
            image[h][w].rgbtRed = gRed;
            image[h][w].rgbtGreen = gGreen;
            image[h][w].rgbtBlue = gBlue;
            }

        }
    }
    return;
}

这些是我运行(check50)检查我的代码时收到的错误消息:

:)边缘正确过滤了中间像素(此部分正确

:(边缘正确过滤边缘上的像素 应该是“ 213228255 \ n”,而不是“ 57103174 \ n”

:)边缘正确过滤了角落中的像素。 (这部分是正确的

:(边缘正确过滤3x3图像 应该是“ 76117255 \ n21 ...”,而不是“ 76117255 \ n57 ...”

:(边缘正确过滤4x4图像 应该是“ 76117255 \ n21 ...”,而不是“ 76117255 \ n57 ...”

1 个答案:

答案 0 :(得分:0)

更新:我在代码中添加了单独的功能,以消除重复的代码并使代码更短。

好的,我解决了。长时间盯着它。我注意到边界像素应包含Gx矩阵的2行或2列(而不只是Gx,Gy矩阵的1行或1列)。在那个3x3矩阵中,您将得到一个假想的3像素,该像素等于零,因为它在该图像的边界之外。

无论如何,这里还是我完整的正确代码,以防万一有人也为此代码而苦恼。

还有其他人解决了这个问题,但是他们的代码对我来说还不够清楚。遗憾的是,由于我的帐户是新帐户,我没有足够的声誉积分来评论该帖子并向他们提出一些问题。

void findG();

float gxRed, gxGreen, gxBlue;
float gyRed, gyGreen, gyBlue;
float gRed, gGreen, gBlue;

// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE imgCopy[height][width];

    // Making a copy of image
    for (int h = 0; h < height; h++)
    {
        for (int w = 0; w < width; w++)
        {
            imgCopy[h][w].rgbtRed = image[h][w].rgbtRed;
            imgCopy[h][w].rgbtGreen = image[h][w].rgbtGreen;
            imgCopy[h][w].rgbtBlue = image[h][w].rgbtBlue;
        }
    }

    for (int h = 0; h < height; h++)
    {
        for (int w = 0; w < width; w++)
        {

            // Top left Corner
            if (h == 0 && w == 0)
            {
                // For Gx and Gy values imagine a 3x3 matrix at the corner and any pixel outside the corner is considered to be equal zero
                //(The corner pixels does not equal 0 you treat the corner pixel as a 3x3 matrix same as the middle pixels and get the Gx and Gy values for them)
                gxRed = 2 * imgCopy[h][w + 1].rgbtRed + imgCopy[h + 1][w + 1].rgbtRed;
                gxGreen = 2 * imgCopy[h][w + 1].rgbtGreen + imgCopy[h + 1][w + 1].rgbtGreen;
                gxBlue = 2 * imgCopy[h][w + 1].rgbtBlue + imgCopy[h + 1][w + 1].rgbtBlue;

                gyRed = 2 * imgCopy[h + 1][w].rgbtRed + imgCopy[h + 1][w + 1].rgbtRed ;
                gyGreen = 2 * imgCopy[h + 1][w].rgbtGreen + imgCopy[h + 1][w + 1].rgbtGreen;
                gyBlue = 2 * imgCopy[h + 1][w].rgbtBlue + imgCopy[h + 1][w + 1].rgbtBlue;

                findG(h,w);
                
                image[h][w].rgbtRed = gRed;
                image[h][w].rgbtGreen = gGreen;
                image[h][w].rgbtBlue = gBlue;
                }

            // Top-right corner
            else if ( h == 0 && w == width - 1 )
            {
                // For Gx and Gy values imagine a 3x3 matrix at the corner and any pixel outside the corner is considered to be equal zero
                //(The corner pixels does not equal 0 you treat the corner pixel as a 3x3 matrix same as the middle pixels and get the Gx and Gy values for them)
                gxRed = -2 * imgCopy[h][w - 1].rgbtRed + -1 * imgCopy[h + 1][w - 1].rgbtRed;
                gxGreen = -2 * imgCopy[h][w - 1].rgbtGreen + -1 * imgCopy[h + 1][w - 1].rgbtGreen;
                gxBlue = -2 * imgCopy[h][w - 1].rgbtBlue + -1 * imgCopy[h + 1][w - 1].rgbtBlue;

                gyRed = 2 * imgCopy[h + 1][w].rgbtRed + imgCopy[h + 1][w - 1].rgbtRed;
                gyGreen = 2 * imgCopy[h + 1][w].rgbtGreen + imgCopy[h + 1][w - 1].rgbtGreen;
                gyBlue = 2 * imgCopy[h + 1][w].rgbtBlue + imgCopy[h + 1][w - 1].rgbtBlue;

                findG(h,w);
                
                image[h][w].rgbtRed = gRed;
                image[h][w].rgbtGreen = gGreen;
                image[h][w].rgbtBlue = gBlue;
            }

            // Bottom left corner
            else if (h == height - 1 && w == 0 )
            {
                // For Gx and Gy values imagine a 3x3 matrix at the corner and any pixel outside the corner is considered to be equal zero
                //(The corner pixels does not equal 0 you treat the corner pixel as a 3x3 matrix same as the middle pixels and get the Gx and Gy values for them)
                gxRed = 1 * imgCopy[h - 1][w + 1].rgbtRed + 2 * imgCopy[h][w + 1].rgbtRed;
                gxGreen = 1 * imgCopy[h - 1][w + 1].rgbtGreen + 2 * imgCopy[h][w + 1].rgbtGreen;
                gxBlue = 1 * imgCopy[h - 1][w + 1].rgbtBlue + 2 * imgCopy[h][w + 1].rgbtBlue;

                gyRed = -1 * imgCopy[h - 1][w + 1].rgbtRed + -2 * imgCopy[h - 1][w].rgbtRed;
                gyGreen = -1 * imgCopy[h - 1][w + 1].rgbtGreen + -2 * imgCopy[h - 1][w].rgbtGreen;
                gyBlue = -1 * imgCopy[h - 1][w + 1].rgbtBlue + -2 * imgCopy[h - 1][w].rgbtBlue;

                findG(h,w);
                
                image[h][w].rgbtRed = gRed;
                image[h][w].rgbtGreen = gGreen;
                image[h][w].rgbtBlue = gBlue;
            }

            // Bottom-right corner
            else if ( h == height - 1 && w == width - 1 )
            {
                // For Gx and Gy values imagine a 3x3 matrix at the corner and any pixel outside the corner is considered to be equal zero
                //(The corner pixels does not equal 0 you treat the corner pixel as a 3x3 matrix same as the middle pixels and get the Gx and Gy values for them)
                gxRed = -1 * imgCopy[h - 1][w - 1].rgbtRed + -2 * imgCopy[h][w - 1].rgbtRed;
                gxGreen = -1 * imgCopy[h - 1][w - 1].rgbtGreen + -2 * imgCopy[h][w - 1].rgbtGreen;
                gxBlue = -1 * imgCopy[h - 1][w - 1].rgbtBlue + -2 * imgCopy[h][w - 1].rgbtBlue;

                gyRed = -1 * imgCopy[h - 1][w - 1].rgbtRed + -2 * imgCopy[h - 1][w].rgbtRed;
                gyGreen = -1 * imgCopy[h - 1][w - 1].rgbtGreen + -2 * imgCopy[h - 1][w].rgbtGreen;
                gyBlue = -1 * imgCopy[h - 1][w - 1].rgbtBlue + -2 * imgCopy[h - 1][w].rgbtBlue;

                findG(h,w);
                
                image[h][w].rgbtRed = gRed;
                image[h][w].rgbtGreen = gGreen;
                image[h][w].rgbtBlue = gBlue;
            }

            // Top border pixels
            else if ( h == 0 && (w > 0 && w < width - 1) )
            {
                // For Gx and Gy values imagine a 3x3 matrix at the border and any pixel outside the border is considered to be equal zero.
                //(The border pixels does not equal 0 you treat the border pixel as a 3x3 matrix same as the middle pixels and get the Gx and Gy values for them)
                gxRed = (-2 * imgCopy[h][w - 1].rgbtRed) + (2 * imgCopy[h][w + 1].rgbtRed) + (-1 * imgCopy[h + 1][w - 1].rgbtRed) + (1 * imgCopy[h + 1][w + 1].rgbtRed) ;
                gxGreen = (-2 * imgCopy[h][w - 1].rgbtGreen) + (2 * imgCopy[h][w + 1].rgbtGreen) + (-1 * imgCopy[h + 1][w - 1].rgbtGreen) + (1 * imgCopy[h + 1][w + 1].rgbtGreen) ;
                gxBlue = (-2 * imgCopy[h][w - 1].rgbtBlue) + (2 * imgCopy[h][w + 1].rgbtBlue) + (-1 * imgCopy[h + 1][w - 1].rgbtBlue) + (1 * imgCopy[h + 1][w + 1].rgbtBlue) ;

                gyRed = (1 * imgCopy[h + 1][w - 1].rgbtRed) + (2 * imgCopy[h + 1][w].rgbtRed) + (1 * imgCopy[h + 1][w + 1].rgbtRed);
                gyGreen = (1 * imgCopy[h + 1][w - 1].rgbtGreen) + (2 * imgCopy[h + 1][w].rgbtGreen) + (1 * imgCopy[h + 1][w + 1].rgbtGreen);
                gyBlue = (1 * imgCopy[h + 1][w - 1].rgbtBlue) + (2 * imgCopy[h + 1][w].rgbtBlue) + (1 * imgCopy[h + 1][w + 1].rgbtBlue);

                findG(h,w);
                
                image[h][w].rgbtRed = gRed;
                image[h][w].rgbtGreen = gGreen;
                image[h][w].rgbtBlue = gBlue;
            }

            // Bottom border pixels
            else if ( h == height - 1 && (w > 0 && w < width - 1) )
            {
                // For Gx and Gy values imagine a 3x3 matrix at the border and any pixel outside the border is considered to be equal zero
                //(The border pixels does not equal 0 you treat the border pixel as a 3x3 matrix same as the middle pixels and get the Gx and Gy values for them)
                gxRed = (-2 * imgCopy[h][w - 1].rgbtRed) + (2 * imgCopy[h][w + 1].rgbtRed) + (-1 * imgCopy[h - 1][w - 1].rgbtRed) + (1 * imgCopy[h - 1][w + 1].rgbtRed) ;
                gxGreen = (-2 * imgCopy[h][w - 1].rgbtGreen) + (2 * imgCopy[h][w + 1].rgbtGreen) + (-1 * imgCopy[h - 1][w - 1].rgbtGreen) + (1 * imgCopy[h - 1][w + 1].rgbtGreen) ;
                gxBlue = (-2 * imgCopy[h][w - 1].rgbtBlue) + (2 * imgCopy[h][w + 1].rgbtBlue) + (-1 * imgCopy[h - 1][w - 1].rgbtBlue) + (1 * imgCopy[h - 1][w + 1].rgbtBlue) ;

                gyRed = (-1 * imgCopy[h - 1][w - 1].rgbtRed) + (-2 * imgCopy[h - 1][w].rgbtRed) + (-1 * imgCopy[h - 1][w + 1].rgbtRed);
                gyGreen = (-1 * imgCopy[h - 1][w - 1].rgbtGreen) + (-2 * imgCopy[h - 1][w].rgbtGreen) + (-1 * imgCopy[h - 1][w + 1].rgbtGreen);
                gyBlue = (-1 * imgCopy[h - 1][w - 1].rgbtBlue) + (-2 * imgCopy[h - 1][w].rgbtBlue) + (-1 * imgCopy[h - 1][w + 1].rgbtBlue);

                findG(h,w);
                
                image[h][w].rgbtRed = gRed;
                image[h][w].rgbtGreen = gGreen;
                image[h][w].rgbtBlue = gBlue;
            }

            // Left-side border pixels
            else if ( (h > 0 && h < height - 1) && w == 0 )
            {
                // For Gx and Gy values imagine a 3x3 matrix at the border and any pixel outside the border is considered to be equal zero
                //(The border pixels does not equal 0 you treat the border pixel as a 3x3 matrix same as the middle pixels and get the Gx and Gy values for them)
                gxRed = (1 * imgCopy[h - 1][w + 1].rgbtRed) + (2 * imgCopy[h][w + 1].rgbtRed) + (1 * imgCopy[h + 1][w + 1].rgbtRed);
                gxGreen = (1 * imgCopy[h - 1][w + 1].rgbtGreen) + (2 * imgCopy[h][w + 1].rgbtGreen) + (1 * imgCopy[h + 1][w + 1].rgbtGreen);
                gxBlue = (1 * imgCopy[h - 1][w + 1].rgbtBlue) + (2 * imgCopy[h][w + 1].rgbtBlue) + (1 * imgCopy[h + 1][w + 1].rgbtBlue);

                gyRed = (-2 * imgCopy[h - 1][w].rgbtRed) + (2 * imgCopy[h + 1][w].rgbtRed) + (-1 * imgCopy[h - 1][w + 1].rgbtRed) + (1 * imgCopy[h + 1][w + 1].rgbtRed);
                gyGreen = (-2 * imgCopy[h - 1][w].rgbtGreen) + (2 * imgCopy[h + 1][w].rgbtGreen) + (-1 * imgCopy[h - 1][w + 1].rgbtGreen) + (1 * imgCopy[h + 1][w + 1].rgbtGreen);
                gyBlue = (-2 * imgCopy[h - 1][w].rgbtBlue) + (2 * imgCopy[h + 1][w].rgbtBlue) + (-1 * imgCopy[h - 1][w + 1].rgbtBlue) + (1 * imgCopy[h + 1][w + 1].rgbtBlue);

                findG(h,w);
                
                image[h][w].rgbtRed = gRed;
                image[h][w].rgbtGreen = gGreen;
                image[h][w].rgbtBlue = gBlue;
            }

            // Right-side border pixels
            else if ( (h > 0 && h < height - 1) && w == width - 1 )
            {
               // For Gx and Gy values imagine a 3x3 matrix at the border and any pixel outside the border is considered to be equal zero
                //(The border pixels does not equal 0 you treat the border pixel as a 3x3 matrix same as the middle pixels and get the Gx and Gy values for them)
                gxRed = (-1 * imgCopy[h - 1][w - 1].rgbtRed) + (-2 * imgCopy[h][w - 1].rgbtRed) + (-1 * imgCopy[h + 1][w - 1].rgbtRed);
                gxGreen = (-1 * imgCopy[h - 1][w - 1].rgbtGreen) + (-2 * imgCopy[h][w - 1].rgbtGreen) + (-1 * imgCopy[h + 1][w - 1].rgbtGreen);
                gxBlue = (-1 * imgCopy[h - 1][w - 1].rgbtBlue) + (-2 * imgCopy[h][w - 1].rgbtBlue) + (-1 * imgCopy[h + 1][w - 1].rgbtBlue);

                gyRed = (-1 * imgCopy[h - 1][w - 1].rgbtRed) + (-2 * imgCopy[h - 1][w].rgbtRed) + (1 * imgCopy[h + 1][w - 1].rgbtRed) + (2 * imgCopy[h + 1][w].rgbtRed);
                gyGreen = (-1 * imgCopy[h - 1][w - 1].rgbtGreen) + (-2 * imgCopy[h - 1][w].rgbtGreen) + (1 * imgCopy[h + 1][w - 1].rgbtGreen) + (2 * imgCopy[h + 1][w].rgbtGreen);
                gyBlue = (-1 * imgCopy[h - 1][w - 1].rgbtBlue) + (-2 * imgCopy[h - 1][w].rgbtBlue) + (1 * imgCopy[h + 1][w - 1].rgbtBlue) + (2 * imgCopy[h + 1][w].rgbtBlue);

                findG(h,w);
                
                image[h][w].rgbtRed = gRed;
                image[h][w].rgbtGreen = gGreen;
                image[h][w].rgbtBlue = gBlue;
            }

            // Middle 3x3 pixels
            else
            {
                gxRed =   (float)((-1 * imgCopy[h - 1][w - 1].rgbtRed) + (1 * imgCopy[h - 1][w + 1].rgbtRed) +
                                  (-2 * imgCopy[h][w - 1].rgbtRed)     + (2 * imgCopy[h][w + 1].rgbtRed) +
                                  (-1 * imgCopy[h + 1][w - 1].rgbtRed) + (1 * imgCopy[h + 1][w + 1].rgbtRed));

                gxGreen = (float)((-1 * imgCopy[h - 1][w - 1].rgbtGreen) + (1 * imgCopy[h - 1][w + 1].rgbtGreen) +
                                  (-2 * imgCopy[h][w - 1].rgbtGreen)     + (2 * imgCopy[h][w + 1].rgbtGreen) +
                                  (-1 * imgCopy[h + 1][w - 1].rgbtGreen) + (1 * imgCopy[h + 1][w + 1].rgbtGreen));

                gxBlue =  (float)((-1 * imgCopy[h - 1][w - 1].rgbtBlue)  + (1 * imgCopy[h - 1][w + 1].rgbtBlue) +
                                  (-2 * imgCopy[h][w - 1].rgbtBlue)      + (2 * imgCopy[h][w + 1].rgbtBlue) +
                                  (-1 * imgCopy[h + 1][w - 1].rgbtBlue)  + (1 * imgCopy[h + 1][w + 1].rgbtBlue));

                gyRed =   (float)((-1 * imgCopy[h - 1][w - 1].rgbtRed) + (-2 * imgCopy[h - 1][w].rgbtRed) + (-1 * imgCopy[h - 1][w + 1].rgbtRed) +
                                  (1 * imgCopy[h + 1][w - 1].rgbtRed)  + (2 * imgCopy[h + 1][w].rgbtRed)  + (1 * imgCopy[h + 1][w + 1].rgbtRed));

                gyGreen = (float)((-1 * imgCopy[h - 1][w - 1].rgbtGreen) + (-2 * imgCopy[h - 1][w].rgbtGreen)  + (-1 * imgCopy[h - 1][w + 1].rgbtGreen) +
                                  (1 * imgCopy[h + 1][w - 1].rgbtGreen)  + (2 * imgCopy[h + 1][w].rgbtGreen)   + (1 * imgCopy[h + 1][w + 1].rgbtGreen));

                gyBlue =  (float)((-1 * imgCopy[h - 1][w - 1].rgbtBlue) + (-2 * imgCopy[h - 1][w].rgbtBlue)  + (-1 * imgCopy[h - 1][w + 1].rgbtBlue) +
                                  (1 * imgCopy[h + 1][w - 1].rgbtBlue)  + (2 * imgCopy[h + 1][w].rgbtBlue)   + (1 * imgCopy[h + 1][w + 1].rgbtBlue));
                                  
                findG(h,w);

                image[h][w].rgbtRed = gRed;
                image[h][w].rgbtGreen = gGreen;
                image[h][w].rgbtBlue = gBlue;
            }

        }
    }
    return;
}

void findG(int h ,int w)
{
                gRed = sqrt((gxRed * gxRed) + (gyRed * gyRed));
                gGreen = sqrt((gxGreen * gxGreen) + (gyGreen * gyGreen));
                gBlue = sqrt((gxBlue * gxBlue) + (gyBlue * gyBlue));

                gRed = round(gRed);
                gGreen = round(gGreen);
                gBlue = round(gBlue);

                if (gRed > 255)
                {
                    gRed = 255;
                }
                if (gGreen > 255)
                {
                    gGreen = 255;
                }
                if (gBlue > 255)
                {
                    gBlue = 255;
                }
return;
}

以下是运行时的结果(检查50):

:)边缘正确过滤了中间像素

:)边缘正确过滤边缘上的像素

:)边缘正确过滤了角落的像素

:)边缘可以正确过滤3x3图像

:)边缘可以正确过滤4x4图像