带有 python 的 numpy:将 3d 数组转换为 2d

numpy with python: convert 3d array to 2d

假设我有一张彩色图像,自然会用python中的3维数组表示,形状为(n x m x 3)并称之为img。

我想要一个新的二维数组,将其命名为 "narray" 以具有 (3,nxm) 的形状,这样该数组的每一行都包含 "flattened" 版本的 R,G , 和 B 通道。此外,它应该有 属性,我可以通过类似

的方式轻松重建任何原始通道
narray[0,].reshape(img.shape[0:2])    #so this should reconstruct back the R channel.

问题是如何从 "img" 构造 "narray"?简单的 img.reshape(3,-1) 不起作用,因为我不希望元素的顺序。

谢谢

您需要使用 np.transpose 重新排列维度。现在,n x m x 3要转换为3 x (n*m),因此将最后一个轴放在前面,并将其余轴的顺序右移(0,1)。最后,重塑为 3 行。因此,实现将是 -

img.transpose(2,0,1).reshape(3,-1)

样本运行-

In [16]: img
Out[16]: 
array([[[155,  33, 129],
        [161, 218,   6]],

       [[215, 142, 235],
        [143, 249, 164]],

       [[221,  71, 229],
        [ 56,  91, 120]],

       [[236,   4, 177],
        [171, 105,  40]]])

In [17]: img.transpose(2,0,1).reshape(3,-1)
Out[17]: 
array([[155, 161, 215, 143, 221,  56, 236, 171],
       [ 33, 218, 142, 249,  71,  91,   4, 105],
       [129,   6, 235, 164, 229, 120, 177,  40]])

[原始答案]

假设我们有一个大小为 m x n x 3 的数组 img 转换为大小为 3 x (m*n)

的数组 new_img

初步解决方案:

new_img = img.reshape((img.shape[0]*img.shape[1]), img.shape[2])
new_img = new_img.transpose()

[编辑后的答案]

缺陷:reshape从第一个维度开始,reshape remainder,这个解决方案有可能混合来自第三个维度的值。在图像的情况下,这在语义上可能是不正确的。

改编的解决方案:

# Dimensions: [m, n, 3]
new_img = new_img.transpose()
# Dimensions: [3, n, m]
new_img = img.reshape(img.shape[0], (img.shape[1]*img.shape[2]))

严格解:

# Dimensions: [m, n, 3]
new_img = new_img.transpose((2, 0, 1))
# Dimensions: [3, m, n]
new_img = img.reshape(img.shape[0], (img.shape[1]*img.shape[2]))

strict 是考虑维度顺序的更好方法,而 AdaptedStrict 的结果在值方面是相同的(set(new_img[0,...]) ),但是顺序打乱了。

如果你安装了 scikit 模块,那么你可以使用 rgb2grey(或 rgb2gray)使照片从彩色到灰色(从 3D 到 2D)

from skimage import io, color

lina_color = io.imread(path+img)
lina_gray = color.rgb2gray(lina_color)

In [33]: lina_color.shape
Out[33]: (1920, 1280, 3)

In [34]: lina_gray.shape
Out[34]: (1920, 1280)