Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Splitting and Merging Channels with OpenCV Python
A standard digital color image is represented by pixels, where each pixel is a combination of primary colors. A channel is a grayscale image that represents only one primary color component of a colored image. For example, an RGB image has three channels: red, green, and blue.
Observe the below colored images to see how each channel looks separately:

Below grayscale images are the representation of each channel of the RGB image.

In this article, we will discuss how to split and merge channels of an image using Python OpenCV library.
Splitting Channels
Python OpenCV module provides a function cv2.split() to split a multi-channel/colored array into separate single-channel arrays. It returns an array with the channels, each corresponding to blue, green, and red channels represented as a 2D ndarray.
Syntax
cv2.split(m[, mv])
Parameters:
src - input multi-channel array
mv - output array or vector of arrays
Example
In this example, we will take a color image "OpenCV_logo.png" and split it into 3 channels ?
import cv2
image = cv2.imread('Images/OpenCV_logo.png')
# Split the image into its three channels
(b_channel, g_channel, r_channel) = cv2.split(image)
# Display the images
cv2.imshow('blue channel', b_channel)
cv2.imshow('green channel', g_channel)
cv2.imshow('red channel', r_channel)
cv2.waitKey(0)
cv2.destroyAllWindows()
Input Image

Output Images

The color image "OpenCV_logo.png" has been split into three grayscale images: r_channel (Red), g_channel (Green), and b_channel (Blue).
Merging Channels
The cv2.merge() function takes single-channel arrays and combines them to create a multi-channel array/image. It returns an array containing the concatenated elements of the input arrays.
Syntax
cv2.merge(mv[, dst])
Parameters
mv - input vector of matrices to be merged. All matrices must have the same size and depth
dst - output array with the same size and depth as input arrays
Example
Let us merge the separate blue, green and red channels back into a BGR image ?
import cv2
image = cv2.imread('Images/OpenCV_logo.png')
# Split the image into its three channels
(b_channel, g_channel, r_channel) = cv2.split(image)
# Display the split channels
cv2.imshow('blue channel', b_channel)
cv2.imshow('green channel', g_channel)
cv2.imshow('red channel', r_channel)
# Merge the channels back
image_merged = cv2.merge((b_channel, g_channel, r_channel))
cv2.imshow('merged image', image_merged)
cv2.waitKey(0)
cv2.destroyAllWindows()
Input Image

Output Image

Working with CMYK Channels
In this example, we will convert an RGB image to CMYK colorspace and then split the channels ?
import cv2
import numpy as np
rgb = cv2.imread('Images/Tajmahal.jpg')
rgbdash = rgb.astype(np.float)/255.
K = 1 - np.max(rgbdash, axis=2)
C = (1 - rgbdash[..., 2] - K)/(1 - K)
M = (1 - rgbdash[..., 1] - K)/(1 - K)
Y = (1 - rgbdash[..., 0] - K)/(1 - K)
# Convert the input BGR image to CMYK colorspace
CMYK = (np.dstack((C, M, Y, K)) * 255).astype(np.uint8)
# Split CMYK channels
Y, M, C, K = cv2.split(CMYK)
# Display the channels
cv2.imshow("Cyan", C)
cv2.imshow("Magenta", M)
cv2.imshow("Yellow", Y)
cv2.imshow("Key", K)
cv2.waitKey(0)
cv2.destroyAllWindows()
Input Image

Output Image Cyan

Output Image Magenta

Output Image Yellow

Output Image Key

In the above example, the RGB image was converted into CMYK colorspace and split into four channels: Cyan, Magenta, Yellow, and Key (black).
Conclusion
Use cv2.split() to separate color channels into individual grayscale arrays and cv2.merge() to combine channels back into color images. These functions are essential for color space manipulation and channel-specific image processing operations.
