Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
RGB Color Channels
Every colour image is made of three separate colour layers called channels. Understanding channels lets you manipulate specific colours independently.
The Three Channels
A colour image is essentially three grayscale images stacked together:
- Red channel: shows how much red is at each pixel
- Green channel: shows how much green is at each pixel
- Blue channel: shows how much blue is at each pixel
When these three channels are combined, they produce the full-colour image.
Note on OpenCV: OpenCV stores colour images as BGR (Blue first), not RGB. When you use matplotlib or convert for display purposes, you must switch to RGB.
Note on value ranges:
- With uint8 (default): values range from 0 to 255
- With float32 (normalised): values range from 0.0 to 1.0
(neural networks typically expect normalised float inputs)
Machine Learning follows a structured pipeline from data to deployment
Splitting and Visualising Channels
import cv2
import numpy as np
image = cv2.imread("photo.jpg") # BGR format
# Split into three channels
b_channel, g_channel, r_channel = cv2.split(image)
# Create colour visualisations for each channel
zeros = np.zeros_like(b_channel)
# Blue channel only (other channels set to zero)
blue_only = cv2.merge([b_channel, zeros, zeros])
# Green channel only
green_only = cv2.merge([zeros, g_channel, zeros])
# Red channel only
red_only = cv2.merge([zeros, zeros, r_channel])
# Statistics per channel
for name, ch in [("Blue", b_channel), ("Green", g_channel), ("Red", r_channel)]:
print(f"{name}: min={ch.min()}, max={ch.max()}, mean={ch.mean():.1f}")