此问题是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类型的2D数组。
谢谢
我在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()
答案 0 :(得分:2)
来自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.
请注意浮动和整数的处理方式之间的区别。