在python中更改图像的数组形状

时间:2016-07-22 08:50:29

标签: image python-2.7 opencv

当我在OpenCV中读取彩色图像时,它显示尺寸为256x256x3。但我需要将它作为3x256x256数组传递给我的神经网络。如何更改阵列形状,保留BGR中的像素位置。

1 个答案:

答案 0 :(得分:1)

您可以简单地转置数组。例如,我的图片是10 x 10图片:

import numpy as np

#my picture
wrong_format = np.arange(300).reshape(10,10,3)

correct_format = wrong_format.T 

如果它正常工作,那么correct_format(0,1,1)应该等于wrong_format(1,1,0)。我们可以看到它是:

correct_format(0,1,1) == wrong_format(1,1,0)