模糊滤镜问题CS50

时间:2020-04-24 11:09:51

标签: c cs50

这是我从CS50x课程的滤镜课程中学到的模糊代码。当我这样做时,图像像素保持完全相同,甚至不会改变。谁能告诉我我哪里出问题了?

语言为C。在每个步骤之前的标题中显示了我执行步骤的原因。基本上,我会遍历所有像素并向其添加相邻像素,然后除以与原始像素相加的总数。

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE temp[height][width];
    RGBTRIPLE total[height][width];
    // Save all the initial colour values into a new variable
    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        {
            temp[i][j] = image[i][j];
        }
    }
    // Loop through the pixels and calculate average value for each
    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        {
            // Clear the pixel's colour values
            total[i][j].rgbtRed = 0;
            total[i][j].rgbtBlue = 0;
            total[i][j].rgbtGreen = 0;
            int counter = 0;
            // Loops through the blocks 1 column and 1 row away from the pixel
            for(int k = i - 1; k < i + 2; k++)
            {
                // If k is less than 0 or more than height, skip that step
                if(k < 0 || k > (height - 1))
                {
                    return;
                }
                for(int l = j - 1; l < j + 2; l++)
                {
                    // If l is less than 0 or more than wddth, skip that step
                    if(l < 0 || l > (width - 1))
                    {
                        return;
                    }
                    total[i][j].rgbtRed = temp[k][l].rgbtRed + total[i][j].rgbtRed;
                    total[i][j].rgbtBlue = temp[k][l].rgbtBlue + total[i][j].rgbtBlue;
                    total[i][j].rgbtGreen = temp[k][l].rgbtGreen + total[i][j].rgbtGreen;
                    counter++;
                }
            }
           // Divide the total by the number of counter
           image[i][j].rgbtRed = round((float) total[i][j].rgbtRed / counter);
           image[i][j].rgbtGreen = round((float) total[i][j].rgbtGreen / counter);
           image[i][j].rgbtBlue = round((float) total[i][j].rgbtBlue / counter);
        }
    }
    return;
}

1 个答案:

答案 0 :(得分:0)

您什么都不会退货。当创建代表新图像的新数组时,仅需要返回该图像。