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

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.

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.

LLM Project to Build and Fine Tune a Large Language Model
In this LLM project for beginners, you will learn to build a knowledge-grounded chatbot using LLM's and learn how to fine tune it.

Azure Deep Learning-Deploy RNN CNN models for TimeSeries
In this Azure MLOps Project, you will learn to perform docker-based deployment of RNN and CNN Models for Time Series Forecasting on Azure Cloud.

Build an AI Chatbot from Scratch using Keras Sequential Model
In this NLP Project, you will learn how to build an AI Chatbot from Scratch using Keras Sequential Model.

Learn How to Build PyTorch Neural Networks from Scratch
In this deep learning project, you will learn how to build PyTorch neural networks from scratch.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

Build CI/CD Pipeline for Machine Learning Projects using Jenkins
In this project, you will learn how to create a CI/CD pipeline for a search engine application using Jenkins.

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.