我一直在研究CS50计算机科学课程的第四套问题,并且在模糊滤镜中遇到了一个我不确定如何解决的问题。对于每个像素,此滤镜的目的是获取当前像素内3x3区域中所有颜色值的平均值,然后使用这些像素的平均值更改中间像素。我的滤镜使图片模糊,但与check50的要求略有出入。这是我的代码。
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE copy[height][width];
for (int i = 0;i < height;i++)
{
for (int j = 0;j < width;j++)
{
copy[i][j] = image[i][j];
}
}
for (int i = 0;i < height;i++)
{
for (int j = 0;j < width;j++)
{
int redAvrg = 0;
int blueAvrg = 0;
int greenAvrg = 0;
int count = 0;
for (int x = i - 1;x < i + 2;x++)
{
for (int y = j - 1;y < j + 2;y++)
{
if(x >= 0 && y >= 0 && x < height && y < width)
{
redAvrg += copy[x][y].rgbtRed;
blueAvrg += copy[x][y].rgbtBlue;
greenAvrg += copy[x][y].rgbtGreen;
count++;
}
}
}
image[i][j].rgbtRed = round(redAvrg / count);
image[i][j].rgbtBlue = round(blueAvrg / count);
image[i][j].rgbtGreen = round(greenAvrg / count);
}
}
return;
}