首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C中的过滤程序不能处理更复杂的图像

C中的过滤程序不能处理更复杂的图像
EN

Stack Overflow用户
提问于 2020-12-24 07:34:37
回答 1查看 43关注 0票数 1

我需要写一个程序,使灰度过滤器。它只能部分工作,我收到以下错误消息:

代码语言:javascript
复制
:) 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..."

代码如下:

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-24 09:22:41

round()无效,因为两个ints的商是int。对int进行舍入没有任何效果。

代码语言:javascript
复制
//                 ........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”。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65432224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档