[英]python - how to get the data from an plt.imshow()?
我有以下代码
import scipy.misc
import matplotlib.pyplot as plt
a = plt.imshow(scipy.misc.lena())
我希望实现的是通过访问a
或它的孩子来获取关于 lena 的数据。
原因是我将访问图像作为plt.gcf()
或plt.gca()
a
应该是一个matplotlib.image.AxesImage
实例,在这种情况下你可以使用
a.get_array()
和
a.set_array(data)
该数组存储为masked array
。
例子
http://matplotlib.org/examples/animation/dynamic_image.html 上有一个官方示例。
直接访问
你也可以使用
a._A
直接访问数组数据,尽管我认为 getter 和 setter 是首选方法。
### get image from the plot ###
plt.figure()
...
plt.imshow(image)
# remove white padding
plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
plt.axis('off')
plt.axis('image')
# redraw the canvas
fig = plt.gcf()
fig.canvas.draw()
# convert canvas to image using numpy
img = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
# opencv format
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
plt.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.