Colour spaces, HSV and the module check
A red ball in sunlight and the same ball in shadow have very different RGB values. How can a robot say 'red' for both?
About 40 min · 3 goals · Checked by automated tests
1.Why should I care?
Colour is one of the fastest cues a robot has: red parts, green lights, a yellow ball. But brightness changes every channel at once. Separating 'what colour' from 'how bright' makes colour thresholds survive a passing cloud.
- ◇ Explain why raw RGB values change with lighting and why hue is more stable.
- ◇ Convert simple RGB colours to hue, saturation and value, and threshold on hue.
- ◇ Review weeks 22–24: sampling, aliasing, filtering, images and thresholds.
2.Discover
In the Code Lab, use Python's colorsys.rgb_to_hsv on the two red readings from the prediction, plus a dim orange (120, 60, 0) and a grey (90, 90, 90). Print hue in degrees, saturation and value for each. Then write is_red(r, g, b) that works for both reds and rejects the others.
Can't use the interactive? Read a description instead
Code Lab task, described: RGB (200, 40, 40) and (100, 20, 20) both have hue 0° and saturation 0.8, with value 0.78 and 0.39. Orange (120, 60, 0) has hue 30°; grey (90, 90, 90) has saturation 0, so its hue is undefined. A rule 'hue under 15° or over 345°, saturation above 0.4, value above 0.15' accepts both reds and rejects the others.
3.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
On a 0–255 scale, what is the value V (as a number 0–255) of RGB (60, 180, 90)?
4.Apply it
Extend is_red to a function that returns a mask for a whole image (as nested lists). Test it on a picture of something red taken in bright light and in shade. Report the share of object pixels detected in each and the settings you chose.
5.Understand
Why RGB is fragile
Each RGB channel measures how much light of one colour reaches the pixel, so dimming the light scales all three together. A rule like 'red if R > 150' fails in shadow. Ratios between channels survive dimming far better than the raw values do.
Hue, saturation, value
HSV re-describes a colour as hue (which colour, an angle round a colour wheel: red near 0°, green 120°, blue 240°), saturation (how vivid versus grey) and value (how bright: the largest of R, G, B). A colour threshold on hue, with a minimum saturation, ignores most brightness changes.
Its weak spots
Hue wraps around: red sits at both 0° and 360°, so 'red' is hue < 15° or > 345°. For greys and very dark pixels saturation is near zero and hue is meaningless noise, so always require a minimum saturation and value. Coloured lighting still shifts hue.
Module check: weeks 22–24
You should be able to: state the Nyquist rate and compute an alias; choose a sample rate with a filter in front; compute moving-average, EMA and median outputs and explain the noise–lag trade-off; size an image in bytes; and choose and defend a threshold. The practice mixes them.
Explain it another way
A red jumper is still red in a dim room: your brain judges colour from the mix, not the brightness. HSV lets a computer do something similar.
V = max(R, G, B), S = (V − min)/V, and hue is computed from which channel is largest and the differences between the others, giving an angle in [0°, 360°).
Scaling (R, G, B) by any k > 0 leaves H and S unchanged and multiplies V by k, which is exactly why hue thresholds are robust to uniform dimming.
The sorting machine's colour sensors are simplified 'how red' readings; real recycling sorters often use hue-like features or even near-infrared spectra to tell plastics apart.
Worked example 1
Find H, S and V for RGB (200, 40, 40) on a 0–255 scale.
V = 200/255 ≈ 0.78. min = 40, so S = (200 − 40)/200 = 0.8. Red is largest and G = B, so hue = 0°. In shade (100, 20, 20): V halves to 0.39, S stays 0.8, H stays 0°.
Common mix-ups
- HSV makes colour detection immune to lighting.
✓ It handles brightness changes well, but coloured light, shadows from coloured objects and camera white balance still shift hue. - Hue thresholds work on any pixel.
✓ Hue is only meaningful when saturation and value are high enough.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2
What is the saturation S of RGB (60, 180, 90), as a decimal?
Practice 3
Which rule correctly selects reds using hue in degrees?
Practice 4 · a fresh version every time
A 62.5 Hz vibration is sampled at 50 Hz with no anti-aliasing filter. At what frequency does it appear in the samples, in Hz?
Practice 5 · a fresh version every time
A filter keeps the mean of the last k = 3 readings. The readings so far, oldest first, are: 23, 11, 29, 27, 24, 24, 15, 13. What does the filter output now?
Practice 6
Why require a minimum saturation in a hue-based colour detector?
7.Predict
A red part reads RGB (200, 40, 40) in light and (100, 20, 20) in shade. What stays the same?
Guess first. Being wrong here is how the surprise works.
8.Reflect
Concepts: Thresholding and simple classification · Images as arrays of numbers