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!
Grayscale Images
A grayscale image stores a single brightness value per pixel. Many CV algorithms operate on grayscale images because they are simpler, faster to process, and colour is often not needed for the task.
What is a Grayscale Image?
A grayscale image removes colour and represents only brightness.
Each pixel holds a single value:
- 0 = completely black
- 255 = completely white
- 1–254 = shades of grey
A grayscale image of size 640×480 has:
- 640 × 480 = 307,200 values
The same image in colour (BGR) has:
- 640 × 480 × 3 = 921,600 values
Grayscale processing is faster and requires less memory. Many algorithms — edge detection, thresholding, morphological operations — work on grayscale images by default.
Machine Learning follows a structured pipeline from data to deployment
Converting and Using Grayscale
import cv2
import numpy as np
# Load and convert to grayscale
image = cv2.imread("photo.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
print(f"Colour image shape: {image.shape}") # (H, W, 3)
print(f"Grayscale shape: {gray.shape}") # (H, W)
# Memory comparison
colour_size = image.nbytes
gray_size = gray.nbytes
print(f"Colour memory: {colour_size:,} bytes")
print(f"Grayscale memory: {gray_size:,} bytes")
print(f"Memory reduction: {colour_size // gray_size}x smaller")
# Grayscale pixel values
print(f"Min brightness: {gray.min()}")
print(f"Max brightness: {gray.max()}")
print(f"Mean brightness: {gray.mean():.1f}")