繁体   English   中英

无法在Python中将图像重塑为二维数组

[英]Cannot reshape image into 2d array in Python

我正在尝试使用以下代码将图像重塑为长2d数组(nrow * ncol,nband):

new_shape = (img.shape[0] * img.shape[1], img.shape[2] - 1)

img_as_array = img[:, :, :7].reshape(new_shape)
print('Reshaped from {o} to {n}'.format(o=img.shape,
                                        n=img_as_array.shape))

产生的错误如下:

ValueError                                Traceback (most recent call last)
<ipython-input-205-9de646941a34> in <module>
      2 new_shape = (img.shape[0] * img.shape[1], img.shape[2] - 1)
      3 
----> 4 img_as_array = img[:, :, :7].reshape(new_shape)
      5 print('Reshaped from {o} to {n}'.format(o=img.shape,
      6                                         n=img_as_array.shape))

ValueError: cannot reshape array of size 71696520 into shape (17924130,3)

正如我从你的错误中看到的那样,原始图像中有nband == 4 ,但是你的new_shape变量nband == 3当你从中减去一个时。 所以重塑的大小不匹配。 您可以根据您的问题使用其中一种解决方案解决问题。

new_shape = (img.shape[0] * img.shape[1], img.shape[2]) # 1. don't subtract

要么

img_as_array = img[:, :, :4].reshape(new_shape) # 2. make nband of image smaller

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM