CS50 PSET4 索贝尔滤波器 - 边缘(边界像素)

CS50 PSET4 Sobel filter - Edges (Border Pixels)

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

这里是 link 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) 检查我的代码时收到的错误消息:

:) 边缘正确过滤中间像素(这部分是正确的

:( 边缘正确过滤边缘上的像素 应为“213 228 255\n”,而不是“57 103 174\n”

:) edges 正确地过滤了角落里的像素。 (这部分是正确的)

:( 边缘正确过滤 3x3 图像 应为“76 117 255\n21...”,而不是“76117 255\n57...”

:( 边缘正确过滤 4x4 图像 应为“76 117 255\n21...”,而不是“76117 255\n57...”

更新:我在我的代码中添加了单独的函数以去除重复代码并缩短代码。

好的,我解决了。盯着它看了许久。我注意到边界像素应该包含 Gx 矩阵的 2 行或列(不仅仅是 Gx,Gy 矩阵的一行或一列)。在那个 3x3 矩阵中,你将有一个假想的 3 个像素,它们等于零,因为在该图像的边界之外。

无论如何,这是我的完整正确代码,以防有人也在为这段代码苦苦挣扎。

还有其他人解决了这个问题,但他们的代码对我来说不够清晰。可悲的是,我没有足够的声誉点数在那里发表评论 post 并问他们一些问题,因为我的帐户是新帐户。

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

以下是您 运行 (check50) 时的结果:

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

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

:) 边缘正确过滤角部像素

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

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