简体   繁体   English

当显示带有 plt.show() 的图像时 -> MemoryError: Unable to allocate array with shape (3600, 7200, 4) and data type float32

[英]When showing an image with plt.show() -> MemoryError : Unable to allocate array with shape (3600, 7200, 4) and data type float32

I am trying to plot an image with some things plotted on it (like points, axes, etc).我正在尝试 plot 一个图像,上面绘制了一些东西(如点、轴等)。 To do that, I use the library matplotlib.pyplot .为此,我使用库matplotlib.pyplot Here is the (reduced) code I use:这是我使用的(简化的)代码:

fig = plt.figure(figsize=(18, 18))
ax = fig.add_subplot(111)
img = plt.imread(stim_dir + image_name)

ax.imshow(img, extent=[-np.pi, np.pi, -np.pi / 2, np.pi / 2])
ax.scatter(*zip(*points), marker='^', s=1, c='red', alpha=0.4)
plt.xlim((-np.pi, np.pi))

plt.title(file_name)
plt.show()

Now, the code works fine when I use a png image, everything is perfect and all.现在,当我使用 png 图像时,代码可以正常工作,一切都很完美。 But I also have bmp ones to plot and there it doesn't work, cannot read a bitmap, but no worries I can convert it to a png one, However: these new png images doesn't work either and I get the error : MemoryError: Unable to allocate array with shape (3600, 7200, 4) and data type float32 .但是我也有到 plot 的 bmp 文件,但它不起作用,无法读取 bitmap,但不用担心我可以将其转换为 png 文件,但是:这些新的 png 图像也不起作用,我得到错误: MemoryError: Unable to allocate array with shape (3600, 7200, 4) and data type float32

So, I print the shape of the initial png image which worked fine, to see the difference(s):因此,我打印了工作正常的初始 png 图像的形状,以查看差异:

>>> print(img.shape)
(2160, 4320, 3)

My guess is that the third dimension should be of 3 (for RGB) instead of 4 (which may be ARGB), so I print the img array with the "new" not working image, which gives me something like:我的猜测是第三维应该是 3(对于 RGB)而不是 4(可能是 ARGB),所以我用“新”不工作的图像打印 img 数组,这给了我类似的东西:

[[[0.46666667 0.46666667 0.46666667 1.        ]
  [0.46666667 0.46666667 0.46666667 1.        ]
  [0.46666667 0.46666667 0.46666667 1.        ]
  ...

I see after checking, that every 4th number is equal to 1, so that is neat, I just have to delete it, it doesn't carry important information !查了一下,发现每4个数就等于1,好整齐,我只需要删掉它,它没有携带重要信息! I use img = np.delete(img, 3, 2) and it works well, I get the new shape of (3600, 7200, 3) with the values of each pixel like I want.我使用img = np.delete(img, 3, 2)并且效果很好,我得到了(3600, 7200, 3)的新形状以及我想要的每个像素的值。 However, it STILL doesn't work and STILL gives me the same error as before: MemoryError: Unable to allocate array with shape (3600, 7200, 4) and data type float32 , it seems like it doesn't take the new img array... (Obviously I do the delete just after the instruction img = plt.imread(stim_dir + image_name) ).但是,它仍然不起作用,并且仍然给我与以前相同的错误: MemoryError: Unable to allocate array with shape (3600, 7200, 4) and data type float32 ,似乎它不采用新的 img 数组...(显然我在指令img = plt.imread(stim_dir + image_name)之后执行删除)。

So, do I miss something obvious?那么,我错过了一些明显的东西吗? What could I do to make it work?我该怎么做才能让它发挥作用?

Sorry, not really a solution:对不起,不是真正的解决方案:

Apparently matplotlib is unable to allocate enough memory to plot all your datapoints, in the end you have 3600*7200*4 = 103.680.000 of them which is quite a number.显然 matplotlib 无法将足够的 memory 分配给 plot 你所有的数据点,最后你有 3600*78000*4 中的 3600*7800*4。 Not taking the scatterplot into account you want to plot as well.不考虑散点图,你也想 plot 。

What happens if you try to plot only a part of the image?如果您尝试仅对图像的一部分进行 plot 会发生什么? Lets say the upper left quarter?让我们说左上角?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MemoryError: 无法分配形状为 (2515, 406272) 且数据类型为 float32 的数组 - MemoryError: Unable to allocate array with shape (2515, 406272) and data type float32 MemoryError:在python中使用word2vec时无法分配形状和数据类型为float32的数组 - MemoryError: unable to allocate array with shape and data type float32 while using word2vec in python MemoryError: Unable to allocate 137. MiB for an array with shape (3000, 4000, 3) and data type float32 - MemoryError: Unable to allocate 137. MiB for an array with shape (3000, 4000, 3) and data type float32 numpy.core._exceptions.MemoryError:无法为形状为 (1073741824, 0) 且数据类型为 float32 的数组分配 0 个字节 - numpy.core._exceptions.MemoryError: Unable to allocate 0 bytes for an array with shape (1073741824, 0) and data type float32 MemoryError: Unable to allocate 5.62 GiB for an array with shape (16384, 30720, 3) and data type float32 when training StyleGan2 - MemoryError: Unable to allocate 5.62 GiB for an array with shape (16384, 30720, 3) and data type float32 When training StyleGan2 训练我的 model 时出现 Memory 错误:无法为形状为(3094、720、1280、3)和数据类型 float32 的数组分配 31.9 GiB - Memory error while training my model: Unable to allocate 31.9 GiB for an array with shape (3094, 720, 1280, 3) and data type float32 MemoryError: 无法为 DIPY 图像配准中具有形状 (344, 344, 127) 和数据类型 float64 的数组分配 115. MiB - MemoryError: Unable to allocate 115. MiB for an array with shape (344, 344, 127) and data type float64 in DIPY image registration MemoryError: 无法分配形状为 (200, 20, 244, 244, 3) 和数据类型为 float64 的数组 - MemoryError: Unable to allocate array with shape (200, 20, 244, 244, 3) and data type float64 MemoryError:无法为形状为 (323313, 3435) 且数据类型为 float64 的数组分配 8.27 GiB - MemoryError: Unable to allocate 8.27 GiB for an array with shape (323313, 3435) and data type float64 MemoryError: 无法分配形状 (118, 840983) 和数据类型 float64 的数组 - MemoryError: Unable to allocate array with shape (118, 840983) and data type float64
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM