CS50(Pset4)滤镜:边缘

时间:2020-09-16 12:29:27

标签: c cs50

我目前正在Pset4上工作,我的edge函数似乎无法通过CS50测试。我无法弄清楚我的代码存在的问题。错误是-

:(边缘正确过滤中间像素 预期为“ 210 150 60 \ n”,而不是“ 255 150 60 \ n”

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

:(边缘正确过滤了角落的像素 应该是“ 76117255 \ n”,而不是“ 100117255 \ n”

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

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

void edges(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE imagecopy[height][width];

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        float GxB = 0, GxG = 0, GxR = 0, GyB = 0, GyG = 0, GyR = 0;
        float sobel[3][3] =
        {
            {-1, 0, 1},
            {-2, 0, 2},
            {-1, 0, 1}
        };

        for (int m = -1; m < 2; m++)
        {
            for (int n = -1; n < 2; n++)
            {
                if (i + m > height - 1 || i + m < 0 || j + n > width - 1 || j + n < 0)
                {
                    continue;
                }
                else
                {
                    GxB += image[i + m][j + n].rgbtBlue * sobel[m + 1][n + 1];
                    GxG += image[i + m][j + n].rgbtGreen * sobel[m + 1][n + 1];
                    GxR += image[i + m][j + n].rgbtRed * sobel[m + 1][n + 1];

                    GyB += image[i + m][j + n].rgbtBlue * sobel[n + 1][m + 1];
                    GyG += image[i + m][j + n].rgbtGreen * sobel[n + 1][m + 1];
                    GxR += image[i + m][j + n].rgbtRed * sobel[n + 1][m + 1];
                }
            }
        }

        int B = round(sqrt(pow(GxB, 2) + pow(GyB, 2)));
        int G = round(sqrt(pow(GxG, 2) + pow(GyG, 2)));
        int R = round(sqrt(pow(GxR, 2) + pow(GyR, 2)));

        if (B > 255)
        {
            B = 255;
        }
        if (G > 255)
        {
            G = 255;
        }
        if (R > 255)
        {
            R = 255;
        }

        if (B < 0)
        {
            B = 0;
        }
        if (G < 0)
        {
            G = 0;
        }
        if (R < 0)
        {
            R = 0;
        }

        imagecopy[i][j].rgbtBlue = B;
        imagecopy[i][j].rgbtGreen = G;
        imagecopy[i][j].rgbtRed = R;
    }
}

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        image[i][j] = imagecopy[i][j];
    }
}
return;
}

Screenshot of errors

1 个答案:

答案 0 :(得分:1)

那里有一个错字:

GxR += image[i + m][j + n].rgbtRed * sobel[n + 1][m + 1];

应为GyR