numpy 3D图像阵列到2D
问题描述:
我有一个灰色图像的 3D-numpy 数组,看起来像这样:
I have a 3D-numpy array of a gray image, which looks something like this:
[[[120,120,120],[67,67,67]]...]
显然我有每个RG和B相同,因为它是一个灰色图像 - 这是redundent。
我想得到一个新的2D数组,看起来像:
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:
[[120,67]...]
这意味着将每个像素的数组[x,x,x]仅取值x
Which means to take every pixel's array [x,x,x] to just the value x
我该怎么做?
答
如果你的形状 ndarray 是(M,N,3),那么你可以得到这样的(M,N)灰度图像:
If the shape of your ndarray
is (M, N, 3), then you can get an (M, N) gray-scale image like this:
>>> gray = img[:,:,0]