Skip to the lesson
FixTheRobot Explore Robotics / Perception / P2 Image Processing for Robotics / P2-T2

Perception · Theory · P2

P2-T2 — RGB, Grayscale and HSV

The same pixel, described three ways. Pick the description in which your question is easy.

  • Lesson 2 of 6
  • Module 2: Image Processing for Robotics
  • Theory
  • About 9 minutes to read

A colour detector asks a simple question: is this pixel red? In RGB the answer depends on the brightness of the light as much as on the colour of the object, because the three numbers move together when the light changes. HSV separates which colour a pixel is (hue) from how pure it is (saturation) and how bright (value). Grayscale throws colour away altogether, which can be exactly what you want, or exactly what ruins the task. This lesson gives the conversions, a worked example with numbers and the traps.

Learning objectives

After this lesson, you should be able to:

  • Describe a colour in RGB, grayscale and HSV and convert between them with the standard formulas.
  • Explain why hue is more stable than the RGB channels when the illumination changes.
  • Define a hue range for a colour, including the wrap-around of red.
  • Name the conditions where HSV and grayscale mislead.

The idea: describe colour the way the question is asked

Ask a person “is this red?” and they will not compare three numbers. They think of a colour, then how vivid it is, then how light or dark. HSV is built on the same split. Hue is the position on the colour circle: red, orange, yellow, green and so on. Saturation says how far the colour is from grey: a pale pink and a deep red have the same hue, different saturation. Value is the brightness: a dark red and a bright red differ in value.

The hue circle and the red rangeA circle of hues. Red is at 0 degrees on the right, yellow at 60, green at 120, cyan at 180, blue at 240 and magenta at 300. A thick arc from minus 20 to plus 20 degrees around red is labelled "red: 340 to 20, wraps through 0". On the right, a short list says saturation is distance from grey and value is brightness.0°60°120°180°240°300° red = 340° … 20°the range wraps through 0° H hue: which colour (angle)S saturation: distance from greyV value: brightness dim the light by half:R, G, B all halveH and S stay the same, V halves
The hue circle, with the range that counts as red. Dimming the light scales the RGB channels together and leaves the hue unchanged.

Why this matters to a robot

The camera does not measure the colour of an object. It measures the colour of the object times the colour and strength of the light on it. Dim the light by half, and a red block that was (210, 40, 40) becomes (105, 20, 20): all three channels halve. A threshold such as “red channel above 150” has stopped working, although the object has not changed. The hue is the same for both, because it depends on the ratios of the channels, which the light scaled equally.

Choose a representation in which the thing that varies (brightness) is separate from the thing you care about (colour).

Grayscale

A grayscale image keeps one number per pixel, the perceived brightness. Many algorithms (edges, corners, many feature detectors) work on grayscale because shape and texture matter more than colour and a single channel is three times cheaper. But discarding colour discards information: a red block and a green block with the same brightness become indistinguishable. Look at the last column of the table below.

The mathematics: the conversions

RGB to grayscale

The common conversion is a weighted sum that follows the eye’s greater sensitivity to green (the weights of ITU-R BT.601, used by many libraries):

luminance weights
Y=0.299 R+0.587 G+0.114 BY=0.299\,R+0.587\,G+0.114\,B
R, G, B
the red, green and blue values of the pixel, 0 to 255 for an 8-bit image.
Y
the gray level, in the same range. A plain average (R + G + B)/3 is also used and treats green as much as blue, which does not match how bright colours look.

RGB to HSV

Scale the channels to 0 to 1. Let M = max(R, G, B), m = min(R, G, B) and Δ = M − m. Then

RGB to HSV
V=MV=M
S=ΔMS=\frac{\Delta }{M}
(S=0 if M=0)(S=0\,\text{if}\,M=0)
H=60∘×(G−BΔ  mod  6)H=60^{\circ}\times (\frac{G-B}{\Delta \,\bmod \,6})
if M=R\text{if}\,M=R
H=60∘×(B−RΔ+2)H=60^{\circ}\times (\frac{B-R}{\Delta }+2)
if M=G\text{if}\,M=G
H=60∘×(R−GΔ+4)H=60^{\circ}\times (\frac{R-G}{\Delta }+4)
if M=B\text{if}\,M=B
H=0H=0
if Δ=0\text{if}\,\Delta =0
V
value: the largest channel. It is the brightness of the strongest colour component.
S
saturation: the fraction of the value that is colour, as opposed to grey. A grey pixel (R = G = B) has S = 0.
H
hue: the angle of the colour on the circle, in degrees: 0° red, 60° yellow, 120° green, 180° cyan, 240° blue, 300° magenta.
Δ
the spread of the channels. If Δ = 0 the pixel is grey and the hue has no meaning.

Scaling all three channels by a factor k (a change of light) multiplies M, m and Δ by k. So S = Δ/M and the ratios inside H are unchanged, and only V changes. That is the formal reason for hue’s robustness to a uniform change of illumination.

Hue is an angle

Hues 359° and 1° are 2° apart. A range for red must therefore wrap: “from 340° up through 360°/0° to 20°”. A test written as lo ≤ h ≤ hi works for green and blue (a range that does not cross 0°) and returns nothing for red. The circular distance between two hues is min(|a − b|, 360° − |a − b|).

Where HSV misleads

  • Low saturation: for a nearly grey pixel, a small amount of noise swings the hue anywhere on the circle. Always require a minimum saturation before trusting the hue.
  • Low value: for a very dark pixel the channels are small integers and the ratios are dominated by quantisation and noise. Require a minimum value too.
  • Non-white light: sodium or LED lamps and sunset light multiply the channels by different factors, so the hue of an object shifts. A colour cast cannot be removed by hue; white balance has to be done first.
  • Saturated pixels: if a channel is clipped at 255, the ratios are wrong, and bright highlights lose their hue.

A worked example: five pixels in three descriptions

Take the red block of the first lab, the same block in half the light, a yellow block, a green block, and a grey floor pixel.

Five pixels in RGB, HSV and grayscale
Pixel (R, G, B)HueSaturationValueGray Y
(210, 40, 40)0.0°0.810.8291
(105, 20, 20)0.0°0.810.4145
(225, 200, 40)51.9°0.820.88189
(40, 170, 70)133.8°0.760.67120
(120, 118, 116)30.0°0.030.47118

Compute the first row by hand. Scaled to 0 to 1: R = 0.824, G = B = 0.157. So M = 0.824, m = 0.157, Δ = 0.667. V = 0.82, S = 0.667/0.824 = 0.81. The maximum is R, so H = 60° × ((G − B)/Δ mod 6) = 60° × 0 = 0°.

Now halve the light (second row, (105, 20, 20)): M = 0.412, m = 0.078, Δ = 0.333, so S = 0.81 again and H = 0°; only V falls to 0.41. The RGB values changed by half and the hue and saturation did not move.

Three more observations from the table:

  • The yellow block has a hue near 53°, well separated from the red at 0° and the green near 134°. A hue range of ±20° around each is enough to tell them apart.
  • The grey floor pixel has a saturation near 0.03. Its hue is computed from a tiny spread of the channels and means nothing: this is why the saturation limit is needed.
  • In grayscale, the red block gives 91 and the grey floor 118, so they are not separated by a lot; with a different red and floor they could be equal. A grayscale detector cannot find a red block on a floor of the same brightness, while a hue test can.

A red range, written out

A pixel is red if its hue is within 20° of 0°, its saturation is at least 0.4 and its value at least 0.2. In code, with the wrap handled:

a red test that wraps
isred=(h≥340∘or h≤20∘) and s≥0.4 and v≥0.2\mathrm{is}_{\text{red}}=(h\ge 340^{\circ}\text{or}\,h\le 20^{\circ})\,\text{and}\,s\ge 0.4\,\text{and}\,v\ge 0.2

Lab P2-L1 asks you to write exactly this and to find out what happens when you write 340 ≤ h ≤ 20 instead.

Engineering implications and limitations

Calibrate thresholds on your own camera. Hue values depend on the sensor, the white balance and the lens. Collect images of the target under the lighting you expect, plot the hue, saturation and value histograms of the target pixels and of the background, and choose limits with a margin. Document the images they came from.

Fix white balance and exposure first. Auto white balance moves hues from frame to frame; auto exposure moves values. For a colour-coded task, lock both, or normalise (for example with a reference patch) before thresholding.

Know your channel order. OpenCV loads images as blue-green-red by default and has separate conversion codes for BGR to HSV. Using the RGB formula on a BGR image gives a hue mirrored around green. Hue in OpenCV’s 8-bit images is also stored as 0 to 179 (half degrees), which breaks thresholds copied from textbooks.

Use more than one cue. A colour test finds red things, not red blocks: a red shirt, a red sign and a red reflection pass it. Combine colour with size, shape, position or depth, and with tracking over time.

Pick grayscale deliberately. Use it when the task is about shape or texture and colour would only add variation, and convert with the luminance weights. Do not use it when colour is the signal.

Common misconceptions

“HSV is a different colour; it knows more than RGB.”

It is the same information in different coordinates. The advantage is that the coordinates separate colour from brightness, so a simple test can be written.

“Hue is always reliable.”

Hue is undefined for greys and unstable for very dark or nearly grey pixels. Without saturation and value limits, noise makes it jump.

“The average of R, G and B is the brightness.”

It is a brightness, not the perceptual one. The luminance weights give green the largest share, and the two give visibly different grey images, especially for saturated colours.

“A hue range is lo ≤ h ≤ hi.”

Not for red. The range crosses 0°/360°, and the test must be a union of two intervals.

Engineering takeaways

  1. RGB, grayscale and HSV describe the same pixel; the right one is the one in which your question is simple.
  2. Gray Y = 0.299 R + 0.587 G + 0.114 B; HSV is computed from the max, min and spread of the channels.
  3. A uniform change of illumination scales R, G and B together, so hue and saturation are unchanged and only value changes.
  4. Hue is an angle: red’s range wraps through 0°, and hue is unreliable at low saturation or low value.
  5. Fix white balance and exposure, calibrate thresholds on your own images, check the channel order, and combine colour with other cues.

References and further reading

The table was computed with the formulas on this page.

  • Smith, A. R. (1978). Color gamut transform pairs. SIGGRAPH ’78: the original definition of the HSV colour model.
  • ITU-R Recommendation BT.601: the luminance coefficients 0.299, 0.587 and 0.114.
  • Szeliski, R. (2022). Computer Vision: Algorithms and Applications, 2nd ed. Springer: colour spaces and white balance.
  • OpenCV documentation, Changing colorspaces: BGR channel order and the 0 to 179 hue range for 8-bit images.

Knowledge check

Three conceptual questions. Write an answer, then reveal the explanation. Your answers stay in your browser.

Question 1

A pixel is (200, 100, 100) in bright light. In shade it is (100, 50, 50). How do its hue, saturation and value change?

Explanation

Hue and saturation are unchanged: both pixels have M = R and the ratio structure is the same, so H = 0° and S = 0.5. Value halves, from about 0.78 to 0.39. That is exactly the case hue-based colour tests are designed for.

Question 2

You want hue values for “orange-red” between 350° and 15°. Write the test for a hue h, and say what happens if you write 350 ≤ h ≤ 15.

Explanation

Inside is h ≥ 350 or h ≤ 15. The single comparison 350 ≤ h ≤ 15 can never be true (no number is both at least 350 and at most 15), so it selects nothing.

Question 3

A detector finds red blocks well on bench images and finds red in nothing outdoors at dusk. What is the most likely cause, and what is the first thing to check?

Explanation

Under the warm, dim light at dusk the channels are scaled unevenly and the exposure is low: the hue shifts and saturation and value fall below the limits. First check the actual hue, saturation and value of the block in dusk images against the thresholds, and whether auto white balance or exposure changed. Locking or correcting those is the first remedy.

What’s next

Next: filtering. How to reduce noise in an image, what each filter keeps, and what it destroys.

Practice this lesson

Detect Colored Objects in an Image

Find coloured objects in an image with a colour-space threshold and locate their centres.

Next lesson

P2-T3 — Filtering and Noise Reduction

Apply and compare mean, Gaussian and median filters and state what each preserves.

Nothing below is required to finish this lesson.