Colour Spaces - iffatAGheyas/computer-vision-handbook GitHub Wiki
๐จ Colour Spaces: RGB, HSV, YCrCb
Colour spaces define how colours are encoded and interpreted in digital images. Choosing the right space can simplify tasks like segmentation, tracking and compression.
๐ What is a Colour Space?
A colour space is a particular organisation of colours, allowing us to represent them as numerical values. Different spaces emphasise different aspects of colour, making them useful for various computer-vision tasks.
RGB = mixing paint ๐จ
HSV = describing colour in human terms (hue, saturation, brightness) ๐
YCrCb = separating brightness (luma) from colour (chroma) ๐ฅ
๐ต RGB (Red, Green, Blue)
- The most common colour space.
- Additive colour model: combine red, green and blue to create other hues.
- Used by screens, cameras and OpenCV by default.
Example pixel:
[R, G, B] = [120, 60, 200]
๐ HSV (Hue, Saturation, Value)
Closer to how humans perceive colour.
Very useful for colour-based filtering (e.g. detecting red objects).
Components
Component | Description |
---|---|
Hue (H) | Colour type (0โ179 in OpenCV, e.g. red, blue) |
Saturation (S) | Intensity/purity of colour (0โ255) |
Value (V) | Brightness (0โ255) |
Example:
Pure red: [H=0, S=255, V=255]
Light red: [H=0, S=100, V=255]
๐ฅ YCrCb (Luminance + Chrominance)
Separates Y (luminance/brightness) from Cr and Cb (red and blue chroma).
Widely used in video and image compression (JPEG, MPEG).
Component | Meaning |
---|---|
Y | Luminance (brightness) |
Cr | Red-difference chroma |
Cb | Blue-difference chroma |
Advantage: Brightness can be processed separately, improving compression and lighting control.
๐งช Python Code: RGB โ HSV and YCrCb Conversions
import cv2
# Read image in colour
img = cv2.imread("bird.jpg")
# Convert to HSV
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# Convert to YCrCb
img_ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
# Check shape and types
print("Original (BGR):", img.shape)
print("HSV:", img_hsv.shape)
print("YCrCb:", img_ycrcb.shape)
Output:
Original (BGR): (144, 349, 3)
HSV: (144, 349, 3)
YCrCb: (144, 349, 3)
All conversions keep the same dimensions and three channels.
๐ Visualization Tip
To see how the colour-space conversions affect the image, plot all three side-by-side using Matplotlib:
import cv2
import matplotlib.pyplot as plt
# Read the image
img = cv2.imread("bird.jpg")
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
img_ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
# Convert BGR โ RGB for correct display in Matplotlib
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Plot
plt.figure(figsize=(15, 5))
plt.subplot(1, 3, 1)
plt.imshow(img_rgb)
plt.title("Original (RGB)")
plt.axis("off")
plt.subplot(1, 3, 2)
plt.imshow(img_hsv)
plt.title("HSV")
plt.axis("off")
plt.subplot(1, 3, 3)
plt.imshow(img_ycrcb)
plt.title("YCrCb")
plt.axis("off")
plt.tight_layout()
plt.show()
โ Summary Table
Colour Space | Description | Use Case |
---|---|---|
RGB | Red, Green, Blue | Default format |
HSV | Hue, Saturation, Value | Colour filtering, segmentation |
YCrCb | Luminance + Chrominance | Compression, illumination |