numpy 3D图像阵列到2D

时间:2022-02-13 21:34:58

I have a 3D-numpy array of a gray image, which looks something like this:

我有一个灰色图像的3D-numpy数组,看起来像这样:

[[[120,120,120],[67,67,67]]...]

Obviously I have every R G and B the same because it is a gray image - this is redundent. I want to get a new 2D array which looks like:

显然我每个R G和B都是相同的,因为它是一个灰色的图像 - 这是多余的。我想得到一个新的2D数组,看起来像:

[[120,67]...]

Which means to take every pixel's array [x,x,x] to just the value x

这意味着将每个像素的数组[x,x,x]仅取值x

How can I do that?

我怎样才能做到这一点?

1 个解决方案

#1


12  

If the shape of your ndarray is (M, N, 3), then you can get an (M, N) gray-scale image like this:

如果你的ndarray的形状是(M,N,3),那么你可以得到这样的(M,N)灰度图像:

>>> gray = img[:,:,0]

#1


12  

If the shape of your ndarray is (M, N, 3), then you can get an (M, N) gray-scale image like this:

如果你的ndarray的形状是(M,N,3),那么你可以得到这样的(M,N)灰度图像:

>>> gray = img[:,:,0]