A color image is really three grayscale images stacked together: one for Blue, one for Green and one for Red. In this tutorial you will split an image into its channels, view each one, and merge them back.
What You Will Learn
- What color channels are
- How to split channels with
cv2.split() - How to read a channel image
- How to merge channels and show a channel in its own color
Prerequisites
Python 3 with OpenCV and NumPy, and a colorful image named ocv2.png in your script folder.
Step 1: Split the Image into B, G and R
import cv2
img = cv2.imread('ocv2.png', 1)
B, G, R = cv2.split(img)
print(B)
print(G)
print(R)
Explanation:
cv2.split()separates the 3 channels. OpenCV order is BGR, so the first is Blue.- Each channel is a 2D array of numbers from 0 to 255, the same height and width as the image.
cv2.split() is slow on big images. Indexing is faster: B = img[:, :, 0], G = img[:, :, 1], R = img[:, :, 2].Step 2: Show the Original Image
cv2.imshow('original', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Step 3: Show Each Channel
cv2.imshow('Blue', B)
cv2.waitKey(0)
cv2.imshow('Green', G)
cv2.waitKey(0)
cv2.imshow('Red', R)
cv2.waitKey(0)
cv2.destroyAllWindows()
Explanation: Each channel appears as a grayscale image, because it has only one value per pixel. Bright areas mean a lot of that color. For example, a red flower looks bright in the Red window and dark in the Blue window.
Three black-and-white windows. Compare them to see which parts of the picture contain each color.
Step 4: Show a Channel in Its Own Color
import numpy as np
zeros = np.zeros(img.shape[:2], dtype='uint8')
blue_img = cv2.merge([B, zeros, zeros])
green_img = cv2.merge([zeros, G, zeros])
red_img = cv2.merge([zeros, zeros, R])
cv2.imshow('Blue channel', blue_img)
cv2.imshow('Green channel', green_img)
cv2.imshow('Red channel', red_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Explanation: We build a 3-channel image where the other two channels are all zeros, so only one color is visible.
Step 5: Merge the Channels Back
merged = cv2.merge([B, G, R])
swapped = cv2.merge([R, G, B]) # swaps red and blue
cv2.imshow('Merged', merged)
cv2.imshow('Swapped', swapped)
cv2.waitKey(0)
cv2.destroyAllWindows()
Explanation: cv2.merge() joins channels into one image. Putting them in the wrong order swaps colors, which is exactly what happens when an OpenCV (BGR) image is shown in Matplotlib (RGB) without conversion.
Common Errors
| Problem | Cause and fix |
|---|---|
img is None / error (-215:Assertion failed) !_src.empty() |
OpenCV could not find the file. cv2.imread() does not raise an error for a wrong path, it returns None. Check the file name and run the script from the folder that contains the image. |
ModuleNotFoundError: No module named 'cv2' |
Install OpenCV: pip install opencv-python |
| Window does not open in Jupyter / Google Colab | cv2.imshow() needs a desktop. In Colab use from google.colab.patches import cv2_imshow, or display with Matplotlib. |
| Colors look blue instead of red in Matplotlib | Convert first: plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)). |
Practice Exercises
- Set the red channel to zero and display the result.
- Find which channel has the highest average with
B.mean(),G.mean(),R.mean(). - Convert the image to HSV with
cv2.COLOR_BGR2HSVand split its channels.
Interview Questions
Q: Why does a single channel display in grayscale?
It has one value per pixel, which imshow treats as intensity.
Q: What is the channel order in OpenCV?
BGR.
Summary
Use cv2.split() or indexing to get B, G, R channels, view them as grayscale intensity maps, and join them with cv2.merge().
Next: Arithmetic Operations on Images · Extracting RGB Values of a Pixel
Machine Learning Tutorials, Courses and Certifications