问题描述
我陷入CS50的Box-blur解决方案中,这是pset4的filter(较少)任务的一部分。当我运行程序时,什么都没有执行...
说明如下: 模糊
有多种方法可以使图像模糊或柔化。对于此问题,我们将使用“框模糊”功能,该功能通过获取每个像素,并针对每种颜色值,通过平均相邻像素的颜色值来为其赋予新值。
(示例) 每个像素的新值将是原始像素(形成3x3框)的1行和1列之内的所有像素值的平均值。例如,像素6的每个颜色值都可以通过平均像素1、2、3、5、6、7、9、10和11的原始颜色值来获得(请注意,像素6本身包含在平均)。同样,像素11的颜色值将通过平均像素6、7、8、10、11、12、14、15和16的颜色值获得。
对于沿边缘或角落的像素(如像素15),我们仍将查找1行和1列内的所有像素:在这种情况下,像素为10、11、12、14、15和16。
我的代码:
// Blur image
void blur(int height,int width,RGBTRIPLE image[height][width])
{
RGBTRIPLE temp[height][width];
int red = 0;
int blue = 0;
int green = 0;
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; coL++)
{
int count = 0;
int rowCoords[] = { row-1,row,row+1 };
int colCoords[] = { col-1,col,col+1 };
//3x3 square around pixel
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
int curR = rowCoords[r];
int curC = colCoords[c];
//check adjacents inside image
if (curR >= 0 && curR < height)
{
if (curC >= 0 && curC < width)
{
//total of all surrounding pixels
RGBTRIPLE pixel = image[curR][curC];
red = red + pixel.rgbtRed;
green = green + pixel.rgbtGreen;
blue = blue + pixel.rgbtBlue;
count++;
}
}
}
//average
temp[row][col].rgbtRed = round(red / count);
temp[row][col].rgbtGreen = round(green / count);
temp[row][col].rgbtBlue = round(blue / count);
}
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
//copy to original
image[i][j] = temp[i][j];
}
}
return;
}
解决方法
代码非常好。我会用不同的方式表示某些事情(例如,我认为if (X) if (Y)
会更清楚地表示为if (X && Y)
,并且通过围绕框索引的查找表进行的间接级别的额外混淆是不必要的),但是在功能上几乎是对。我看到的唯一问题是red
,green
和blue
的值仅初始化为0
一次。这需要在处理每个像素之前发生。因此,例如,您可以将这些行放在这里:
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
int count = 0;
int red = 0;
int blue = 0;
int green = 0;
...
也就是说,该错误应该导致图像中出现古怪的颜色值。它不应具有被描述为“什么都没有执行”的效果。因此,您的函数blur()
函数可能根本不会被调用。您可以在此函数的顶部放置一个断点以确保它确实被调用了吗?还是要发布main()
函数?