繁体   English   中英

CS50 模糊编译但滤镜未应用于图片

[英]CS50 Blur compiles but filter doesn't get applied on the picture

我是编程新手,正在学习 CS50 课程。 在模糊问题上停留了数周。 代码可以编译,但图像不会变得模糊。 这些是错误

我真的不明白错误消息中显示的那些数字及其含义,看起来像素值不是它应该是的值。 我在平台上发现了一个类似的问题,但是代码写的和我的不一样,答案对我没有帮助:(谢谢你的时间!

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
// make copy of file
{
    RGBTRIPLE temp[height][width];

for (int row = 0; row < height; row++)
{
    for (int column = 0; column < width; column++)
    {
        temp[row][column] = image[row][column];
    }
}
    for (int row = 0; row < height; row++)
    {
        for (int column = 0; column < width; column++)
        {
           int totalRed, totalGreen, totalBlue;
            totalRed = totalGreen = totalBlue = 0;
            float counter = 0.00;


            //find neighbour pixels

            for ( int i = -1; i < 2; i++)
            {
                for (int j = -1; j < 2; j++)
                {
                  int currentRow = height + row;
                  int currentColumn = width + column;

                    //exclude invalid pixels

                      if (currentRow < 0 || currentRow > (height - 1) || currentColumn < 0 || currentColumn > (width - 1))
                       {
                        continue;
                       }

                       // adding the total value of the valid neighbouring pixels
                       totalRed += image[currentRow][currentColumn].rgbtRed;
                       totalGreen += image[currentRow][currentColumn].rgbtGreen;
                       totalBlue += image[currentRow][currentColumn].rgbtBlue;

                       counter++;

                }
                //calculate average of sorrounding pixels
          temp[height][width].rgbtRed = round(totalRed / counter);
          temp[height][width].rgbtGreen= round(totalGreen / counter);
          temp[height][width].rgbtBlue= round(totalBlue / counter);

            }
        }
    }
 // copy temp values in original file
    for (int row = 0; row < height; row++)
    {
        for (int column = 0; column < width; column++)
        {
            image[row][column].rgbtRed = temp[row][column].rgbtRed;
            image[row][column].rgbtGreen = temp[row][column].rgbtGreen;
            image[row][column].rgbtBlue = temp[row][column].rgbtBlue;
        }
    }

    return;
}

我猜问题出在这些行上:

for ( int i = -1; i < 2; i++)
{
    for (int j = -1; j < 2; j++)
    {
        int currentRow = height + row;
        int currentColumn = width + column;

这里将currentRow设置为图像的全高,然后添加当前行索引。 总是在图像之外。 与专栏相同。

你应该做的是:

int currentRow = row + i;
int currentColumn = column + j;

这将获取当前行和列周围的像素并用于模糊处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM