How to split and merge images/channels using OpenCV

This recipe helps you split and merge images channels using OpenCV

Recipe Objective: How to split and merge images/channels using OpenCV?

This recipe explains how to split the channels of a color image and merge them to their original form.

Step 1: Importing library and reading the images

First, let us import the necessary libraries and read the image in default mode using the cv2.imread() function. The image that we are using in this recipe is the one below.

OpenCV logo
import cv2
import matplotlib.pyplot as plt
image = cv2.imread('OpenCV_Logo.jpg',cv2.IMREAD_COLOR)

Since we are reading the image using cv2.IMREAD_COLOR flag, the image will be read in B, G, R color scheme, and it will contain three channels

Step 2: Split the image into different channels

We can extract the pixel values of the blue, green, and red channels separately using the cv2.split() function. This function expects the image as the input parameter and returns three arrays of pixel data, each of which corresponds to blue, green, and red channels, respectively

(b_channel, g_channel, r_channel) = cv2.split(image)

We can see how the image looks after splitting these channels using the cv2.imshow() function and destroy the windows generated using the cv2.destroyAllWindows() function

cv2.imshow('blue channel',b_channel)
cv2.imshow('green channel',g_channel)
cv2.imshow('red channel',r_channel)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

The three separate images would look like this.

OpenCV split logo

Step 3: Merging channels

Now let us merge these separate blue, green and red channels into a BGR image. For this purpose, we use the cv2.merge() function, which takes the three channels that we separated previously as input and returns us a picture with all the three channels merged.

image_merged = cv2.merge((b_channel,g_channel,r_channel))

Now let us display the merged image and see how it looks using the cv2.imshow() function.

cv2.imshow('merged image',image_merged)
cv2.waitKey(0)

Output:

Merged output

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

Learn How to Build a Logistic Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple logistic regression model in PyTorch for customer churn prediction.

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

AWS MLOps Project to Deploy Multiple Linear Regression Model
Build and Deploy a Multiple Linear Regression Model in Python on AWS

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

CycleGAN Implementation for Image-To-Image Translation
In this GAN Deep Learning Project, you will learn how to build an image to image translation model in PyTorch with Cycle GAN.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.