在图像上应用颜色插值

发布于 2024-12-26 03:37:27 字数 1156 浏览 2 评论 0原文

我编写了一种方法,将 3 种颜色的渐变应用于图像。 颜色是蓝色->黄色->红色。 我得到了从黄色到红色的平滑过渡,但如果亮度低于 0.5,则从蓝色过渡到红色 -> 。黄色有从蓝色到绿色再到黄色的凹凸,所以一点也不光滑。 我希望图像具有类似热视觉的效果。

我不知道我做错了什么,我写了下面的方法。 我计算原始像素的亮度以应用于颜色模板。

public void applyGradient()
{
    size = width * height;
    float r, g, b;
    float redTemp, yellowTemp, blueTemp;
    float luminance = 0;
    float result;
    index = 0;
    redTemp = 0xff000000 | (255 << 16) | (0 << 8) | 0;
    yellowTemp = 0xff000000 | (255 << 16) | (255 << 8) | 0;
    blueTemp = 0xff000000 | (0 << 16) | (0 << 8) | 255;
    while(index < size)
    {
        r = (rgbInput[index]& 0x00FF0000) >> 16;
        g = (rgbInput[index]& 0x0000FF00) >> 8;
        b = (rgbInput[index]& 0x000000FF);


    luminance = ((r+b+g)/3)/255;
    if (luminance < 0.5)
    {
        result = (float) ((yellowTemp * luminance * 2.0) +  blueTemp * (0.5 - luminance) * 2.0);
    }
    else
    {
        result = (float) (redTemp * (luminance - 0.5) * 2.0 + yellowTemp * (1.0 - luminance) * 2.0);          
    }

    output[index] = (int)result;
    index++;
    }

}

I wrote a method to apply a gradient for 3 colors to an image.
The colors are Blue->Yellow->Red.
I am getting a smooth transition in colors from yellow to red but if luminance is below 0.5 the transition from Blue -> yellow is with bumps from blue to green to yellow, so it isn't smooth at all.
I would like to have a thermal vision like effect on the image.

I don't know what i'm doing wrong, i wrote the method below.
I calculate the luminance of the original pixel to apply to the color template.

public void applyGradient()
{
    size = width * height;
    float r, g, b;
    float redTemp, yellowTemp, blueTemp;
    float luminance = 0;
    float result;
    index = 0;
    redTemp = 0xff000000 | (255 << 16) | (0 << 8) | 0;
    yellowTemp = 0xff000000 | (255 << 16) | (255 << 8) | 0;
    blueTemp = 0xff000000 | (0 << 16) | (0 << 8) | 255;
    while(index < size)
    {
        r = (rgbInput[index]& 0x00FF0000) >> 16;
        g = (rgbInput[index]& 0x0000FF00) >> 8;
        b = (rgbInput[index]& 0x000000FF);


    luminance = ((r+b+g)/3)/255;
    if (luminance < 0.5)
    {
        result = (float) ((yellowTemp * luminance * 2.0) +  blueTemp * (0.5 - luminance) * 2.0);
    }
    else
    {
        result = (float) (redTemp * (luminance - 0.5) * 2.0 + yellowTemp * (1.0 - luminance) * 2.0);          
    }

    output[index] = (int)result;
    index++;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧 2025-01-02 03:37:27

根据亮度的分布,我认为你的问题在这里:

if (luminance < 0.5)
{
    result = (int) ((yellowTemp * luminance * 2.0) +  blueTemp * (0.5 - luminance) * 2.0);
}
else
{
    result = (int) (redTemp * (luminance - 0.5) * 2.0 + yellowTemp * (1.0 - luminance) * 2.0);          
}

如果亮度小于 0.5 并且亮度非常小,那么你会得到蓝色。
例如,如果亮度为 0.0000001,则黄色将为 (0.0000001 * 2.0) = 0.0000002,蓝色将为 ((0.5 - 0.0000001) * 2.0) = 0.9999998,蓝色比黄色高 6 个数量级。

如果亮度为 0.6,则红色将为 ((0.6 - 0.5) * 2.0) = 0.2,黄色将为 ((1.0 - 0.6) * 2.0) = 0.8,如果您的亮度分布始终位于那么如果亮度为 0.9,则红色将为 0.8,黄色将为 0.2。 (如果亮度 = 0.9999,则红色将为 0.9998,黄色为 0.0002)。

如果您检查亮度分布,并且它始终位于尾部,即始终非常小或非常大,那么您将不会得到任何黄色。

另一方面,如果您的亮度分布是高斯分布或对数正态分布,那么我建议您的 YellowTemp 或 redTemp 计算中有错误。

Depending on the distribution of luminance, I think your problem is here:

if (luminance < 0.5)
{
    result = (int) ((yellowTemp * luminance * 2.0) +  blueTemp * (0.5 - luminance) * 2.0);
}
else
{
    result = (int) (redTemp * (luminance - 0.5) * 2.0 + yellowTemp * (1.0 - luminance) * 2.0);          
}

If luminance is less than 0.5 AND luminance is very small, then you will get blue.
For example, if luminance is 0.0000001 then yellow will be (0.0000001 * 2.0) = 0.0000002 and blue will be ((0.5 - 0.0000001) * 2.0) = 0.9999998 which is 6 orders of magnitude more blue than yellow.

If luminance is 0.6 then red will be ((0.6 - 0.5) * 2.0) = 0.2 and yellow will be ((1.0 - 0.6) * 2.0) = 0.8 which should give you some yellow HOWEVER if your distribution of luminance is always in the tails then if luminance is 0.9 then red will be 0.8 and yellow will be 0.2. (and if luminance = 0.9999 then red will be 0.9998 and yellow 0.0002).

If you check the distribution of luminance and it is always in the tails i.e. always very small or very large, then you will not get any yellow.

On the other hand, if your distribution of luminance is Gaussian or log-normal, then I would suggest that you have an error in your yellowTemp or redTemp calculation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文