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!
Understanding Color Channels
Colour images are made of multiple channels. Each channel stores one component of the colour. Understanding channels is the foundation of all colour-based image processing.
What are Colour Channels?
A colour image is made of stacked grayscale layers called channels. Each channel stores one component of the colour information.
For a BGR image (OpenCV default):
- Channel 0: Blue component (0–255)
- Channel 1: Green component (0–255)
- Channel 2: Red component (0–255)
For an HSV image:
- Channel 0: Hue (0–179 in OpenCV) — the colour type
- Channel 1: Saturation (0–255) — how vivid/intense
- Channel 2: Value (0–255) — how bright
Thinking in channels allows you to isolate, modify, and analyse individual colour components independently.
Machine Learning follows a structured pipeline from data to deployment
Splitting and Merging Channels
import cv2
import numpy as np
image = cv2.imread("photo.jpg")
# Split into individual channels
b, g, r = cv2.split(image)
print(f"Blue channel shape: {b.shape}, mean: {b.mean():.1f}")
print(f"Green channel shape: {g.shape}, mean: {g.mean():.1f}")
print(f"Red channel shape: {r.shape}, mean: {r.mean():.1f}")
# Merge channels back into one image
merged = cv2.merge([b, g, r])
print(f"Merged shape: {merged.shape}")
# Create colour-isolated images for visualisation
zeros = np.zeros_like(b)
blue_image = cv2.merge([b, zeros, zeros])
green_image = cv2.merge([zeros, g, zeros])
red_image = cv2.merge([zeros, zeros, r])
cv2.imwrite("channel_blue.jpg", blue_image)
cv2.imwrite("channel_green.jpg", green_image)
cv2.imwrite("channel_red.jpg", red_image)
print("Channel isolation complete")