This question is a continuation of tcaswell's solution (answer No. 2) for my question: is there a way to convert the pyplot.imshow () object to a numpy array?
Consider the following python code:
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 )
I use a small array to save space, but this is what is visible even when using large arrays.
Results:
>>> 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. ]])
I don't seem to understand this behavior. Although the values ββare the same, the instance cm.get_map()produces different results for the data types numpy.int32and numpy.float32. Is there something wrong with the code above? Please help with this. I need to build 2D arrays like numpy.float.
thank
I am using python 2.7.3 32bit on Windows7 x64 Home Basic
EDIT: , , ,
script , , pylab.imshow pylab.pcolor - . , .
import pylab
import numpy as np
a = np.random.random( (512, 512) )*100
normed_a = ( a - a.min() )/( a.max() - a.min() )
my_cm = pylab.cm.get_cmap('jet_r')
mapped_a = my_cm( normed_a )
import cv2 as cv
mapped_au8 = (255 * mapped_a).astype('uint8')
cv.imshow( 'a', mapped_au8 )
cv.waitKey( 0 )
cv.destroyAllWindows()
EDIT: cm.get_cmap RGBA, OpenCV BGR. , , cm.get_cmap(), , BGR ( ALPHA opencv , , BGRA ). :
mapped_au8 = (255 * mapped_a).astype('uint8')
mapped_u8 = cv.cvtColor( mapped_au8, cv.COLOR_RGBA2BGR )
cv.imshow( 'a', mapped_au8 )
cv.waitKey( 0 )
cv.destroyAllWindows()