Note that there are some explanatory texts on larger screens.

plurals
  1. POpyplot.cm instance is producing different results for same values but different data type
    primarykey
    data
    text
    <p>This question is in continuation of the solution provided by <a href="https://stackoverflow.com/users/380231/tcaswell">tcaswell</a> (answer #2) for my question: <a href="https://stackoverflow.com/questions/14869321/is-there-a-way-to-convert-pyplot-imshow-object-to-numpy-array">Is there a way to convert pyplot.imshow() object to numpy array?</a> </p> <p>Consider the following python code:</p> <pre><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 ) </code></pre> <p>I am using a small array to save space, but this is what is seen even when large arrays are used.</p> <p>The results:</p> <pre><code>&gt;&gt;&gt; a array([ 30., 129.], dtype=float32) &gt;&gt;&gt; b array([ 30, 129]) &gt;&gt;&gt; a_mapped_data array([[ 0.5, 0. , 0. , 1. ], [ 0.5, 0. , 0. , 1. ]]) &gt;&gt;&gt; b_mapped_data array([[ 0. , 0. , 1. , 1. ], [ 0.5028463 , 1. , 0.46489564, 1. ]]) </code></pre> <p>I don't seem to understand the behavior here. Even though the values are same, <code>cm.get_map()</code> instance is producing different results for <code>numpy.int32</code> and <code>numpy.float32</code> data types. Is there something wrong with the code above? Please help out with this. I need to plot 2D arrays of type numpy.float.</p> <p>Thank you</p> <p>I am using python 2.7.3 32bit on Windows7 x64 Home Basic </p> <hr> <p><strong>EDIT :</strong> <strong><em>Solution for those who are facing the same problem as I did</em></strong></p> <p>The script below performs a color map on the input data and the map is converted to image as is, without using <code>pylab.imshow</code> or <code>pylab.pcolor</code> and without any scales or borders. I thank everyone contributed and helped me understand on how it can be done.</p> <pre><code>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() </code></pre> <hr> <p><strong>EDIT :</strong> Return type <code>cm.get_cmap</code> instance is of RGBA format but OpenCV by default operates on BGR format. Hence before displaying any image obtained by converting return values of <code>cm.get_cmap()</code> instance as in the above code, convert it to BGR format ( <em>The ALPHA channel is anyway stripped by default in opencv before the image is displayed so dont bother to convert it into BGRA unless necessary</em> ). The code below gives a better explanation:</p> <pre><code>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() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload