3

我需要将 0.0 到 1.0 的值转换为它们的颜色表示(每个值都是一个像素)。我正在制作频谱图,因此像素应该越高,值越高(如下图所示)。

我怎样才能做到这一点?我在 C# 中工作,但一个普遍适用的解决方案也很好。

例子:

在此处输入图像描述

4

2 回答 2

7

这个答案不仅应该向您展示如何从单个浮点值创建颜色,而且输出应该与显示的强度比例非常相似。

private float LinearStep(float x, float start, float width = 0.2f)
{
    if (x < start)
        return 0f;
    else if (x > start+width)
        return 1;
    else
        return (x-start) / width;
}

private float GetRedValue(float intensity)
{
    return LinearStep(intensity, 0.2f);
}

private float GetGreenValue(float intensity)
{
    return LinearStep(intensity, 0.6f);
}

private float GetBlueValue(float intensity)
{
    return LinearStep(intensity, 0f)
    - LinearStep(intensity, 0.4f)
    + LinearStep(intensity, 0.8f);
}

private Color getColor(float intensity)
{
    return Color.FromArgb(255,
        (int)(255*GetRedValue(intensity)),
        (int)(255*GetGreenValue(intensity)),
        (int)(255*GetBlueValue(intensity))
    );
}

我在记事本++中做了这个,所以它没有经过测试。但是你应该明白了;-)

当然,您可以使用它来创建查找表。这完全取决于你;-)


由于我无法将此图像放在评论中,因此这是获得此解决方案的过程: 图1

  1. Identify the "pure" colors. by this I mean the ones in the 8 colors palette where every component is either 255 or zero.
  2. Draw the dashed lines. You know the rgb values at these points.
  3. Interpolate linearly.

Step 3 is done by the LinearStep() function in the code, one call for each slope. Ascending slopes are added, descending slopes are subtracted. If you are unsure about this, try to do this with the original color map. I hope you can take it from here :-)

于 2013-04-07T22:23:25.680 回答
1

What you see here is a use of pseudocolor or false color. Depending on the field of application different palettes tend to be used, the one you show looks a lot like one commonly used in thermography, but there are several others that might be more adapted to your field of use. And the construction of such a palette is a bit more sophisticated than just cycling through rgb components by the way.

当我使用假彩色图像时,我们使用查找表,因为它是(并且可能是)迄今为止获取与输入值对应的颜色的最快方法。您显示的那个似乎有 256 个不同的值(这不足为奇),因此您需要一个包含 256 个颜色值的数组作为查找表。我个人建议编写一个小程序,通过从您喜欢的色标中读取像素并在您的主程序中使用它来构建一些源代码。可能还会有准备使用的源代码或色标信息,也许关键字热成像和伪彩色/假色可以帮助您进行搜索。

于 2013-04-07T22:23:57.983 回答