Introduction
Most cameras capture colour, and colour carries a lot of meaning: a ripe fruit, a red traffic light, a green screen behind an actor. In code, a colour image is just a greyscale image with three numbers in each pixel instead of one. This lesson shows how those numbers are stored, how to turn colour into greyscale correctly, and how a different colour space, HSV, makes it easy to pick out "all the orange pixels".
How it works
RGB. Screens mix red, green and blue light. A colour pixel is a triple (R, G, B), each from 0 to 255: (255, 0, 0) is pure red, (255, 255, 0) is yellow, (0, 0, 0) is black and (255, 255, 255) is white. A colour image is a grid of triples, or equally three grids, one per channel. The red channel of an image is [[p[0] for p in row] for row in img].
Channel order. Not every library uses the same order. Pillow and most file formats think in RGB, but OpenCV stores pixels as BGR: blue first. Showing an OpenCV image with an RGB tool swaps red and blue, so skin looks blue. Always check which order you have.
Greyscale. Many algorithms need one number per pixel. The simplest idea is the average (R + G + B) / 3, but people see green as much brighter than blue, so the average makes blue skies too bright and green leaves too dark. The standard luma formula weights the channels:
grey = 0.299 R + 0.587 G + 0.114 B
The weights add up to 1, so white stays 255. With whole numbers you can compute it exactly as (299 * r + 587 * g + 114 * b + 500) // 1000, where adding 500 rounds to the nearest whole number. OpenCV and Pillow both use these weights.
HSV. RGB mixes "what colour" with "how bright": a red apple in shadow has very different RGB numbers from the same apple in sunlight. HSV separates them:
- Hue is the colour's angle on a wheel: 0° red, 60° yellow, 120° green, 180° cyan, 240° blue, 300° magenta, and back to red at 360°.
- Saturation is how pure the colour is: 0 for greys, 1 for vivid colours. It is (max − min) / max.
- Value is the brightness of the strongest channel: max / 255.
Hue comes from whichever channel is largest. If red is largest, hue = 60 × (((G − B) / d) mod 6), where d = max − min; if green is largest, 60 × ((B − R) / d + 2); if blue, 60 × ((R − G) / d + 4). Greys have d = 0 and no real hue, so we call it 0.
To find coloured pixels, test a hue range together with minimum saturation and value. Red sits at both ends of the wheel, so a red range such as 330° to 30° wraps around: keep hues ≥ 330 or ≤ 30.
Example
def to_grey(r, g, b):
return (299 * r + 587 * g + 114 * b + 500) // 1000
def to_hsv(r, g, b):
hi, lo = max(r, g, b), min(r, g, b)
d = hi - lo
if d == 0:
h = 0.0
elif hi == r:
h = 60 * (((g - b) / d) % 6)
elif hi == g:
h = 60 * ((b - r) / d + 2)
else:
h = 60 * ((r - g) / d + 4)
s = 0.0 if hi == 0 else d / hi
return h, s, hi / 255
colours = [
("red", (255, 0, 0)),
("green", (0, 255, 0)),
("blue", (0, 0, 255)),
("yellow", (255, 255, 0)),
("orange", (193, 107, 60)),
("grey", (128, 128, 128)),
]
for name, (r, g, b) in colours:
h, s, v = to_hsv(r, g, b)
print(f"{name:6} average {(r + g + b) // 3:3} luma {to_grey(r, g, b):3} hue {h:5.1f} sat {s:.2f} val {v:.2f}")
img = [[(255, 0, 0), (0, 255, 0)], [(0, 0, 255), (255, 255, 255)]]
red = [[p[0] for p in row] for row in img]
print("red channel:", red)
print("greyscale:", [[to_grey(*p) for p in row] for row in img])
bgr = [[(b, g, r) for (r, g, b) in row] for row in img]
print("first pixel as RGB", img[0][0], "and as BGR", bgr[0][0])Line by line
to_greyapplies the luma weights with whole numbers and rounds to the nearest value.to_hsvfinds the largest and smallest channel, then hue from the largest channel, saturation from the spread, and value from the maximum.- Red, green and blue all have the same average, 85, but very different luma: green 150, red 76, blue 29. That matches how bright they look.
- Yellow has hue 60.0, halfway between red and green.
- The orange (193, 107, 60) has hue 21.2, saturation 0.69 and value 0.76: a fairly strong, fairly bright orange.
- Grey has saturation 0, so its hue means nothing.
- The red channel of the 2 × 2 image is the first number of each pixel.
- Swapping the order to BGR turns (255, 0, 0) into (0, 0, 255): the same red pixel written the OpenCV way.
Expected result
red average 85 luma 76 hue 0.0 sat 1.00 val 1.00
green average 85 luma 150 hue 120.0 sat 1.00 val 1.00
blue average 85 luma 29 hue 240.0 sat 1.00 val 1.00
yellow average 170 luma 226 hue 60.0 sat 1.00 val 1.00
orange average 120 luma 127 hue 21.2 sat 0.69 val 0.76
grey average 128 luma 128 hue 0.0 sat 0.00 val 0.50
red channel: [[255, 0], [0, 255]]
greyscale: [[76, 150], [29, 255]]
first pixel as RGB (255, 0, 0) and as BGR (0, 0, 255)
In the real world
Video calls blur or replace backgrounds, fruit-sorting machines reject green tomatoes, and sports broadcasts track a coloured ball, all with colour spaces. Here is the OpenCV version (shown as text; OpenCV does not run on this site):
import cv2
img = cv2.imread("fruit.jpg") # BGR, shape (height, width, 3)
grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # luma weights 0.299, 0.587, 0.114
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, (5, 100, 100), (25, 255, 255)) # orange-ish pixels
For 8-bit images, OpenCV stores hue as degrees ÷ 2 (0 to 179) so it fits in a byte, and saturation and value as 0 to 255. Remember this when you copy hue ranges from other tools.
Common mistakes
- Mixing up RGB and BGR, which swaps red and blue.
- Averaging the channels for greyscale, which makes blue too bright and green too dark.
- Forgetting that red wraps around the hue wheel, so a range like 350° to 10° needs an "or".
- Selecting by hue alone. Greys and very dark pixels have meaningless hues; also require some saturation and value.
- Copying hue ranges between libraries without checking the scale (0 to 360, 0 to 179 or 0 to 1).
Best practices
- Convert to greyscale with luma weights unless you have a reason not to.
- Write the channel order next to every image variable, such as
img_bgr. - Pick colour thresholds in HSV, and test them on images taken in different light.
- Print a few pixels' HSV values before choosing a range.
Summary
A colour pixel is an (R, G, B) triple and a colour image is three channels; OpenCV stores them as BGR. Greyscale uses the luma weights 0.299, 0.587 and 0.114 because people see green as brightest. HSV splits a colour into hue, saturation and value, which makes it easy to select a colour even when the light changes; red needs a wrapping hue range.
Quick check
Key takeaways
- A colour pixel is three numbers; a colour image is three grids, one per channel.
- Greyscale uses luma weights because green looks brighter to people than red, and red brighter than blue.
- HSV separates "which colour" (hue) from "how bright" (value), which makes colour selection robust to lighting.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.