Life2Coding
Split Video Channels into RGB components using OpenCV in Python

This post will be helpful in learning OpenCV using Python programming. Here I will show how to implement OpenCV functions and apply it in various aspects using some examples. Then the output will be shown with some comparisons as well.

Requirements:

First, you need to setup your Python Environment with OpenCV. You can easily do it by following Life2Coding’s tutorial on YouTube: Linking OpenCV 3 with Python 3

Goals:

In this tutorial, I will show you how split the 3 channels of a color webcam video feed using OpenCV library and Python coding.

Documentation:

Python: cv2.split(m[, mv]) → mv

Divides a multi-channel array into several single-channel arrays.

Parameters:

  • m– input multi-channel array.
  • mv – output array or vector of arrays; in the first variant of the function the number of arrays must match channels() the arrays themselves are reallocated, if needed. The function cv2.split() splits a multi-channel array into separate single-channel arrays

Python: cv2.merge(mv[, dst]) → dst

Creates one multichannel array out of several single-channel ones.

Parameters:

  • mv– input array or vector of matrices to be merged; all the matrices in mv must have the same size and the same depth.
  • count– number of input matrices when mv is a plain C array; it must be greater than zero.
  • dst– output array of the same size and the same depth as mv[0]; The number of channels will be the total number of channels in the matrix array.

Steps:

  • Initialize webcam feed using cv2.VideoCapture()
  • Read webcam images using cv2.VideoCapture. read()
  • Split the BGR channels using cv2.split()
  • Merge the single channel with two zero matrix channels to create a color image using cv2.merge()
  • Display the output channel separately image using cv2.imshow()
  • Wait for keyboard button press using cv2.waitKey()
  • Exit window and destroy all windows using cv2.destroyAllWindows()

Example Code:

import cv2
import numpy as np

def split_video_channels(mirror=False):

    cap = cv2.VideoCapture(0)
    cv2.namedWindow('Webcam Life2Coding',cv2.WINDOW_NORMAL)
    zeros = None
    while True:
        ret_val, frame = cap.read()

        if ret_val == True:
            if mirror:
                #flip the image
                frame = cv2.flip(frame, 1)

            # split the image into its RGB channels
            height, width, layers = frame.shape
            zeroImgMatrix = np.zeros((height, width), dtype="uint8")

            # The OpenCV image sequence is Blue(B),Green(G) and Red(R)
            (B, G, R) = cv2.split(frame)

            # we would like to construct a 3 channel Image with only 1 channel filled
            # and other two channels will be filled with zeros
            B = cv2.merge([B, zeroImgMatrix, zeroImgMatrix])
            G = cv2.merge([zeroImgMatrix, G, zeroImgMatrix])
            R = cv2.merge([zeroImgMatrix, zeroImgMatrix, R])


            #we would like to show the 4 images like ( Original | Blue
            #                                          Green    | Red  )

            # so we need to double the image size as it will be 4 times the original image
            final = np.zeros((height * 2, width * 2, 3), dtype="uint8")

            final[0:height, 0:width] = frame # 1st Quarter=original
            final[0:height, width:width * 2] = B # 2nd Quarter= Blue
            final[height:height * 2, 0:width] = G   # 3rd Quarter= Red
            final[height:height * 2, width:width * 2] = R  # 4th Quarter= Green

            cv2.imshow('Webcam Life2Coding', final)
        else:
            break

        if cv2.waitKey(1) & 0xFF == ord('q'):  # if 'q' is pressed then quit
            break
    cap.release()
    cv2.destroyAllWindows()

def main():
    split_video_channels(mirror=True)

if __name__ == '__main__':
    main()

Output:

Capture-2 Split Video Channels into RGB components using OpenCV in Python

life2coding_icon [] Split Video Channels into RGB components using OpenCV in Python

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.