How to Split and Merge Color Channels in cv2

Let’s do it…

import cv2
import matplotlib.pyplot as plt
img = cv2.imread(‘test.jpg’)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
print(‘Shape of input image → ‘, img.shape)
r, g, b = cv2.split(img)
print('Shape of red channel --> ', r.shape)
print('Shape of green channel --> ', g.shape)
print('Shape of blue channel --> ', b.shape)
images = [cv2.merge((r, g, b)), r, g, b]

plt.subplot(2, 2, 1)
plt.xticks([])
plt.yticks([])
plt.imshow(images[0])
plt.title('original')

plt.subplot(2, 2, 2)
plt.xticks([])
plt.yticks([])
plt.imshow(images[1], cmap='Reds')
plt.title('red')

plt.subplot(2, 2, 3)
plt.xticks([])
plt.yticks([])
plt.imshow(images[2], cmap='Greens')
plt.title('green')

plt.subplot(2, 2, 4)
plt.xticks([])
plt.yticks([])
plt.imshow(images[3], cmap='Blues')
plt.title('blue')

plt.show()

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store