【发布时间】:2020-02-18 16:16:33
【问题描述】:
现在已经被这个问题困扰了一段时间,我很困惑为什么我的代码没有按预期编译。我应该将 5 号像素的颜色值与其周围像素进行平均,这样生成的图像就会看起来模糊。这是我的代码:
// Blur image
// 1 | 2 | 3
// - - -
// 4 | 5 | 6
// - - -
// 7 | 8 | 9
//assuming i is 5
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 red = 0;
int green = 0;
int blue = 0;
float counter = 0;
// 5
red += copy[i][j].rgbtRed;
green += copy[i][j].rgbtGreen;
blue += copy[i][j].rgbtBlue;
counter++;
//1
if (i - 1 > -1 && j - 1 > -1)
{
red += copy[i - 1][j].rgbtRed;
green += copy[i - 1][j].rgbtGreen;
blue += copy[i - 1][j].rgbtBlue;
counter++;
}
// 2
if (i - 1 > -1)
{
red += copy[i - 1][j].rgbtRed;
green += copy[i - 1][j].rgbtGreen;
blue += copy[i - 1][j].rgbtBlue;
counter++;
}
// 3
if (i - 1 > -1 && j + 1 < height)
{
red += copy[i - 1][j + 1].rgbtRed;
green += copy[i - 1][j + 1].rgbtGreen;
blue += copy[i - 1][j + 1].rgbtBlue;
counter++;
}
// 4
if (j - 1 > -1)
{
red += copy[i][j - 1].rgbtRed;
green += copy[i][j - 1].rgbtGreen;
blue += copy[i][j - 1].rgbtBlue;
counter++;
}
// 6
if (j + 1 < width)
{
red += copy[i][j + 1].rgbtRed;
green += copy[i][j + 1].rgbtGreen;
blue += copy[i][j + 1].rgbtBlue;
counter++;
}
// 7
if (i + 1 < height && j - 1 > -1)
{
red += copy[i + 1][j - 1].rgbtRed;
green += copy[i + 1][j - 1].rgbtGreen;
blue += copy[i + 1][j - 1].rgbtBlue;
counter++;
}
// 8
if (i + 1 < height)
{
red += copy[i + 1][j].rgbtRed;
green += copy[i + 1][j].rgbtGreen;
blue += copy[i + 1][j].rgbtBlue;
counter++;
}
// 9
if (i + 1 < height && j + 1 < width)
{
red += copy[i + 1][j + 1].rgbtRed;
green += copy[i + 1][j + 1].rgbtGreen;
blue += copy[i + 1][j + 1].rgbtBlue;
counter++;
}
image[i][j].rgbtRed = round(red/counter);
image[i][j].rgbtGreen = round(green/counter);
image[i][j].rgbtBlue = round(blue/counter);
}
}
return;
}
我从中心像素开始检查并检查周围像素的存在,并相应地使用其周围像素的值来平均中心像素的值。我的代码编译得很好,当我运行它时它似乎正在工作。当我尝试运行 debug50 时;当我进行一些手动测试时,所有的值都会加起来。但是,当我尝试实施 Check50 时,会出现此错误消息
expected "127 140 149\n", not "130 143 153\n"
Log
testing with sample 3x3 image
first row: (10, 20, 30), (40, 50, 60), (70, 80, 90)
second row: (110, 130, 140), (120, 140, 150), (130, 150, 160)
third row: (200, 210, 220), (220, 230, 240), (240, 250, 255)
running ./testing 3 0...
checking for output "127 140 149\n"...
Expected Output:
127 140 149
Actual Output:
130 143 153
不太确定我的代码出了什么问题。任何建议将不胜感激!
【问题讨论】: