Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a way to convert pyplot.imshow() object to numpy array?
    text
    copied!<p>I need to visualize a 2D numpy array. I am using pyplot for this. Here's the code:</p> <pre><code>import cv2 as cv import numpy as np from matplotlib import pyplot img = pyplot.imshow( radiance_val ) #radiance_val is a 2D numpy array of size = ( 512, 512 ) #filled with np.float32 values pyplot.show() </code></pre> <p>I am getting the output as expected.</p> <p>Now my question is, is there any way of converting "img" in the above code from pyplot type to numpy type. I need this so that I can load the visualization as opencv image and perform further processing on it. Im am using python 2.7, 32 bit.</p> <p>Kindly help</p> <p>Thank you</p> <hr> <p>EDIT 1: after Thorsten Kranz's solution</p> <pre><code>import numpy as np import cv2 as cv import matplotlib.pyplot as plt import PIL from cStringIO import StringIO frame1 = plt.gca() frame1.axes.get_xaxis().set_visible(False) frame1.axes.get_yaxis().set_visible(False) plt.imshow(np.random.random((10,10))) buffer_ = StringIO() plt.savefig( buffer_, format = "png", bbox_inches = 'tight', pad_inches = 0 ) buffer_.seek(0) image = PIL.Image.open( buffer_ ) ar = np.asarray(image) cv.imshow( 'a', ar ) cv.waitKey(0) cv.destroyAllWindows() </code></pre> <p>Here I am getting a runtime error from MS VC++ runtime library after the program terminates. My better guess is that it is because of the open "buffer_". But I am getting the required output.</p> <hr> <p>EDIT 2: closing the buffer using </p> <pre><code>buffer_.close() </code></pre> <p>did not solve the runtime error</p> <hr> <p><strong>FINAL EDIT</strong> : <a href="https://stackoverflow.com/questions/14880766/pyplot-cm-instance-is-producing-different-results-for-same-values-but-different"> <strong><em>SOLUTION</em></strong> </a></p>
 

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