Colour Spaces
A better way to see colour.
A red apple in sunshine and the same apple in shadow have very different RGB numbers. To a simple program, they're different colours. Yet to you, it's still red. Your brain separates colour from brightness. We can teach a computer to do the same.

- Sunshine versus shadow
- Different numbers, same red
- Separate colour from brightness
HSV: colour on its own
HSV describes a pixel as hue, the colour itself, saturation, how vivid it is, and value, how bright it is. Now a range of hue picks out a colour, even if the lighting changes. Here, we find the red and brown parts of a photo, with one line. Run it. Our mask lights up where the colour matches. This was the first, simple version of AppleEye. Find the red pixels, and you've found your fruit, in just a few milliseconds.
import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import data
image = data.coffee()
hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
mask = cv2.inRange(hsv, (0, 90, 40), (20, 255, 255))
fig, ax = plt.subplots(1, 3, figsize=(10, 3.2))
ax[0].imshow(image); ax[0].set_title("Original")
ax[1].imshow(hsv[..., 0], cmap="hsv"); ax[1].set_title("Hue")
ax[2].imshow(mask, cmap="gray"); ax[2].set_title("Red-brown pixels")
for a in ax: a.axis("off")
plt.tight_layout(); plt.savefig("hsv.png", dpi=110)
print(f"Matching pixels: {(mask > 0).mean():.0%}")Matching pixels: 83%
Which colour space?
Use RGB for display, and for neural networks. Use HSV for simple colour rules, such as finding ripe fruit. Use LAB when you need to measure how different two colours look to a person, such as checking paint or fruit ripeness. Controlling your lighting is always better than any algorithm. A closed box with even lights will beat clever software in a dark barn.
Summary
Hue separates colour from brightness, and controlling light helps most of all. You've learned how to read an image. Next, the classic toolbox for changing images, starting with the most important operation in vision.
Key takeaways- HSV separates colour and brightness
- inRange picks a colour
- Control your lighting