Foundations to Frontier
Tests
G. Computer Vision and Remote Sensing › G1 Image Fundamentals

Image Representation and Colour Spaces

A digital image is an array of samples: height × width × channels, stored as integers or floats, with a transfer function that says how stored numbers relate to light. This page covers pixels, bit depth and data types, sRGB gamma encoding versus linear light, the colour spaces used in practice (RGB, HSV, YCbCr, CIELAB, OKLab), chroma subsampling, Bayer sensors and raw data, and multispectral images such as Sentinel-2 with indices like NDVI. In the exercises you implement the sRGB transfer functions and the YCbCr and HSV conversions. You then measure three common mistakes on a synthetic scene. Averaging encoded values when resizing turns 1-pixel black and white stripes into grey 127.5 instead of the correct 187.5. With the same data budget, subsampling chroma in YCbCr gave 31.1 dB PSNR against 26.0 dB for subsampling red and blue, but the advantage vanished on a control image without fine luminance detail. Storing linear light in 8 bits gave lightness errors up to 1.77 L* units in the shadows, against 0.25 for sRGB encoding.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Numbers that stand for light

A photo on a computer is a grid of tiny squares, the pixels, and each pixel holds a few numbers: usually how much red, green and blue light it has. A satellite image can hold many more numbers per pixel, one per wavelength band, including infrared that our eyes cannot see. The catch is that those numbers are not simply proportional to light: they are squeezed so that the dark tones, which our eyes notice most, get more of the available values. Different ‘colour spaces’ rearrange the same information to separate brightness from colour or to match how we see. The demo converts one colour between spaces and shows a surprising grey.

Image lab
linear RGB 0.578, 0.188, 0.021
luminance Y 0.259 · L* 57.9
Y′CbCr (JPEG) 134.8, 74.5, 174.5
HSV 30°, 0.80, 0.78
gamma check: stripes · sRGB 128 · sRGB 188
Perceived lightness L* follows linear luminance through a cube root, which is why 8-bit images store gamma-encoded values: equal code steps look roughly equally spaced. The stripes average to 50% linear light, which encodes to sRGB 188, not 128.
Why engineers care

Earth-observation pipelines mix 13-band Sentinel-2 reflectance, 16-bit raw camera frames and 8-bit sRGB previews; feeding the wrong encoding to a model or averaging in the wrong space silently biases every result.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A colour image is usually stored as…
  2. 2. 8-bit sRGB values are…
  3. 3. Satellite images often have…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Arrays: an image is an H × W × C array (HWC, as in NumPy, OpenCV, Pillow) or C × H × W (CHW, as in PyTorch). OpenCV loads channels in BGR order. Coordinates are (row, column) with the origin at the top left.
  • Data types: uint8 (0–255) for display images, uint16 for raw sensors and satellite products (Sentinel-2 L2A stores reflectance × 10000), float32 in [0, 1] or standardised for models. Integer overflow and silent clipping are common bugs.
  • Transfer function: sRGB (IEC 61966-2-1, 1999) encodes linear light l as about l^(1/2.4) with a linear toe, so 8-bit codes are spread roughly evenly in perceived lightness. Physical operations (blending, resizing, blurring, exposure) belong in linear light; HDR video uses PQ (SMPTE ST 2084) and HLG.
  • RGB primaries: sRGB and BT.709 share primaries; Display P3 and BT.2020 cover wider gamuts. Luminance from linear RGB is Y = 0.2126 R + 0.7152 G + 0.0722 B.
  • YCbCr: luma plus two colour differences (BT.601 for JPEG, BT.709 for HD video). Eyes resolve colour more coarsely than brightness, so JPEG and video keep chroma at quarter resolution (4:2:0).
  • HSV and HSL: hue, saturation, value; convenient for thresholds and colour jitter, but hue is undefined for greys and unstable at low saturation.
  • CIELAB and OKLab: L* approximates perceived lightness (CIE 1976); distances approximate perceived colour difference (ΔE). OKLab (Ottosson, 2020) is a better-behaved modern alternative used in CSS Color 4.
  • Sensors: a Bayer colour filter array (Bayer, 1976) records one colour per photosite; demosaicing interpolates the rest. Raw files are linear, 12–14 bits, before white balance and colour correction.
  • Multispectral and hyperspectral: Sentinel-2 has 13 bands at 10, 20 and 60 m; Landsat 8/9 has 11; hyperspectral sensors (EnMAP, PRISMA) have hundreds. Indices such as NDVI = (NIR − Red)/(NIR + Red) combine bands. Large rasters are stored as Cloud-Optimized GeoTIFF or Zarr.
📝 Quick check (Level 1)
0/3 answered
  1. 1. PyTorch expects image tensors in…
  2. 2. Blending and resizing should happen in…
  3. 3. JPEG stores colour at reduced resolution using…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Converting and storing images correctly

Exercise 1.

  • sRGB to linear and back.
  • RGB to YCbCr (JPEG, BT.601 full range).
  • RGB to HSV.

Practice.

  • Decide once where linear light is needed (resizing, blending, physics-based models) and convert explicitly; keep track of the dtype and range at every step.
  • Check channel order (OpenCV BGR versus RGB) and layout (HWC versus CHW) when moving between libraries.
  • For pretrained models, reproduce the training preprocessing exactly (resize method, range, mean and standard deviation).
  • For satellite data, keep physical units (reflectance, radiance), use per-band statistics for normalisation and do not drop bands just to fit an RGB network.
  • Use scikit-image, OpenCV, Kornia or colour-science for conversions; rasterio and xarray for geospatial rasters.
import numpy as np, cv2, rasterio
bgr = cv2.imread("frame.png", cv2.IMREAD_UNCHANGED)        # HWC, uint8, BGR order
rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB).astype(np.float32) / 255
ycc = cv2.cvtColor(bgr, cv2.COLOR_BGR2YCrCb)                # note OpenCV's Y, Cr, Cb order
with rasterio.open("S2_L2A_B04_B08.tif") as src:            # Sentinel-2 red and NIR, uint16
    red, nir = src.read([1, 2]).astype(np.float32) / 10000   # scaled reflectance
ndvi = (nir - red) / (nir + red + 1e-6)
🐍 Exercise 1: transfer functions and colour spaces
Implement the sRGB transfer functions and the RGB to YCbCr and HSV conversions.
📝 Quick check (Level 2)
0/3 answered
  1. 1. OpenCV reads colour images in…
  2. 2. Sentinel-2 L2A reflectance is stored as…
  3. 3. For a pretrained model, preprocessing must…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why gamma encoding. Perceived lightness follows roughly a cube root of luminance: L* = 116 (Y/Y_n)^(1/3) − 16 above a small linear toe. A code that is uniform in linear light wastes most of its values on bright tones and leaves too few in the shadows. The sRGB curve (exponent 2.4 with an offset, about 2.2 overall) spreads codes nearly uniformly in L*, so 8 bits suffice for display.

Linear operations need linear light. Resizing, blurring and blending are weighted averages of light. Averaging encoded values computes the encoding of a different quantity, so the result is too dark wherever bright and dark pixels mix: a 1-pixel black and white pattern averages to 50% light, which encodes to sRGB 188, but naive averaging gives 128.

Luma and chroma. YCbCr is a fixed linear transform of encoded RGB. Natural images have most fine detail in brightness (shading, texture) and smoother colour (material), and the human contrast sensitivity for colour differences falls off at lower spatial frequencies. Subsampling chroma therefore loses little; subsampling a primary such as R or B removes luminance detail along with colour.

Spectral images. Each band integrates scene radiance over a spectral response. Ratios such as NDVI cancel multiplicative illumination changes to first order, which is why indices are robust to sun angle and slope.

📝 Quick check (Level 3)
0/3 answered
  1. 1. 8-bit sRGB spreads codes roughly uniformly in…
  2. 2. Naive averaging of a black and white stripe pattern gives…
  3. 3. Ratio indices such as NDVI cancel…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How much do gamma mistakes, chroma subsampling and 8-bit linear storage cost on a realistic image?

History.

  • B. Bayer, “Color imaging array” (US patent 3971065, 1976).
  • CIE, “Colorimetry”, defining the CIELAB colour space (1976).
  • IEC 61966-2-1, “Default RGB colour space: sRGB” (1999); C. Poynton, Digital Video and HD (2nd ed., 2012).
  • B. Ottosson, “A perceptual color space for image processing” (OKLab, 2020).

Exercise 2. A 128 × 128 synthetic scene in linear light: 14 Voronoi regions of random colour (reflectance) multiplied by a grey shading texture with a 1/f spectrum (fine luminance detail), stored as 8-bit sRGB.

  • Resizing: 2× box downsampling by averaging encoded values versus averaging in linear light, on the scene and on 1-pixel black and white stripes.
  • Chroma subsampling: both keep 1.5 samples per pixel: full Y plus 4:2:0 Cb and Cr, or full G plus 4:2:0 R and B. Control: the same scene without the shading texture.
  • Quantisation: a linear ramp of luminance coded in 8 bits linear, 8 bits sRGB or 12 bits linear; error in CIELAB L*.
test result
stripes, naive average 127.5
stripes, average in linear light 187.5
scene, naive vs linear resize mean 0.94, 99th percentile 7.3 levels
4:2:0 YCbCr vs R, B subsampling 31.1 vs 26.0 dB
control without texture 31.8 vs 33.3 dB
max L* error: 8-bit linear, 8-bit sRGB, 12-bit linear 1.77, 0.25, 0.11
codes below L* = 50: 8-bit linear vs sRGB 47 vs 119 of 256
  • Gamma errors are small on average (under 1 level of 255) but reach 7–9 levels at high-contrast edges, and fine patterns lose a third of their brightness.
  • Chroma subsampling beat subsampling R and B by about 5 dB on the textured scene (seeds 1 and 2: 30.9 vs 26.0 and 32.9 vs 26.8 dB).
  • Null result in the control: without luminance texture, YCbCr was no better: the differences were −1.5, +0.2 and +2.4 dB on seeds 0, 1 and 2. The advantage comes from where the detail lives, not from YCbCr itself.
  • 8-bit linear storage puts only 47 of 256 codes in the darker half of perceived lightness, with errors of 1.77 L* units (visible banding); sRGB coding keeps errors at 0.25, and 12-bit linear (raw) at 0.11.
  • Caveat: synthetic scene; PSNR is not a perceptual metric, and real codecs filter chroma before subsampling.
🐍 Exercise 2 (research): how much do gamma mistakes, chroma subsampling and 8-bit linear storage cost on a realistic image?
Measure the error of resizing in sRGB codes, compare YCbCr 4:2:0 with R and B subsampling, and compute lightness errors of 8-bit linear and sRGB storage.

Image representation in AI, robotics and aerospace

  • Earth observation: Sentinel-2 and Landsat products are delivered as scaled reflectance in uint16; foundation models such as Prithvi (NASA–IBM, 2023) and SatMAE take many bands and need per-band normalisation, not ImageNet RGB statistics.
  • Robotics cameras: ROS image messages carry an explicit encoding string (rgb8, bgr8, mono16, bayer_rggb8); mismatched encodings are a classic source of silent failure.
  • Space imaging: science cameras keep linear 12–16-bit raw data for calibration (dark, flat field) and only apply gamma for previews.

References

  1. IEC 61966-2-1 (1999). “Multimedia systems and equipment: colour measurement and management, part 2-1: default RGB colour space, sRGB.”
  2. C. Poynton (2012). Digital Video and HD: Algorithms and Interfaces, 2nd ed. Morgan Kaufmann.
  3. ITU-R Recommendations BT.601 (1982) and BT.709 (1990), studio encoding parameters.
  4. B. Ottosson (2020). “A perceptual color space for image processing.” Blog post introducing OKLab.
  5. M. Drusch et al. (2012). “Sentinel-2: ESA’s optical high-resolution mission for GMES operational services.” Remote Sensing of Environment.
  6. C. J. Tucker (1979). “Red and photographic infrared linear combinations for monitoring vegetation.” Remote Sensing of Environment.