【问题标题】:CS50 pset4 filter blur function turns the whole image to one colorCS50 pset4滤镜模糊功能将整个图像变成一种颜色
【发布时间】:2021-09-23 14:15:45
【问题描述】:

模糊函数是一个框模糊算法的实现,该算法通过获取每个像素,并且对于每个颜色值,通过平均相邻像素的颜色值来为其赋予一个新值。试图理解这个问题花了我一整天的时间和很多挫败感。我不确定为什么图像不会模糊,而是将整体变为一种颜色。

// 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;
    
    }

【问题讨论】:

  • 使用循环扫描周围的像素是个好主意,但现在你拥有它的方式总是相同的。你想要的是for(int r = i - 1; r &lt;= i + 1; r++)c 相同,除了j 而不是i。您也不需要带有 continue 的 else 子句。后面没有代码可以跳过。
  • r 和 c 只取 -1, 0 和 1 (这是像素​​之前的索引,像素本身和像素之后的索引),如果我写 r = i - 1 ,如果我= 10 然后 10 -1 = 9 这不应该再次发生,因为 r 只需要 -1、0 和 1

标签: c pixel cs50 gaussianblur


【解决方案1】:

我想出了答案。在计算无法移动到相邻行和列的红色、绿色和蓝色时,我没有将第 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;

}

【讨论】:

  • &lt;= 2r 的循环条件中不正确,if(r &gt;= 0 &amp;&amp; r &lt; height &amp;&amp; c &gt;= 0 &amp;&amp; c &lt; width) 不检查最终位置是否超出范围,它只是跳过所有 - 1 值。 for(int r = i - 1; r &lt;= i + 1; r++)for(int c = j - 1; c &lt;= j + 1; c++) 可以解决这两个问题。
  • r 应该是 &lt; 2。抱歉打错了,If (r &gt;= 0 &amp;&amp; r &lt; height &amp;&amp; c &gt;= 0 &amp;&amp; c &lt; width) 应该更正为if( r + i &gt;= 0 &amp;&amp; r + i &lt; height &amp;&amp; c + j &gt;= 0 &amp;&amp; c + j &lt; width) 或者像你提到的for(int r = i - 1; r &lt;= i + 1; r++)for(int c = j - 1; c &lt;= j + 1; c++)。我后来想通了。事实证明你是对的。但两者都可以正常工作。谢谢!
  • 但是我尝试按照你提到的进行编码,我遇到了分段错误。
  • 所以我把if(r &gt;= 0 &amp;&amp; r &lt; height &amp;&amp; c &gt;= 0 &amp;&amp; c &lt; width)改成了if(r+i &gt;= 0 &amp;&amp; r+i &lt; height &amp;&amp; c+j &gt;= 0 &amp;&amp; c+j &lt; width),但是模糊的图像变成了左上角图像的缩小版,剩下的部分用黑色填充
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多