Computer Vision Lab Lessons Colour spaces

Colour spaces

Color spaces: RGB, grayscale, HSV

Why we convert between RGB, greyscale and HSV, and how that affects processing.

Beginner20 minImage basicsImage processing

This content was prepared automatically from documented sources and its human scientific review is not yet complete. If you find an error, let us know.

Introduction

Colour can be represented in more than one way, and each representation has advantages for certain tasks.

Learning objectives

  • Understand RGB and conversion to greyscale.
  • Get to know HSV and separating colour from lighting.
  • Choose the right space for the task.

Prerequisites

The digital image basics lesson.

Scientific explanation

RGB represents colour by mixing red, green and blue; it suits display but mixes colour with lighting. Greyscale reduces the channels to a single intensity with weights that reflect the eye's sensitivity. HSV separates hue, saturation and value, which makes it easy to select a given colour regardless of the lighting intensity.

In-depth explanation

Why do we need more than RGB?

RGB suits display because it matches the screen's components, but it mixes colour and lighting: a shadow on a red object changes all three channels together. Other colour spaces separate lighting from colour (hue and saturation), which makes tasks easier, such as tracking an object by its colour despite changing light, or improving contrast without distorting colours.

The main spaces

SpaceComponentsWhen we use it
RGBRed, green, blueDisplay, and inputs to pretrained networks
HSV / HSLHue (angle 0–360°), saturation, valueColour-based segmentation and tracking coloured objects
YCrCb / YUVLuma Y and two chroma componentsImage and video compression (JPEG), and histogram equalisation on Y only
CIELABL* lightness, a* green–red, b* blue–yellowMeasuring perceptual colour differences, and SLIC

Mathematical formulation

From RGB (normalised to [0,1]) to HSV:

V=\max(R,G,B),\quad S=\frac{V-\min(R,G,B)}{V},\quad H = 60^\circ\times\begin{cases}\frac{G-B}{V-m} \bmod 6 & V=R\\ \frac{B-R}{V-m}+2 & V=G\\ \frac{R-G}{V-m}+4 & V=B\end{cases}

where m=\min(R,G,B). Hue is circular: 359° is very close to 0°, so red lies at both ends of the range, which requires two ranges when segmenting red.

In CIELAB, the Euclidean distance approximates the perceptual difference: \Delta E^*_{ab} = \sqrt{(\Delta L^*)^2+(\Delta a^*)^2+(\Delta b^*)^2}; a difference of about 2.3 is considered an approximate threshold of what the eye can tell apart (later formulas such as CIEDE2000 refine it).

Practical notes

  • In OpenCV, for uint8 images the hue is stored in the range 0–179 (the angle divided by 2) to fit in one byte.
  • Hue is unstable when saturation or value is close to zero (greys and black); ignore it there.
  • Do not convert input images to HSV before a model trained on RGB.
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# red needs two ranges because hue is circular
m1 = cv2.inRange(hsv, (0, 80, 50), (10, 255, 255))
m2 = cv2.inRange(hsv, (170, 80, 50), (179, 255, 255))
red = m1 | m2

Review questions

  1. Why is YCrCb used in JPEG compression?
  2. Why is it hard to segment red in HSV with a single range?
  3. Which space is best for measuring "how different two colours are" as the eye sees them?

Mathematical equations

Y = 0.299R + 0.587G + 0.114B (the ITU-R BT.601 standard used in OpenCV).

Practical example

To track a red ball under changing lighting, a threshold on the HSV hue channel is more stable than a threshold on the R channel.

Implementation steps

  1. Load a colour image.
  2. Convert it to greyscale and compare before/after with the slider.
  3. Notice how different colours sometimes turn into similar intensities.

Expected results

Two different colours (red and dark green, for example) may end up as the same grey level.

Common mistakes

Assuming greyscale is the average of the three channels, or using RGB thresholds for colour-dependent tasks under changing lighting.

Practical applications

Sorting products by colour, skin detection, analysing agricultural images.

References

  1. R. Szeliski (2022).Computer Vision: Algorithms and Applications (2nd ed.). Springer, Texts in Computer Science. doi:10.1007/978-3-030-34372-9
  2. OpenCV (2024).OpenCV documentation: Image Processing (imgproc module). docs.opencv.org. docs.opencv.org

Related experiments

Related algorithms

Try in image processing