CS50 pset4 滤镜模糊功能将整个图像变成一种颜色

CS50 pset4 filter blur function turns the whole image to one color

模糊函数是框模糊算法的实现,该算法的工作原理是获取每个像素,并通过对相邻像素的颜色值进行平均来为每个颜色值赋予新值。试图理解这个问题花了我一整天的时间和很多挫折。不知道为什么图片不模糊而是整体变成一种颜色。

// Blur image
    void blur(int height, int width, RGBTRIPLE image[height][width])
    {
    
      RGBTRIPLE temp[height][width];
    
    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        {
            temp[i][j] = image[i][j];
        }
    }
    
    
    for(int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            float count = 0;
            float red = 0, green = 0, blue = 0;
    
           // for row-1,row,row+1
           //for col-1,col.col+1
    
            for(int r = -1; r <= 2; r++)
            {
                for (int c = -1; c < 2; c++)
                {
                    if(r >= 0 && r < height && c >= 0 && c < width)
                    {
                        red +=   temp[r][c].rgbtRed;
                        green += temp[r][c].rgbtGreen;
                        blue +=  temp[r][c].rgbtBlue;
                        count++;
                    }
                    else
                    {
                        continue;
                    }
                }
            }
    
             image[i][j].rgbtRed = round(red/count);
             image[i][j].rgbtGreen = round(green/count);
             image[i][j].rgbtBlue = round(blue/count);
    
    
    
        }
    
    }
    
    
     return;
    
    }

我找到了答案。在计算红色、绿色和蓝色时,我没有将第 i 和第 j 个索引添加到行(r)和列(c),我无法移动到相邻的行和列。

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{

  RGBTRIPLE temp[height][width];

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


for(int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        float count = 0;
        float red = 0, green = 0, blue = 0;

       // for row-1,row,row+1
       //for col-1,col.col+1

        for(int r = -1; r <= 2; r++)
        {
            for (int c = -1; c < 2; c++)
            {
                if(r >= 0 && r < height && c >= 0 && c < width)
                {
                    red += temp[i+r][j+c].rgbtRed;
                    green += temp[i+r][j+c].rgbtGreen;
                    blue += temp[i+r][j+c].rgbtBlue;
                    count++;
                }
               
            }
        }

         image[i][j].rgbtRed = round(red/count);
         image[i][j].rgbtGreen = round(green/count);
         image[i][j].rgbtBlue = round(blue/count);



    }

}


 return;

}