色彩图的非线性缩放以增强对比度

发布于 2024-11-17 17:00:18 字数 1334 浏览 5 评论 0原文

以下 python 代码创建包含正态分布值的矩阵的热图

import numpy as np
from matplotlib import pylab as plt


np.random.seed(123) #make sure we all have same data
m = np.random.randn(200).reshape(10, 20)
plt.imshow(m, cmap='RdYlGn', interpolation='nearest')
plt.colorbar()

这是此代码的输出

example 1

我会喜欢通过“淡出”接近于零的值来增强该图像的对比度。 我可以通过使用原始数据的双曲线缩放轻松地做到这一点,如下所示:

def disigmoidScaling(values, steepnessFactor=1, ref=None):
    ''' Sigmoid scaling in which values around a reference point are flattened
    arround a reference point

    Scaled value y is calculated as 
        y = sign(v - d)(1 - exp(-((x - d)/s)**2)))
    where v is the original value,  d is the referenc point and s is the 
    steepness factor
    '''
    if ref is None:
        mn = np.min(values)
        mx = np.max(values)
        ref = mn + (mx - mn) / 2.0

    sgn = np.sign(values - ref)
    term1 = ((values - ref)/steepnessFactor) ** 2
    term2 = np.exp(- term1) 
    term3 = 1.0 - term2 
    return sgn * term3


plt.imshow(disigmoidScaling(m, 4), cmap='RdYlGn', interpolation='nearest')
plt.colorbar()

这是输出。

example 2

我对结果很满意,除了这个版本中的原始版本 值已交换为缩放值。

有没有办法执行值到颜色图的非线性映射?

The following python code creates a heatmap of a matrix that contains normally distributed values

import numpy as np
from matplotlib import pylab as plt


np.random.seed(123) #make sure we all have same data
m = np.random.randn(200).reshape(10, 20)
plt.imshow(m, cmap='RdYlGn', interpolation='nearest')
plt.colorbar()

This is the output of this code

example 1

I would like to enhance the contrast of this image by "fading out" the values close to zero.
I can easily do this by using disigmoid scaling of the original data as follows:

def disigmoidScaling(values, steepnessFactor=1, ref=None):
    ''' Sigmoid scaling in which values around a reference point are flattened
    arround a reference point

    Scaled value y is calculated as 
        y = sign(v - d)(1 - exp(-((x - d)/s)**2)))
    where v is the original value,  d is the referenc point and s is the 
    steepness factor
    '''
    if ref is None:
        mn = np.min(values)
        mx = np.max(values)
        ref = mn + (mx - mn) / 2.0

    sgn = np.sign(values - ref)
    term1 = ((values - ref)/steepnessFactor) ** 2
    term2 = np.exp(- term1) 
    term3 = 1.0 - term2 
    return sgn * term3


plt.imshow(disigmoidScaling(m, 4), cmap='RdYlGn', interpolation='nearest')
plt.colorbar()

Here is the output.

example 2

I'm pleased with the result, except the fact that in this version the original
values have been exchanged for scaled ones.

Is there a way to perform a non-linear mapping of values to colormap?

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

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

发布评论

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

评论(1

给不了的爱 2024-11-24 17:00:18

颜色图包含映射在区间 [0,1] 上的红色、绿色和蓝色值的字典。 线性分段颜色图 类文档给出了示例

cdict = {'red':   [(0.0,  0.0, 0.0),
               (0.5,  1.0, 1.0),
               (1.0,  1.0, 1.0)],

     'green': [(0.0,  0.0, 0.0),
               (0.25, 0.0, 0.0),
               (0.75, 1.0, 1.0),
               (1.0,  1.0, 1.0)],

     'blue':  [(0.0,  0.0, 0.0),
               (0.5,  0.0, 0.0),
               (1.0,  1.0, 1.0)]}

“每一行给定颜色的表是 x、y0、y1 元组的序列。在每个序列中,x 必须从 0 单调增加到 1。对于任何输入值 z。落在 x[i] 和 x[i+1] 之间,给定颜色的输出值将在 y1[i] 和 y0[i+1] 之间线性插值:”

RdYlGn 颜色图具有每种颜色有 11 个 x 值,从 0 到 1.0,步长为 0.1。您可以通过调用获取 cdict 值,

plt.cm.RdYlGn._segmentdata

然后您可以将 x 值更改为您想要的任何步骤(只要它们单调递增且范围从 0 到 1),并通过调用 <新的 cdict 上的 code>matplotlib.colors.LinearSegmentedColormap 。 Matplotlib Cookbook 中有几个很好的例子。

A colormap contains a dictionary of red, green and blue values mapped over the interval [0,1]. The Linear Segmented Colormap class docs give the example

cdict = {'red':   [(0.0,  0.0, 0.0),
               (0.5,  1.0, 1.0),
               (1.0,  1.0, 1.0)],

     'green': [(0.0,  0.0, 0.0),
               (0.25, 0.0, 0.0),
               (0.75, 1.0, 1.0),
               (1.0,  1.0, 1.0)],

     'blue':  [(0.0,  0.0, 0.0),
               (0.5,  0.0, 0.0),
               (1.0,  1.0, 1.0)]}

"Each row in the table for a given color is a sequence of x, y0, y1 tuples. In each sequence, x must increase monotonically from 0 to 1. For any input value z falling between x[i] and x[i+1], the output value of a given color will be linearly interpolated between y1[i] and y0[i+1]:"

The RdYlGn colormap has 11 x values for each color going from 0 to 1.0 in steps of 0.1. You can get the cdict values by calling

plt.cm.RdYlGn._segmentdata

You can then change the x values to whatever steps you want (as long as they are monotonically increasing and range from 0 to 1) and get a new colormap by calling matplotlib.colors.LinearSegmentedColormap on your new cdict. There are several great examples of this in the Matplotlib Cookbook.

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