CS50(2020)PSET 4滤镜:模糊功能无法正常运行

时间:2020-02-20 21:35:57

标签: c cs50

我已经几次重写此代码,以使其正常工作。当我阅读这段代码时,看起来它在逻辑上应该可以工作,但是当我通过Check50运行它时,我仍然遇到错误,并且除角落之外的所有错误都返回了。我不确定这是引起问题的原因,我知道这些if语句之一不能正确添加所有内容,但是从逻辑上来说它们对我来说都是正确的。代码的这一部分确实给了我一些问题。预先感谢您的帮助。

The 4x4 photo error Check50 gives me

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    //Store copy of original
    RGBTRIPLE Original[height][width];
    for (int i = 0; i < height; i++) //loop through rows
    {
        for (int n = 0; n < width; n++) //loop through columns
        {
            Original[i][n] = image[i][n];
        }
    }
int counter = 0;
float newred = 0;
float newgreen = 0;
float newblue = 0;
    for (int i = 0; i < height; i++) //loop through rows
    {
        for (int n = 0; n < width; n++) //loop through columns
        {
            if (i >= 0 && n >= 0)
            {
                newred = newred + Original[i][n].rgbtRed;
                newgreen = newgreen + Original[i][n].rgbtGreen;
                newblue = newblue + Original[i][n].rgbtBlue;
                counter++;
            }
            if (i >= 0 && n - 1 >= 0)
            {
                newred = newred + Original[i][n-1].rgbtRed;
                newgreen = newgreen + Original[i][n-1].rgbtGreen;
                newblue = newblue + Original[i][n-1].rgbtBlue;
                counter++;
            }
            if (i - 1 >= 0 && n >= 0)
            {
                newred = newred + Original[i-1][n].rgbtRed;
                newgreen = newgreen + Original[i-1][n].rgbtGreen;
                newblue = newblue + Original[i-1][n].rgbtBlue;
                counter++;
            }
            if (i >= 0  && n + 1 < width)
            {
                newred = newred + Original[i][n+1].rgbtRed;
                newgreen = newgreen + Original[i][n+1].rgbtGreen;
                newblue = newblue + Original[i][n+1].rgbtBlue;
                counter++;
            }
            if (i + 1 < height && n >= 0)
            {
                newred = newred + Original[i+1][n].rgbtRed;
                newgreen = newgreen + Original[i+1][n].rgbtGreen;
                newblue = newblue + Original[i+1][n].rgbtBlue;
                counter++;
            }
            if (i + 1 < height && n - 1 >= 0)
            {
                newred = newred + Original[i+1][n-1].rgbtRed;
                newgreen = newgreen + Original[i+1][n-1].rgbtGreen;
                newblue = newblue + Original[i+1][n-1].rgbtBlue;
                counter++;
            }
            if (i - 1 >= 0 && n + 1 < width)
            {
                newred = newred + Original[i-1][n+1].rgbtRed;
                newgreen = newgreen + Original[i-1][n+1].rgbtGreen;
                newblue = newblue + Original[i-1][n+1].rgbtBlue;
                counter++;
            }
            if (i - 1 >= 0 && n - 1 >= 0)
            {
                newred = newred + Original[i-1][n-1].rgbtRed;
                newgreen = newgreen + Original[i-1][n-1].rgbtGreen;
                newblue = newblue + Original[i-1][n-1].rgbtBlue;
                counter++;
            }
            if (i + 1 < height && n + 1 < width)
            {
                newred = newred + Original[i+1][n+1].rgbtRed;
                newgreen = newgreen + Original[i+1][n+1].rgbtGreen;
                newblue = newblue + Original[i+1][n+1].rgbtBlue;
                counter++;
            }
    image[i][n].rgbtRed = round(newred / (counter * 1.0));
    image[i][n].rgbtGreen = round(newgreen / (counter * 1.0));
    image[i][n].rgbtBlue = round(newblue / (counter * 1.0));
        }
    }

    return;
}

0 个答案:

没有答案