卡在CS50滤镜模糊上

时间:2020-11-09 17:14:07

标签: c filter cs50 blur

一切都可以编译,但是在测试时,我得到以下结果,我一直在用这个把头撞在墙上。

:((模糊正确过滤了中间像素
预期为“ 127140149 \ n”,而不是“ 120140150 \ n”
:(模糊可以正确过滤边缘上的像素
应该是“ 80 95 105 \ n”,而不是“ 40 50 60 \ n”
:(模糊可以正确过滤角落中的像素
应该是“ 70 85 95 \ n”,而不是“ 10 20 30 \ n”
:(正确模糊过滤3x3图像
应该是“ 70 85 95 \ n80 9 ...”,而不是“ 10 20 30 \ n40 5 ...”
:(正确模糊过滤4x4图像
应该是“ 70 85 95 \ n80 9 ...”,而不是“ 10 20 30 \ n40 5 ...”

这是我的代码:

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE copyOfImage[height][width];
    
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            float totalPixels = 0.0;
            int red = 0;
            int green = 0;
            int blue = 0;

            //Rows around pixel
            for (int r = -1; r <= 1; r++)
            {
                //Columns around pixel
                for (int c = -1; c <= 1; c++)
                {
                    // If current row + next row are within bounds
                    // If current column + next column are within bounds
                    if(i + r < 0 || i + r > height - 1 || j + c < 0 || j + c > width - 1)
                    {
                        continue;
                    }

                        red += image[i + r][j + c].rgbtRed;
                        green += image[i + r][j + c].rgbtGreen;
                        blue += image[i + r][j + c].rgbtBlue;

                        totalPixels++;
                    }
                }
            copyOfImage[i][j].rgbtRed = round(red / totalPixels);
            copyOfImage[i][j].rgbtGreen = round(green / totalPixels);
            copyOfImage[i][j].rgbtBlue = round(blue / totalPixels);
            
            }
            
        }
        for(int i = 0; i < height; i++){
            for(int j = 0; j < width; j++)
            {
                copyOfImage[i][j].rgbtRed = image[i][j].rgbtRed;
                copyOfImage[i][j].rgbtBlue = image[i][j].rgbtBlue;
                copyOfImage[i][j].rgbtGreen = image[i][j].rgbtGreen;
            }
        }
        return;
    }

0 个答案:

没有答案