The Wayback Machine - https://web.archive.org/web/20240723145224/https://www.geeksforgeeks.org/splitting-and-merging-channels-with-python-opencv/
Open In App

Splitting and Merging Channels with Python-OpenCV

Last Updated : 03 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

In this article, we will learn how to split a multi-channel image into separate channels and combine those separate channels into a multi-channel image using OpenCV in Python. 

To do this, we use cv2.split() and cv2.merge() functions respectively. 

Image Used:

Splitting Channels

cv2.split() is used to split coloured/multi-channel image into separate single-channel images. The cv2.split() is an expensive operation in terms of performance(time). The order of the output vector of arrays depends on the order of channels of the input image.

Syntax: cv2.split(m[, mv])

Parameters:

  • m: Input multi-channel array
  • mv: Output vector of arrays

Example:

Python3




# Python program to explain splitting of channels
  
# Importing cv2
import cv2 
  
# Reading the image using imread() function
image = cv2.imread('img.jpg')
  
# Displaying the original BGR image
cv2.imshow('Original_Image', image)
  
# Using cv2.split() to split channels of coloured image 
b,g,r = cv2.split(image)
  
# Displaying Blue channel image
# Blue colour is highlighted the most
cv2.imshow("Model Blue Image", b)
  
# Displaying Green channel image
# Green colour is highlighted the most
cv2.imshow("Model Green Image", g)
  
# Displaying Red channel image
# Red colour is highlighted the most
cv2.imshow("Model Red Image", r)
  
# Waits for user to press any key
cv2.waitKey(0)


Output:

Merging Channels

cv2.merge() is used to merge several single-channel images into a colored/multi-channel image.

Syntax: cv2.merge(mv[, dst])

Parameters:

  • mv: Input vector of matrices to be merged. All matrices must have same size.
  • dst: Output multi-channel array of size mv[0]. Number of channel will be equal to total no. of channel in matrix array.

Example: 

Python3




# Python program to explain Merging of Channels
  
# Importing cv2
import cv2
  
# Reading the BGR image using imread() function
image = cv2.imread("img.jpg")
  
# Splitting the channels first to generate different 
# single
  
# channels for merging as we don't have separate
# channel images
b, g, r = cv2.split(image)
  
# Displaying Blue channel image
cv2.imshow("Model Blue Image", b)
  
# Displaying Green channel image
cv2.imshow("Model Green Image", g)
  
# Displaying Red channel image
cv2.imshow("Model Red Image", r)
  
# Using cv2.merge() to merge Red, Green, Blue Channels
  
# into a coloured/multi-channeled image
image_merge = cv2.merge([r, g, b])
  
# Displaying Merged RGB image
cv2.imshow("RGB_Image", image_merge)
  
# Waits for user to press any key
cv2.waitKey(0)


Output:



Similar Reads

Transition from OpenCV 2 to OpenCV 3.x
OpenCV is one of the most popular and most used Computer vision libraries. It contains tools to carry out image and video processing. When OpenCV 3..4.1 is an improved version of OpenCV 2.4 as it introduced new algorithms and features. Although some of the existing modules were rewritten and moved to sub-modules. In this articles, I will focus on t
2 min read
Top Books for Learning OpenCV: Computer Vision with OpenCV Library
OpenCV or Open Source Computer Vision Library, is an open-source computer vision and machine learning software library. It's extensively used for real-time computer vision tasks such as object detection, face recognition, image processing, etc. Whether you're a beginner or an experienced developer looking to deepen your understanding of OpenCV, her
5 min read
Automatic Document Scanner using OpenCV (OpenCV Document Scanner)
An automatic document scanner using OpenCV is a computer vision application that automatically detects and extracts documents from images. This type of scanner can be useful in various scenarios, such as digitizing paper documents, processing scanned documents, or automating document recognition tasks. In this article, we will see how we can build
6 min read
How to Find Mean Across the Image Channels in PyTorch?
In this article, we are going to see how to find mean across the image channels in PyTorch. We have to compute the mean of an image across the channels Red, Green, and, Blue. we can find the mean across the image channel by using torch.mean() method. torch.mean() method torch.mean() method is used to find the mean of all elements in the input tenso
2 min read
Splitting Cells and Counting Unique Values in Python List
In data processing, especially when dealing with tabular data, it's common to encounter cells containing multiple values separated by delimiters like commas or semicolons. Splitting these cells and counting the unique values can provide valuable insights and is a frequent task in data analysis. In this article, we will explore three different metho
2 min read
Python | Merging two strings with Suffix and Prefix
Given two strings A and B and these strings contain lower case letters. The task is to tell the length of the merged strings. For example, given A is "abcde" and B is "cdefg", then merging the two strings results in "abcdefg". The merge operation is performed in such a manner that the joining characters are both the suffix of A and Prefix of B. Bef
2 min read
Merging and Updating Dictionary Operators in Python 3.9
Python 3.9 is still in development and scheduled to be released in October this year. On Feb 26, alpha 4 versions have been released by the development team. One of the latest features in Python 3.9 is the merge and update operators. There are various ways in which Dictionaries can be merged by the use of various functions and constructors in Pytho
3 min read
Python | Merging two Dictionaries
In Python, a dictionary is a data structure that contains the element in the key-value pair in which keys are used to access the values in the dictionary. Python has some inbuilt dictionaries like defaultdict. In this article, we will see various ways to merge two dictionaries. Example Input: dict1 = {'a': 10, 'b': 8} dict2 = {'d': 6, 'c': 4}Output
7 min read
Splitting stereo audio to mono with PyDub
Splitting a stereo audio file into multiple mono audio files is very useful if you are trying to process or transcribe the stereo audio files. This is because stereo audio has 2 audio sources on different channels which makes it very hard to process the file. Splitting the stereo audio file into mono audio files makes this job easier. For this arti
2 min read
Python3 Program to Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
Given an array A[] of length N, where N is an even number, the task is to answer Q independent queries where each query consists of a positive integer K representing the number of circular shifts performed on the array and find the sum of elements by performing Bitwise OR operation on the divided array.Note: Each query begins with the original arra
5 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg