c-CS50模糊功能



我试图创建一个模糊函数,但它返回了错误的输出。该功能未通过所有检查,我不明白为什么:

:( blur correctly filters middle pixel
expected "127 140 149n", not "143 158 168n"
:( blur correctly filters pixel on edge
expected "80 95 105n", not "96 114 126n"
:( blur correctly filters pixel in corner
expected "70 85 95n", not "93 113 127n"
:( blur correctly filters 3x3 image
expected "70 85 95n80 9...", not "93 113 127n96..."
:( blur correctly filters 4x4 image
expected "70 85 95n80 9...", not "93 113 127n96..."

如果有人能检查我的代码并帮助我识别错误,我将不胜感激:

void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE copy[height][width];
int offsetx[] = {0, 1, 1, 1, 0, -1, -1, -1};
int offsety[] = {-1, -1, 0, 1, 1, 1, 0, -1};
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
int sum_Red = 0;
int sum_Green = 0;
int sum_Blue = 0;
int counter = 0;
for(int i = 0; i < 9; i++)
{
int r = row + offsetx[i];
int c = col + offsety[i];

if (r >= 0 && r < height && c >= 0 && c < width)
{
sum_Red += image[r][c].rgbtRed;
sum_Green += image[r][c].rgbtGreen;
sum_Blue += image[r][c].rgbtBlue;
counter++;
}
}
copy[row][col].rgbtRed = round(sum_Red / (double)counter);
copy[row][col].rgbtGreen = round(sum_Green / (double)counter);
copy[row][col].rgbtBlue = round(sum_Blue / (double)counter);
}
}
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
image[row][col] = copy[row][col];
}
}
return;
}

感谢您的时间和帮助。

我的问题已经在评论中得到了回答,但我会把答案留在这里,以防有人错过。在应用了所有建议后,仍然需要为偏移数组添加一个条目,如下所示:

int offsetx[] = {0, 1, 1, 1, 0, -1, -1, -1, 0};
int offsety[] = {-1, -1, 0, 1, 1, 1, 0, -1, 0};

最新更新