我需要写一个程序,使灰度过滤器。它只能部分工作,我收到以下错误消息:
:) grayscale correctly filters single pixel with whole number average
:( grayscale correctly filters single pixel without whole number average
expected "28 28 28\n", not "27 27 27\n"
:) grayscale leaves alone pixels that are already gray
:) grayscale correctly filters simple 3x3 image
:( grayscale correctly filters more complex 3x3 image
expected "20 20 20\n50 5...", not "20 20 20\n50 5..."
:( grayscale correctly filters 4x4 image
expected "20 20 20\n50 5...", not "20 20 20\n50 5..."代码如下:
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
for(int i = 0; i < height; i++) //Loop thought each row of 2D array
{
for(int j = 0; j < width; j++)//Loop through each pixel of each row
{
int red = image[i][j].rgbtRed;
int blue = image[i][j].rgbtBlue;
int green = image[i][j].rgbtGreen;
int avr = round((red + blue + green) / 3);
image[i][j].rgbtBlue = image[i][j].rgbtGreen = image[i][j].rgbtRed = avr;
}
}
return;
}发布于 2020-12-24 09:22:41
round()无效,因为两个ints的商是int。对int进行舍入没有任何效果。
// ........int........ / int
// int avr = round((red + blue + green) / 3);
// Divide by a `double`
int avr = round((red + blue + green) / 3.0);
// or
int avr = lround((red + blue + green) / 3.0);
// or even better, round with integer math.
int avr = ((red + blue + green)*2 + 3)/6;可能存在其他问题,但这解释了“预期的"28 28 28\n",而不是"27 27 27\n”。
https://stackoverflow.com/questions/65432224
复制相似问题