【问题标题】:CS50 pset 4 // sepia filterCS50 pset 4 // 棕褐色滤镜
【发布时间】:2021-01-03 17:57:03
【问题描述】:

您好,我的 cs50 课程的棕褐色过滤器有问题,这是它给我的错误。我真的不确定问题是什么。如果有人可以帮助我,那就太好了

void sepia(int height, int width, RGBTRIPLE image[height][width])
{
double sepiaRed;
double sepiaGreen;
double sepiaBlue;

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        sepiaRed = (0.393 * image[i][j].rgbtRed) + (0.769 * image[i][j].rgbtGreen) + (0.189 * image[i][j].rgbtBlue);
        sepiaGreen = (0.349 * image[i][j].rgbtRed) + (0.686 * image[i][j].rgbtGreen) + (0.168 * image[i][j].rgbtBlue);
        sepiaBlue = (0.272 * image[i][j].rgbtRed) + (0.534 * image[i][j].rgbtGreen) + (0.131 * image[i][j].rgbtBlue);


        if( sepiaRed > 255)
        {
            image[i][j].rgbtRed = 255;
        }

        else if(sepiaGreen > 255)
        {
            image[i][j].rgbtGreen = 255;
        }

        else if(sepiaBlue > 255){
            image[i][j].rgbtBlue = 255;
        }

        else{
            image[i][j].rgbtRed = round(sepiaRed);
            image[i][j].rgbtGreen = round(sepiaGreen);
            image[i][j].rgbtBlue = round(sepiaBlue);
        }

     }
}
return;

}

【问题讨论】:

  • 您应该在专门的 CS50 StackExchange 站点 cs50.stackexchange.com 中提出这个问题
  • 浮点运算几乎总是会导致复合舍入错误,可能会导致错误的结果。尝试找出一种不使用浮点运算的方法来进行计算。
  • 谢谢大家,这也有助于我的知识谢谢

标签: c cs50


【解决方案1】:

在您将一个组件固定到255 的情况下,您还没有更新其他组件。我建议更改这部分代码中的错误逻辑

if( sepiaRed > 255) {
    image[i][j].rgbtRed = 255;
}
else if(sepiaGreen > 255) {
    image[i][j].rgbtGreen = 255;
}
else if(sepiaBlue > 255) {
    image[i][j].rgbtBlue = 255;
}
else {
    image[i][j].rgbtRed = round(sepiaRed);
    image[i][j].rgbtGreen = round(sepiaGreen);
    image[i][j].rgbtBlue = round(sepiaBlue);
}

to this,它会夹住每个组件,然后将它们全部写入

if(sepiaRed > 255) {
    sepiaRed = 255;
}
if(sepiaGreen > 255) {
    sepiaGreen = 255;
}
if(sepiaBlue > 255) {
    sepiaBlue = 255;
}
image[i][j].rgbtRed = round(sepiaRed);
image[i][j].rgbtGreen = round(sepiaGreen);
image[i][j].rgbtBlue = round(sepiaBlue);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 2020-04-05
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多