Search code examples
pythonnumpypython-3.7

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))?


Solution

  • If you are asking about a usage convention, in image processing for machine learning, I usually see each image flattened so that each image is one long row, in row-major order followed by channel order. Numpy has obj.flatten() command to make this easy. Then to retrieve the middle channel, Numpy slicing or indexing can be used. Each processed batch has many images (rows), and each image is one very long flattened row.

    Example:

    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])
    
    

    For other use cases, there may be a different convention.

    Longer example using a 3x3 image array with 3 channels.
    Note numerical values are in row-major order followed by channel order.

    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]])
    '''