Python OpenCV - Remove Red Channel from Image


OpenCV - Remove Red 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 red channel.

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

Examples

1: Remove red color channel from given image

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

Input or Source Image

Python Program

import cv2
import numpy as np

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

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

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

Output Image

Python OpenCV - Remove Red Channel from Image

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

Summary

In this Python OpenCV Tutorial, we learned how to remove red channel from an image.