CS50 PSET4过滤器边缘检测

时间:2020-08-04 20:19:26

标签: c filter cs50 edge-detection

我对帖子的主要更改做了新的帖子,但是现在的问题是我的图像返回的是与输入的相同图像。我相信newimage变量存在问题,并将其与旧的image变量交换,但我不知道为什么。

void edges(int height, int width, RGBTRIPLE image[height][width])
{
    int gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
    int gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
    RGBTRIPLE newimage[height][width];

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            int redx = 0;
            int greenx = 0;
            int bluex = 0;
            int redy = 0;
            int greeny = 0;
            int bluey = 0;
            for (int k = i - 1; k <= i + 1 && k < height; k++)
            {
                for (int m = j - 1; m <= j + 1 && m < width; m++)
                {
                    if (k != -1 && m != -1)
                    {
                        redx   += image[k][m].rgbtRed   * gx[k-(i-1)][m-(j-1)];
                        greenx += image[k][m].rgbtGreen * gx[k-(i-1)][m-(j-1)];
                        bluex  += image[k][m].rgbtBlue  * gx[k-(i-1)][m-(j-1)];
                        redy   += image[k][m].rgbtRed   * gy[k-(i-1)][m-(j-1)];
                        greeny += image[k][m].rgbtGreen * gy[k-(i-1)][m-(j-1)];
                        bluey  += image[k][m].rgbtBlue  * gy[k-(i-1)][m-(j-1)];
                    }
                }
            }
            int finalred = round(sqrt((redx * redx) + (redy * redy)));
            int finalgreen = round(sqrt((greenx * greenx) + (greeny * greeny)));
            int finalblue = round(sqrt((bluex * bluex) + (bluey * bluey)));
            if (finalred > 255)
            {
                finalred = 255;
            }
            if (finalgreen > 255)
            {
                finalgreen = 255;
            }
            if (finalblue > 255)
            {
                finalblue = 255;
            }
            newimage[i][j].rgbtRed = finalred;
            newimage[i][j].rgbtGreen = finalgreen;
            newimage[i][j].rgbtBlue = finalblue;
        }
    }
    image = newimage;
    return;
}

0 个答案:

没有答案