【问题标题】:pyplot.cm instance is producing different results for same values but different data typepyplot.cm 实例为相同的值但不同的数据类型产生不同的结果
【发布时间】:2013-02-14 17:38:13
【问题描述】:

这个问题是tcaswell(答案#2)为我的问题提供的解决方案的延续:Is there a way to convert pyplot.imshow() object to numpy array?

考虑以下python代码:

import pylab
import numpy as np

a = np.array( ( 30, 129 ) , dtype = np.float32 )
b = np.array( ( 30, 129 ) , dtype = np.int32 )
my_cm = pylab.cm.get_cmap('jet')
a_mapped_data = my_cm( a )
b_mapped_data = my_cm( b )

我使用小数组来节省空间,但即使使用大数组也会出现这种情况。

结果:

>>> a
array([  30.,  129.], dtype=float32)

>>> b
array([ 30, 129])

>>> a_mapped_data
array([[ 0.5,  0. ,  0. ,  1. ],
       [ 0.5,  0. ,  0. ,  1. ]])

>>> b_mapped_data
array([[ 0.        ,  0.        ,  1.        ,  1.        ],
       [ 0.5028463 ,  1.        ,  0.46489564,  1.        ]])

我似乎不明白这里的行为。即使值相同,cm.get_map() 实例也会为numpy.int32 和numpy.float32 数据类型产生不同的结果。上面的代码有问题吗?请帮忙解决这个问题。我需要绘制 numpy.float 类型的二维数组。

谢谢

我在 Windows7 x64 Home Basic 上使用 python 2.7.3 32bit


编辑: 为那些和我遇到同样问题的人提供解决方案

下面的脚本对输入数据执行颜色映射,并将映射按原样转换为图像,不使用pylab.imshow 或pylab.pcolor,也没有任何比例或边框。我感谢所有做出贡献并帮助我了解如何完成的人。

import pylab
import numpy as np

a = np.random.random( (512, 512) )*100
# a is a 2D array of random data not in the range of 0.0 to 1.0

# normalize the data
normed_a = ( a - a.min() )/( a.max() - a.min() )

# now create an instance of pylab.cm.get_cmap()
my_cm = pylab.cm.get_cmap('jet_r')

# create the map
mapped_a = my_cm( normed_a )

# to display the map, opencv is being used
# import opencv
import cv2 as cv

# convert mapped data to 8 bit unsigned int
mapped_au8 = (255 * mapped_a).astype('uint8')

# show the image
cv.imshow( 'a', mapped_au8 )
cv.waitKey( 0 )
cv.destroyAllWindows()

编辑: 返回类型 cm.get_cmap 实例是 RGBA 格式,但 OpenCV 默认以 BGR 格式运行。因此,在显示通过转换上面代码中cm.get_cmap() 实例的返回值获得的任何图像之前,将其转换为 BGR 格式(在显示图像之前,默认情况下,在 opencv 中,ALPHA 通道无论如何都会被剥离,所以不要费心转换除非必要,否则将其放入 BGRA 中)。下面的代码给出了更好的解释:

mapped_au8 = (255 * mapped_a).astype('uint8')

#convert mapped_au8 into BGR fromat before display
mapped_u8 = cv.cvtColor( mapped_au8, cv.COLOR_RGBA2BGR )

# show the image
cv.imshow( 'a', mapped_au8 )
cv.waitKey( 0 )
cv.destroyAllWindows()

【问题讨论】:

    标签: python numpy matplotlib


    【解决方案1】:

    来自my_cm.__call__的文档字符串:

    *X* is either a scalar or an array (of any dimension).
    If scalar, a tuple of rgba values is returned, otherwise
    an array with the new shape = oldshape+(4,). If the X-values
    are integers, then they are used as indices into the array.
    If they are floating point, then they must be in the
    interval (0.0, 1.0).
    Alpha must be a scalar between 0 and 1, or None.
    If bytes is False, the rgba values will be floats on a
    0-1 scale; if True, they will be uint8, 0-255.
    

    注意浮点数和整数的处理方式之间的区别。

    【讨论】:

    • 那么,如果有一个像[12.02, 1.122][0.23, 7.543] 这样的数组,我可以用适用的最大位值(在本例中为 100)划分整个数组并绘制它吗?它应该产生正确的结果,对吧?
    • 好吧,我试着除以最大的位值。它认为它没有给出正确的结果.....
    • 位值是什么意思?
    • 也许s = (a - a.min()) / a.ptp(); a_mapped = my_cm(s);将数据线性映射到区间 [0,1]。
    • @Yash gah,对不起,我搞砸了。修正了我的另一个答案。
    猜你喜欢
    • 2020-12-15
    • 2019-06-16
    • 2019-07-25
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多