创建可变长度的颜色查找表

3 投票
2 回答
2316 浏览
提问于 2025-04-16 02:38

我想知道有没有人能给我一些建议,或者推荐一些关于创建颜色查找表用于图像合成的好资源。在我的应用中,我有一些浮点值,范围在-1.0到1.0之间,这些值需要映射到RGB颜色空间。问题是,我不知道这些浮点值的精度会是什么样的,所以我不确定该在查找表中放多少条目,也不知道它们应该是什么。有没有什么常用的方法可以把这种数据映射到颜色上?似乎为每张图像根据数据范围创建一个新的颜色表会太耗费资源。

我想定义一个值的范围来进行映射可能会有效,但我想听听你们的想法。如果有人知道现成的工具(最好是基于Python的)来创建颜色查找表,那就太好了。

2 个回答

1

如果你需要使用查找表,并且处理的是浮点数据,你就得把浮点数据分成不同的范围,然后在你的表里查找每个范围。

不过,在这种情况下用查找表似乎不太合适;为什么不定义一个映射函数,输入你的浮点值,输出RGB值呢?我用这个方法给分形上色过(可以看看这个链接里的“上色”部分)。

基本上,我的方法是做一个简化的HSV到RGB的转换,使用固定的饱和度和亮度值,而把浮点输入数据作为色相。这会给你的值输出以下RGB结果:

i to RGB conversion

想了解用这个映射函数上色的分形,可以查看这个链接

我有一些C语言代码可以实现这个功能,转换成Python也很简单。注意,这段代码假设0 <= i <= 1,而你可能想要的是-1 <= i <= 1:

/* for a value x (which is between x_min and x_max), interpolate a y value
 * (between y_min and y_max) of the same proportion.
 */
static float interpolate(float x, float x_min, float x_max,
        float y_min, float y_max)
{
    x = (x - x_min) / (x_max - x_min);
    return x * (y_max - y_min) + y_min;

}

/*
 * given a the i and i_max values from a point in our (x,y) coordinates,
 * compute the colour of the pixel at that point.
 *
 * This function does a simplified Hue,Saturation,Value transformation to
 * RGB. We take i/i_max as the Hue, and keep the saturation and value
 * components fixed.
 */
void colour_map(struct pixel *pix, float i, float i_max)
{
    const float saturation = 0.8;
    const float value = 0.8;
    float v_min, hue;

    hue = i / (i_max + 1);
    v_min = value * (1 - saturation);

    /* create two linear curves, between value and v_min, of the
     * proportion of a colour to include in the rgb output. One
     * is ascending over the 60 degrees, the other descending
     */

    if (hue < 0.25) {
        pix->r = value * 255;
        pix->g = interpolate(hue, 0.0, 0.25, v_min, value) * 255;
        pix->b = v_min * 255;

    } else if (hue < 0.5) {
        pix->r = interpolate(hue, 0.25, 0.5, value, v_min) * 255;
        pix->g = value * 255;
        pix->b = v_min * 255;

    } else if (hue < 0.75) {
        pix->r = v_min * 255;
        pix->g = value * 255;
        pix->b = interpolate(hue, 0.5, 0.75, v_min, value) * 255;

    } else {
        pix->r = v_min * 255;
        pix->g = interpolate(hue, 0.75, 1.0, value, v_min) * 255;
        pix->b = value * 255;
    }

    pix->a = 255;
}
1

你要找的词是伪彩色图像

选择颜色的方式取决于你想展示什么内容。简单的方法是把数据分成两部分,一部分在中间值之上,另一部分在中间值之下。

对于低的一半,你可以设置红色为0,然后蓝色等于(255 - 你的值),绿色等于(你的值)。这样就能得到一种颜色,最低值时是最蓝的,最高值时是绿色。

然后在高的一半,设置蓝色为0,红色等于你的值,绿色等于255 - (你的值),这样就能得到一种颜色,最高值时是红色,最低值时是绿色。

你还可以改变曲线的形状,以突出特定的范围。

撰写回答