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.
- OpenCV 3.4
- Python 3.6+
- Image, Webcam or Video input
- Documentation Source: OpenCV Official Documentation
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
In this tutorial, I will show you how split the 3 channels of a color webcam video feed using OpenCV library and Python coding.
Python: cv2.split(m[, mv]) → mv
Divides a multi-channel array into several single-channel arrays.
- 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.
- 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; The number of channels will be the total number of channels in the matrix array.
- 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()
import numpy as np
cap = cv2.VideoCapture(0)
zeros = None
ret_val, frame = cap.read()
if ret_val == True:
#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)
if cv2.waitKey(1) & 0xFF == ord('q'): # if 'q' is pressed then quit
if __name__ == '__main__':
Latest posts by Life2Coding (see all)
- How to Install OpenCV 3.4.0 with Python 3 on Raspberry Pi 3 - 3 March, 2018
- Split Video Channels into RGB components using OpenCV in Python - 6 February, 2018
- Convert Image Frames to Video File using OpenCV in Python - 6 February, 2018