numpy with python:将3d数组转换为2d

问题描述 投票:10回答:3

假设我有一个彩色图像,当然这将由python中的三维数组表示,比如说形状(n x m x 3)并将其称为img。

我想要一个新的2-d数组,称它为“narray”以具有形状(3,nxm),使得该数组的每一行分别包含R,G和B通道的“扁平”版本。此外,它应该具有我可以通过类似的东西轻松地重建任何原始频道的属性

narray[0,].reshape(img.shape[0:2])    #so this should reconstruct back the R channel.

问题是如何从“img”构建“叙事”?简单的img.reshape(3,-1)不起作用,因为元素的顺序对我来说是不可取的。

谢谢

python arrays numpy image-processing computer-vision
3个回答
0
投票

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

来自skimage import io,颜色

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.