范围值到伪彩色 [英] Range values to pseudocolor

查看:17
本文介绍了范围值到伪彩色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个特定的浮点数组(在 Python 中),其范围可能从 0 到 100.我想创建一个伪彩色图像,以便颜色从绿色(对应于 0)变化到红色(100).这类似于 matplotlib 中的 pcolor.但是,我不想使用 pcolor.

是否有像 pseudocolorForValue(val,(minval,maxval)) 这样的函数返回一个 RGB 三元组,对应于 val 的伪颜色值?另外,这个功能是否可以灵活选择显示颜色是从绿色到红色还是从红色到绿色?

谢谢,尼克

解决方案

您可以编写自己的函数,将值转换为 0…100 → 0…120 度,然后将该值用作颜色的 H(或角度)在 HSV(或 HLS)色彩空间中.然后可以将其转换为 RGB 颜色以用于显示目的.在此颜色空间中计算线性解释的颜色通常看起来更好:HSV 颜色空间如下所示:

更新:

好消息,我惊喜地发现 Python 在其内置的

I have a certain array of floats (in Python) that might range from 0 to 100. I want to create a pseudo-color image so that the colors vary from green (corresponding to 0) to red (100). This is similar to pcolor from matplotlib. However, I do not want to use pcolor.

Is there a function like pseudocolorForValue(val,(minval,maxval)) which returns an RGB triple corresponding to the pseudo-color value for val? Also, is there a flexibility in this function to choose whether to display colors from green-to-red or from red-to-green?

Thanks, Nik

解决方案

You could write your own function that converted the values 0…100 → 0…120 degrees and then used that value as the H (or angle) of a color in the HSV (or HLS) colorspace. This could then be converted into an RGB color for display purposes. Linearly interpreted colors often look better when they're calculated in this colorspace: Here's what HSV colorspace looks like:

Update:

Good news, I was pleasantly surprised to discover that Python has colorspace conversion routines in its built-in colorsys module (they really mean "batteries included"). What's nice about that is that it makes creating a function that does what I described fairly easy, as illustrated below:

from colorsys import hsv_to_rgb

def pseudocolor(val, minval, maxval):
    """ Convert val in range minval..maxval to the range 0..120 degrees which
        correspond to the colors Red and Green in the HSV colorspace.
    """
    h = (float(val-minval) / (maxval-minval)) * 120

    # Convert hsv color (h,1,1) to its rgb equivalent.
    # Note: hsv_to_rgb() function expects h to be in the range 0..1 not 0..360
    r, g, b = hsv_to_rgb(h/360, 1., 1.)
    return r, g, b

if __name__ == '__main__':
    steps = 10

    print('val       R      G      B')
    for val in range(0, 100+steps, steps):
        print('{:3d} -> ({:.3f}, {:.3f}, {:.3f})'.format(
                                                val, *pseudocolor(val, 0, 100)))

Output:

val       R      G      B
  0 -> (1.000, 0.000, 0.000)
 10 -> (1.000, 0.200, 0.000)
 20 -> (1.000, 0.400, 0.000)
 30 -> (1.000, 0.600, 0.000)
 40 -> (1.000, 0.800, 0.000)
 50 -> (1.000, 1.000, 0.000)
 60 -> (0.800, 1.000, 0.000)
 70 -> (0.600, 1.000, 0.000)
 80 -> (0.400, 1.000, 0.000)
 90 -> (0.200, 1.000, 0.000)
100 -> (0.000, 1.000, 0.000)

Here's a sample showing what its output looks like:

I think you may find the colors generated nicer than in my other answer.

Generalizing:

It's possible to modify this function to be a little more generic in the sense that it will work with colors other then just the Red and Green currently hardcoded into it.

Here's how to do that:

def pseudocolor(val, minval, maxval, start_hue, stop_hue):
    """ Convert val in range minval..maxval to the range start_hue..stop_hue
        degrees in the HSV colorspace.
    """
    h = (float(val-minval) / (maxval-minval)) * (stop_hue-start_hue) + start_hue

    # Convert hsv color (h,1,1) to its rgb equivalent.
    # Note: hsv_to_rgb() function expects h to be in the range 0..1 not 0..360
    r, g, b = hsv_to_rgb(h/360, 1., 1.)
    return r, g, b

if __name__ == '__main__':
    # angles of common colors in hsv colorspace
    RED, YELLOW, GREEN, CYAN, BLUE, MAGENTA = range(0, 360, 60)
    steps = 10

    print('val       R      G      B')
    for val in range(0, 100+steps, steps):
        print('{:3d} -> ({:.3f}, {:.3f}, {:.3f})'.format(
                val, *pseudocolor(val, 0, 100, YELLOW, BLUE)))

Results:

这篇关于范围值到伪彩色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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