我希望首先将原始图像(其形状和数据类型为((1024, 1024, 3), dtype('uint8'))
)转换为1D数组,以便我可以将该1D数组作为一个观察结果输入到训练集中。
现在我想将该1D数组转换回其原始形式。
为了将原始图像转换为1D数组,我使用了numpy中可用的flatten()
函数。以下是代码:
In[80]: t = misc.imread('b.png') #to read the image
In[81]: t.shape, t.dtype
Out[81]: ((1024, 1024, 3), dtype('uint8'))
#To convert the above image into 1D array
In[82]: t.flatten()
Out[82]: array([ 5, 40, 121, ..., 130, 110, 89], dtype=uint8)
现在我想将上面的矩阵(来自
t.flatten()
的结果)转换回原始矩阵(即形状为(1024,1024,3)
)。请告诉我应该怎么做。
更新: 我检查了
t.flatten
的形状,发现它是:In[86]: p=t.flatten()
In[87]: p.shape
Out[86]:(6291456,)
但是6291456=(1024*1024*3*2)。现在我很困惑这个额外项(即2)从哪里来。
我也使用了reshape命令,但当我执行该命令时会出错。
l=p.reshape(1024,1024,3)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-89-b1ab41666df7> in <module>()
----> 1 l=p.reshape(1024,1024,3)
ValueError: total size of new array must be unchanged
p.shape
变为(6291456,)
的情况。 - akilat90