【发布时间】: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 <= 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