CS50棕褐色问题,将图像从正常状态转换为棕褐色 [英] Cs50 sepia problem with converting image from normal to sepia

查看:118
本文介绍了CS50棕褐色问题,将图像从正常状态转换为棕褐色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个名为filter(不舒适,第4周)的CS50程序,它必须将图像从正常图像传输到棕褐色.除非必须转移白色,否则它工作正常.尝试转移白色时,它只是将其转换为蓝色和绿色.像这样:

I'm working on a cs50 program called filter(less comfortable, week 4), and it has to transfer images from normal to sepia. It's working fine unless it has to transfer the color white. When trying to transfer the color white, it just converts it to blue and green. Like so:

原始

SEPIA

如您所见,除了白色或接近于白色的颜色外,它可以将所有东西都转换得很好. 这是我的代码(仅适用于棕褐色部分):

As you can see, it converted everything fine, except for the white or close-to-white colors. Here's my code(Sepia part only):


void sepia(int height, int width, RGBTRIPLE image[height][width])
{
    for(int j = 0; j < width; j++)
    {
       for (int i = 0; i < height; i++)
       {
           int sepiared =  image[i][j].rgbtRed *.393  +   image[i][j].rgbtGreen *.769 +  image[i][j].rgbtBlue *.189;
           int sepiagreen =  image[i][j].rgbtRed *.349  +  image[i][j].rgbtGreen *.686 +  image[i][j].rgbtBlue *.168;
            int sepiablue =  image[i][j].rgbtRed *.272  +   image[i][j].rgbtGreen *.534 +  image[i][j].rgbtBlue *.131;
           image[i][j].rgbtRed = sepiared;
           image[i][j].rgbtGreen = sepiagreen;
           image[i][j].rgbtBlue = sepiablue;
       }
    }

    return;
}

请帮助我理解为什么会这样. Clang不打印任何错误消息.

Please help me understand why this happens. Clang prints no error messages.

您的确是, 代码丢失:)

Yours truly, Lost in code:)

推荐答案

因此您需要认真编辑代码

So You Need To Edit Your Code Sligitly

将原始图像暂时存储一段时间

Store Orginal Image To a temp for a while


            originalBlue = image[i][j].rgbtBlue;
            originalRed = image[i][j].rgbtRed;
            originalGreen = image[i][j].rgbtGreen;

每个公式的结果都可能不是整数,因此请使用float并将其四舍五入到最接近的整数

the result of each of these formulas may not be an integer so use float and round them to nearest integer

            sepiaRed = round(.393 * originalRed + .769 * originalGreen + .189 * originalBlue);
            sepiaGreen = round(.349 * originalRed + .686 * originalGreen + .168 * originalBlue);
            sepiaBlue = round(.272 * originalRed + .534 * originalGreen + .131 * originalBlue);

            if (sepiaRed > 255)
            {
                sepiaRed = 255;
            }

            if (sepiaGreen > 255)
            {
                sepiaGreen = 255;
            }

            if (sepiaBlue > 255)
            {
                sepiaBlue = 255;
            }

现在将值存储为原始值

            image[i][j].rgbtBlue = sepiaBlue;
            image[i][j].rgbtRed = sepiaRed;
            image[i][j].rgbtGreen = sepiaGreen;

在循环外声明所有变量

    float sepiaRed;
    float sepiaBlue;
    float sepiaGreen;
    int originalRed;
    int originalBlue;
    int originalGreen;

希望对您有帮助

这篇关于CS50棕褐色问题,将图像从正常状态转换为棕褐色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆