Digital Image Representation and Color Spaces
Module: Computer Vision | Difficulty: Premium
Image as a Matrix
A grayscale image is a 2D function sampled on a discrete grid:
A color image adds a channel dimension:
RGB to HSV Conversion
Given normalized RGB values :
CIE XYZ Color Space
The CIE 1931 XYZ color matching functions map spectral power distributions to perceptual color:
| Color Space | Channels | Perceptual | Gamut |
|---|---|---|---|
| RGB | 3 | No | Device-dependent |
| HSV | 3 | Partial | Same as RGB |
| CIE XYZ | 3 | Yes | Full human vision |
| CIELAB | 3 | Yes | Perceptually uniform |
import numpy as np
def rgb_to_hsv(rgb):
rgb = rgb / 255.0
r, g, b = rgb[..., 0], rgb[..., 1], rgb[..., 2]
max_c = np.max(rgb, axis=-1)
min_c = np.min(rgb, axis=-1)
diff = max_c - min_c
v = max_c
s = np.where(max_c == 0, 0, diff / max_c)
h = np.zeros_like(max_c)
mask = diff > 0
rm = mask & (max_c == r)
gm = mask & (max_c == g)
bm = mask & (max_c == b)
h[rm] = 60 * ((g[rm] - b[rm]) / diff[rm] % 6)
h[gm] = 60 * ((b[gm] - r[gm]) / diff[gm] + 2)
h[bm] = 60 * ((r[bm] - g[bm]) / diff[bm] + 4)
return np.stack([h / 360, s, v], axis=-1)
def compute_histogram(img, bins=256):
hist, _ = np.histogram(img.ravel(), bins=bins, range=(0, 256))
return hist / hist.sum()
Research Insight: The choice of color space profoundly affects downstream tasks. HSV and HSL decouple luminance from chrominance, making them robust to illumination changes in tasks like object tracking. CIELAB provides perceptual uniformity, which is critical for color constancy algorithms and image quality assessment. Modern deep networks typically learn their own color representations directly from raw RGB, but preprocessing with color space normalization can accelerate convergence.