因此,这个问题称为“过滤器” PSET4,我被分配来编写使图像模糊的“模糊”函数。我的代码模糊了示例图片,但是当我使用网站内工具检查代码时,它说该程序“无法正确模糊3x3和4x4图片”。这是代码:
该工具的内容如下: 4x4
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE image0[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image0[i][j] = image[i][j];
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (i == 0 && j == 0) //topleft
{
float red00 = round((float)(image0[0][0].rgbtRed + image0[0][1].rgbtRed + image0[1][1].rgbtRed + image[1][0].rgbtRed) / 4);
float green00 = round((image0[0][0].rgbtGreen + image0[0][1].rgbtGreen + image0[1][1].rgbtGreen + image[1][0].rgbtGreen) / 4);
float blue00 = round((image0[0][0].rgbtBlue + image0[0][1].rgbtBlue + image0[1][1].rgbtBlue + image[1][0].rgbtBlue) / 4);
image[0][0].rgbtRed = red00;
image[0][0].rgbtGreen = green00;
image[0][0].rgbtBlue = blue00;
}
else if (i == 0 && j == width - 1) //topright
{
float red01 = round((float)(image0[0][width - 1].rgbtRed + image0[0][width - 2].rgbtRed + image0[1][width - 2].rgbtRed + image0[1][width - 1].rgbtRed)
/ 4);
float green01 = round((float)(image0[0][width - 1].rgbtGreen + image0[0][width - 2].rgbtGreen + image0[1][width - 2].rgbtGreen + image0[1][width - 1].rgbtGreen)
/ 4);
float blue01 = round(((float)image0[0][width - 1].rgbtBlue + image0[0][width - 2].rgbtBlue + image0[1][width - 2].rgbtBlue + image0[1][width - 1].rgbtBlue)
/ 4);
image[0][width - 1].rgbtRed = red01;
image[0][width - 1].rgbtGreen = green01;
image[0][width - 1].rgbtBlue = blue01;
}
else if (i == height - 1 && j == 0) //bottomleft
{
float red10 = round((float)(image0[height - 1][0].rgbtRed + image0[height - 2][0].rgbtRed + image0[height - 2][1].rgbtRed + image0[height - 1][1].rgbtRed)
/ 4);
float green10 = round((float)(image0[height - 1][0].rgbtGreen + image0[height - 2][0].rgbtGreen + image0[height - 2][1].rgbtGreen + image0[height - 1][1].rgbtGreen)
/ 4);
float blue10 = round((float)(image0[height - 1][0].rgbtBlue + image0[height - 2][0].rgbtBlue + image0[height - 2][1].rgbtBlue + image0[height - 1][1].rgbtBlue)
/ 4);
image[height - 1][0].rgbtRed = red10;
image[height - 1][0].rgbtGreen = green10;
image[height - 1][0].rgbtBlue = blue10;
}
else if (i == height - 1 && j == width - 1) //bottomright
{
float red11 = round((float)(image0[height - 1][width - 1].rgbtRed + image0[height - 2][width - 1].rgbtRed + image0[height - 2][width - 2].rgbtRed
+ image0[height - 1][width - 2].rgbtRed) / 4);
float green11 = round((float)(image0[height - 1][width - 1].rgbtGreen + image0[height - 2][width - 1].rgbtGreen + image0[height - 2][width - 2].rgbtGreen
+ image0[height - 1][width - 2].rgbtGreen) / 4);
float blue11 = round((float)(image0[height - 1][width - 1].rgbtBlue + image0[height - 2][width - 1].rgbtBlue + image0[height - 2][width - 2].rgbtBlue
+ image0[height - 1][width - 2].rgbtBlue) / 4);
image[height - 1][width - 1].rgbtRed = red11;
image[height - 1][width - 1].rgbtGreen = green11;
image[height - 1][width - 1].rgbtBlue = blue11;
}
else if (i == 0 && j != 0 && j != width - 1) //top
{
float redtop = round((float)(image0[i][j].rgbtRed + image0[i][j - 1].rgbtRed + image0[i + 1][j - 1].rgbtRed
+ image0[i + 1][j].rgbtRed + image0[i + 1][j + 1].rgbtRed + image0[i][j + 1].rgbtRed) / 6);
float greentop = round((float)(image0[i][j].rgbtGreen + image0[i][j - 1].rgbtGreen + image0[i + 1][j - 1].rgbtGreen
+ image0[i + 1][j].rgbtGreen + image0[i + 1][j + 1].rgbtGreen + image0[i][j + 1].rgbtGreen) / 6);
float bluetop = round((float)(image0[i][j].rgbtBlue + image0[i][j - 1].rgbtBlue + image0[i + 1][j - 1].rgbtBlue
+ image0[i + 1][j].rgbtBlue + image0[i + 1][j + 1].rgbtBlue + image0[i][j + 1].rgbtBlue) / 6);
image[i][j].rgbtRed = redtop;
image[i][j].rgbtGreen = greentop;
image[i][j].rgbtBlue = bluetop;
}
else if (i != 0 && i != height - 1 && j == 0) //left
{
float redleft = round((float)(image0[i][j].rgbtRed + image0[i - 1][j].rgbtRed + image0[i - 1][j + 1].rgbtRed
+ image0[i][j + 1].rgbtRed + image0[i + 1][j + 1].rgbtRed + image0[i + 1][j].rgbtRed) / 6);
float greenleft = round((float)(image0[i][j].rgbtGreen + image0[i - 1][j].rgbtGreen + image0[i - 1][j + 1].rgbtGreen
+ image0[i][j + 1].rgbtGreen + image0[i + 1][j + 1].rgbtGreen + image0[i + 1][j].rgbtGreen) / 6);
float blueleft = round((float)(image0[i][j].rgbtBlue + image0[i - 1][j].rgbtBlue + image0[i - 1][j + 1].rgbtBlue
+ image0[i][j + 1].rgbtBlue + image0[i + 1][j + 1].rgbtBlue + image0[i + 1][j].rgbtBlue) / 6);
image[i][j].rgbtRed = redleft;
image[i][j].rgbtGreen = greenleft;
image[i][j].rgbtBlue = blueleft;
}
else if (i != 0 && i != height - 1 && j == width - 1) //right
{
float redright = round((float)(image0[i][j].rgbtRed + image[i - 1][j].rgbtRed + image0[i - 1][j - 1].rgbtRed + image0[i][j - 1].rgbtRed
+ image0[i + 1][j - 1].rgbtRed + image0[i + 1][j].rgbtRed) / 6);
float greenright = round((float)(image0[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image0[i - 1][j - 1].rgbtGreen + image0[i][j - 1].rgbtGreen
+ image0[i + 1][j - 1].rgbtGreen + image0[i + 1][j].rgbtGreen) / 6);
float blueright = round((float)(image0[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image0[i - 1][j - 1].rgbtBlue + image0[i][j - 1].rgbtBlue
+ image0[i + 1][j - 1].rgbtBlue + image0[i + 1][j].rgbtBlue) / 6);
image[i][j].rgbtRed = redright;
image[i][j].rgbtGreen = greenright;
image[i][j].rgbtBlue = blueright;
}
else if (i == height - 1 && j != 0 && j != width - 1) //bottom
{
float redbottom = round((float)(image0[i][j].rgbtRed + image0[i][j - 1].rgbtRed + image0[i - 1][j - 1].rgbtRed + image0[i - 1][j].rgbtRed
+ image0[i - 1][j + 1].rgbtRed + image0[i][j + 1].rgbtRed) / 6);
float greenbottom = round((float)(image0[i][j].rgbtGreen + image0[i][j - 1].rgbtGreen + image0[i - 1][j - 1].rgbtGreen + image0[i - 1][j].rgbtGreen
+ image0[i - 1][j + 1].rgbtGreen + image0[i][j + 1].rgbtGreen) / 6);
float bluebottom = round((float)(image0[i][j].rgbtBlue + image0[i][j - 1].rgbtBlue + image0[i - 1][j - 1].rgbtBlue + image0[i - 1][j].rgbtBlue
+ image0[i - 1][j + 1].rgbtBlue + image0[i][j + 1].rgbtBlue) / 6);
image[i][j].rgbtRed = redbottom;
image[i][j].rgbtGreen = greenbottom;
image[i][j].rgbtBlue = bluebottom;
}
else
{
float red = round((float)(image0[i][j].rgbtRed + image0[i - 1][j - 1].rgbtRed + image0[i - 1][j].rgbtRed + image0[i - 1][j + 1].rgbtRed
+ image0[i][j - 1].rgbtRed + image0[i][j + 1].rgbtRed + image0[i + 1][j - 1].rgbtRed + image0[i + 1][j].rgbtRed + image0[i + 1][j + 1].rgbtRed)
/ 9);
float green = round((float)(image0[i][j].rgbtGreen + image0[i - 1][j - 1].rgbtGreen + image0[i - 1][j].rgbtGreen + image0[i - 1][j + 1].rgbtGreen
+ image0[i][j - 1].rgbtGreen + image0[i][j + 1].rgbtGreen + image0[i + 1][j - 1].rgbtGreen + image0[i + 1][j].rgbtGreen + image0[i + 1][j + 1].rgbtGreen)
/ 9);
float blue = round((float)(image0[i][j].rgbtBlue + image0[i - 1][j - 1].rgbtBlue + image0[i - 1][j].rgbtBlue + image0[i - 1][j + 1].rgbtBlue
+ image0[i][j - 1].rgbtBlue + image0[i][j + 1].rgbtBlue + image0[i + 1][j - 1].rgbtBlue + image0[i + 1][j].rgbtBlue + image0[i + 1][j + 1].rgbtBlue)
/ 9);
image[i][j].rgbtRed = red;
image[i][j].rgbtGreen = green;
image[i][j].rgbtBlue = blue;
}
}
}
return;
}
答案 0 :(得分:1)
在这段代码中,您在循环函数中错误地使用了image
3次。应该是image0
。
if (i == 0 && j == 0) //topleft
{
float red00 = round((float)(image0[0][0].rgbtRed + image0[0][1].rgbtRed + image0[1][1].rgbtRed + image[1][0].rgbtRed) / 4);
float green00 = round((image0[0][0].rgbtGreen + image0[0][1].rgbtGreen + image0[1][1].rgbtGreen + image[1][0].rgbtGreen) / 4);
float blue00 = round((image0[0][0].rgbtBlue + image0[0][1].rgbtBlue + image0[1][1].rgbtBlue + image[1][0].rgbtBlue) / 4);