首页 > 解决方案 > Get the second channel from a three channels image as a Numpy array

问题描述

I'm using Python 3.7.7.

I have a three channels image as a Numpy array with this shape: (200, 200, 3).

Because it is so huge, I have tried to guess what I have to do with this example:

import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

print(a[1]) # Output [4, 5, 6]

But I'm not sure if I'm doing right.

What do I have to do to get the second channel from the image array (output shape (200, 200, 1))?

标签: pythonnumpypython-3.7

解决方案


如果您询问使用约定,在机器学习的图像处理中,我通常会看到每个图像被展平,以便每个图像是一个长行,按行优先顺序,然后是通道顺序。Numpy 有 obj.flatten() 命令使这变得容易。然后要检索中间通道,可以使用 Numpy 切片或索引。每个处理过的批次都有很多图像(行),每个图像都是一个很长的扁平行。

例子:

b = a.flatten()  
print(b)  
# output array([1, 2, 3, 4, 5, 6, 7, 8, 9])
channel2 = b[3:6]  
print(channel2) 
# output array([4, 5, 6])

对于其他用例,可能会有不同的约定。

使用具有 3 个通道的 3x3 图像阵列的更长示例。
请注意,数值按行优先顺序排列,然后是通道顺序。

img_a = np.arange(0, 27).reshape(3, 3, 3) 
''' output 
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]]])
'''  
# Flatten into one long row  
row_a = img_a.flatten()
# output array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
#       17, 18, 19, 20, 21, 22, 23, 24, 25, 26])

# Select middle channel using Numpy slicing    
channel_mid = row_a[9:18] 
# output array([ 9, 10, 11, 12, 13, 14, 15, 16, 17])

# Convert middle channel back into a matrix shape (if needed).
matrix_mid = channel_mid.reshape(3, 3)
''' output 
array([[ 9, 10, 11],
       [12, 13, 14],
       [15, 16, 17]])
'''       

推荐阅读