c语言 - 为什么滤镜对角落中的像素有效,但对其他任何东西都不起作用?



这是我对CS50 2020问题集4的失败解决方案"计算机科学导论";哈佛大学。问题集4筛选器(较少(-https://cs50.harvard.edu/x/2020/psets/4/filter/less/

在过去的一天里,我一直在努力寻找这段代码中的错误。在多次阅读代码后,它似乎在逻辑上是正确的。输出文件看起来很模糊,但代码未通过check50文本。

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
// Iterating through each row
for (int i = 0; i < height; i++)
{
// Iterating through each pixel in a row
for (int j = 0; j < width; j++)
{
// Declaring variables required to calculate average
float total_b = 0;
float total_g = 0;
float total_r = 0;
int counter = 0;

// Creating a copy of the image
RGBTRIPLE tmp[height][width];
tmp[i][j] = image[i][j];

// Checking if top-right neighbouring pixel is out of bounds
if ( i - 1 >= 0 && j + 1 < width)
{
total_b += image[i - 1][j + 1].rgbtBlue;
total_g += image[i - 1][j + 1].rgbtGreen;
total_r += image[i - 1][j + 1].rgbtRed;
counter++;
}

// Checking if top neighbouring pixel is out of bounds
if (i - 1 >= 0)
{
total_b += image[i - 1][j].rgbtBlue;
total_g += image[i - 1][j].rgbtGreen;
total_r += image[i - 1][j].rgbtRed;
counter++;
}

// Checking if top-left neighbouring pixel is out of bounds
if (i - 1 >= 0 && j - 1 >= 0)
{
total_b += image[i - 1][j - 1].rgbtBlue;
total_g += image[i - 1][j - 1].rgbtGreen;
total_r += image[i - 1][j - 1].rgbtRed;
counter++;
}

// Checking if left neighbouring pixel is out of bounds
if (j - 1 >= 0)
{
total_b += image[i][j - 1].rgbtBlue;
total_g += image[i][j - 1].rgbtGreen;
total_r += image[i][j - 1].rgbtRed;
counter++;
}

// Checking if bottom-left neighbouring pixel is out of bounds
if (i + 1 < height && j - 1 >= 0)
{
total_b += image[i + 1][j - 1].rgbtBlue;
total_g += image[i + 1][j - 1].rgbtGreen;
total_r += image[i + 1][j - 1].rgbtRed;
counter++;
}

// Checking if bottom neighbouring pixel is out of bounds
if (i + 1 < height)
{
total_b += image[i + 1][j].rgbtBlue;
total_g += image[i + 1][j].rgbtGreen;
total_r += image[i + 1][j].rgbtRed;
counter++;
}

// Checking if bottom-right neighbouring pixel is out of bounds
if (i + 1 < height && j + 1 < width)
{
total_b += image[i + 1][j + 1].rgbtBlue;
total_g += image[i + 1][j + 1].rgbtGreen;
total_r += image[i + 1][j + 1].rgbtRed;
counter++;
}

// Checking if right neighbouring pixel is out of bounds
if (j + 1 < width)
{
total_b += image[i][j + 1].rgbtBlue;
total_g += image[i][j + 1].rgbtGreen;
total_r += image[i][j + 1].rgbtRed;
counter++;
}

// Adding the values of the middle pixel to the total
total_b += image[i][j].rgbtBlue;
total_g += image[i][j].rgbtGreen;
total_r += image[i][j].rgbtRed;
counter++;

// Calculating the new values of rgb required
tmp[i][j].rgbtBlue = round(total_b / counter);
tmp[i][j].rgbtGreen = round(total_g / counter);
tmp[i][j].rgbtRed = round(total_r / counter);

// Changing the values of the original image
image[i][j].rgbtBlue = tmp[i][j].rgbtBlue;
image[i][j].rgbtGreen = tmp[i][j].rgbtGreen;
image[i][j].rgbtRed = tmp[i][j].rgbtRed;
}
}
return;
}

Check50给出以下结果-

:( blur correctly filters middle pixel expected "127 140 149n", not "145 160 169n"
:( blur correctly filters pixel on edge expected "80 95 105n", not "90 106 116n"
:) blur correctly filters pixel in corner
:( blur correctly filters 3x3 image expected "70 85 95n80 9...", not "70 85 95n90 1..."
:( blur correctly filters 4x4 image expected "70 85 95n80 9...", not "70 85 95n90 1..."

您边修改图像,每次迭代都覆盖image中的像素。当你模糊一个像素时,顶部和左侧的像素已经被模糊了,这会导致值出现错误。

您将需要修改代码,使原始像素保留更长的时间,而不是在执行时覆盖它们。最简单的方法是简单地创建源图像的模糊副本,并且只在循环完成后覆盖image

相关内容

最新更新