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!
Color Images
Colour images store three values per pixel — Red, Green, and Blue — that combine to produce the full range of visible colours. Understanding colour representation is essential for image processing.
How Colour Is Stored
A colour image uses three channels per pixel. In standard RGB:
- Red channel: how much red is in the pixel (0–255)
- Green channel: how much green is in the pixel (0–255)
- Blue channel: how much blue is in the pixel (0–255)
These three values mix like light:
- (255, 0, 0) = red
- (0, 255, 0) = green
- (0, 0, 255) = blue
- (255, 255, 0) = yellow (red + green)
- (255, 0, 255) = magenta (red + blue)
- (0, 255, 255) = cyan (green + blue)
- (255, 255, 255) = white
- (0, 0, 0) = black
Important
OpenCV stores images in BGR order (Blue, Green, Red) — not RGB. This is a common source of bugs.
Machine Learning follows a structured pipeline from data to deployment
Working With Colour Channels
import cv2
import numpy as np
image = cv2.imread("photo.jpg") # Loaded in BGR format
# Split into individual channels
blue, green, red = cv2.split(image)
print("Channel shapes:", blue.shape, green.shape, red.shape)
# Each channel is a grayscale image of just that colour
# Access the blue value of a specific pixel
row, col = 50, 100
b = image[row, col, 0] # Blue
g = image[row, col, 1] # Green
r = image[row, col, 2] # Red
print(f"Pixel at ({row},{col}): B={b}, G={g}, R={r}")
# Convert BGR to RGB (for display with matplotlib)
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
print(f"Grayscale shape: {gray.shape}") # (height, width) — no channel dim