Python OpenCV - Remove Green Channel from Image


OpenCV - Remove Green Channel from Image

To remove red channel from color image, read image to BGR array using cv2.imread() and assign zeros to the 2D array corresponding to green channel.

In this tutorial, we shall use OpenCV Python library and transform an image, such that no green channel is present in the image.

Examples

1. Remove Green color channel from given image

In this example, we will remove the green channel from the following image.

Source or Input Image

Python Program

import cv2
import numpy as np

#read image
src = cv2.imread('D:/original.png', cv2.IMREAD_UNCHANGED)
print(src.shape)

# assign green channel to zeros
src[:,:,1] = np.zeros([src.shape[0], src.shape[1]])

#save image
cv2.imwrite('D:/no-green-channel.png',src) 

Output Image

Python OpenCV - Remove Green Channel from Image

This image has only red and blue channels of the original image.

Summary

In this Python OpenCV Tutorial, we learned how to extract green channel from an image.