【发布时间】:2017-05-29 17:57:17
【问题描述】:
我想首先将原始图像(形状和 dtype 为 ((1024, 1024, 3), dtype('uint8')))转换为一维数组,以便我可以将该一维数组输入到训练集中作为一次观察。
现在我想将该一维数组转换为其原始形式。
为了将原始图像转换为一维数组,我使用了 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.flattern()的结果)转换为原始矩阵(即(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
【问题讨论】:
-
只是
reshape具有所需的形状? -
@Divaker 我用过它,但它显示了一些模糊的错误
-
对于给定初始形状的数组,我无法重现
p.shape变为(6291456,)。 -
@akilat90 如果你按照我所做的,那么我认为你将能够得到相同的结果
标签: python numpy image-processing flatten