Definition
To a computer, a photo of a sunset is not orange and beautiful. It is a grid of about two million tiny boxes, each holding three numbers. Everything in computer vision starts with understanding those numbers.
A digital image is a grid of pixels. In a grayscale image each pixel holds one brightness value, usually from 0 (black) to 255 (white). A colour image has three channels, red, green and blue, so it is stored as an array of shape height × width × 3. A colour space is a way of describing colour with numbers: RGB mixes light, HSV separates hue, saturation and value (brightness), and grayscale keeps only brightness.
In simple words
Look very closely at a phone screen with a magnifying glass and you will see tiny red, green and blue dots. Every colour you see is made by mixing different strengths of these three lights. A photo file simply records the strength of each dot.
Overview
- A digital image is a grid of pixels; each pixel stores numbers describing its colour or brightness.
- Greyscale images have one channel; colour images usually have three (red, green, blue).
- 8-bit channels store values from 0 (dark) to 255 (bright), so one RGB pixel can show about 16.7 million colours.
- An image array has shape (height, width, channels), such as (1080, 1920, 3) for full HD.
- Resolution is the number of pixels; more pixels show more detail but need more memory.
- Choosing the right colour space makes tasks such as skin detection or ripeness checks easier.
Why it matters
Every vision task, from reading a QR code to detecting a tumour, begins with these arrays. Choosing the right colour space can make a problem easy: detecting ripe tomatoes by hue is far simpler than by raw RGB values, which change with lighting.
Key terms
- Pixel
- The smallest element of an image, holding one value per channel.
- Resolution
- The number of pixels, written width × height, such as 1920 × 1080.
- Channel
- One layer of an image, such as the red, green or blue values.
- Bit depth
- How many bits store each value; 8 bits give 256 levels from 0 to 255.
- RGB and HSV
- RGB mixes red, green and blue light; HSV describes hue, saturation and brightness separately.
- Grayscale
- A single-channel image holding only brightness.
Types
Images can be stored in several colour spaces. Each type describes colour differently and suits different tasks.
1. Greyscale (single channel)
One brightness value per pixel, from black to white.
- How it works: Colour images are converted with a weighted sum such as 0.299R + 0.587G + 0.114B, reflecting the eye's sensitivity to green.
- Example: X-rays, scanned documents and the 8 × 8 digit images used in this course.
- When to use it: Use it when colour does not matter, to cut memory and computation by two-thirds and simplify edge detection.
2. RGB (and OpenCV's BGR)
Three channels for red, green and blue light, mixed additively.
- How it works: Each channel holds 0–255; (255, 255, 255) is white and (255, 0, 0) pure red; OpenCV loads images in BGR order, which must be swapped for display.
- Example: Phone cameras, screens and almost every photo file.
- When to use it: Use it as the default for storing and displaying images and as input to most CNNs.
3. HSV and HSL
Colour described by hue (which colour), saturation (how vivid) and value or lightness (how bright).
- How it works: Separating hue from brightness means a colour can be picked out even when lighting changes.
- Example: Detecting ripe red tomatoes by keeping pixels with hue near red and high saturation.
- When to use it: Use it for colour-based segmentation and filtering under varying light.
4. Lab and YCrCb
Colour spaces that separate lightness from colour information.
- How it works: Lab is designed so that equal distances look like equal colour differences to people; YCrCb splits brightness (Y) from two colour-difference channels, as in JPEG compression.
- Example: Measuring colour differences between fabric batches in Lab; skin detection in YCrCb.
- When to use it: Use them for accurate colour comparison, compression and some detection tasks.
5. Multi-channel and special images
Images with extra or different channels: RGBA, depth, thermal and multispectral.
- How it works: RGBA adds transparency; depth cameras store distance per pixel; satellites capture near-infrared and other bands invisible to people.
- Example: Satellite images of Indian farmland where the near-infrared band shows crop health (NDVI).
- When to use it: Use them in agriculture, robotics, medicine and remote sensing, where extra channels add information.
Explained in detail
Images as arrays
A 1920 × 1080 photo has 2,073,600 pixels. With three 8-bit channels it needs 6,220,800 bytes, about 6 MB, before compression. In Python an image is a NumPy array: img.shape is (1080, 1920, 3), img[0, 0] is the top-left pixel and img[0, 0] = [255, 165, 0] makes it orange. Rows come first, then columns, so the coordinate (x, y) on screen is img[y, x] in the array. JPEG files shrink the data by about ten times by discarding detail the eye barely notices, while PNG compresses without losing anything.
Colour spaces
| Colour space | Channels | Good for |
|---|---|---|
| RGB | Red, green, blue | Displays, cameras, most deep-learning models |
| BGR | Blue, green, red | OpenCV's default order; mixing it up turns faces blue |
| Grayscale | Brightness only | Edge detection, document scanning, faster processing |
| HSV | Hue, saturation, value | Picking objects by colour despite shadows |
| YCbCr / LAB | Brightness plus two colour channels | Compression (JPEG), skin detection, colour correction |
Grayscale is not a simple average. The eye is most sensitive to green and least to blue, so the standard formula is gray = 0.299 R + 0.587 G + 0.114 B. In HSV, hue is an angle around the colour wheel, about 0° for red, 120° for green and 240° for blue. Because shadows change mostly value and not hue, a rule such as "hue between 0° and 15°, saturation above 0.5" finds a red tomato in sun and shade, whereas an RGB rule would break.
- Neural networks usually expect pixel values scaled to 0 to 1, or standardised per channel.
- Medical scans and satellite images may have 12 or 16 bits per value, or many more channels, such as infrared bands.
- Video is a sequence of frames, typically 24 to 60 images per second.
Resolution and bit depth trade quality against size and speed. Most image classifiers shrink photos to about 224 × 224 pixels before processing, because fine detail matters less than overall shape and the smaller input makes training far faster. Tasks that depend on small details, such as reading number plates or finding hairline cracks in metal, need higher resolution or crops of the important region. When you design a vision system, decide early how large the images must be for the smallest thing you need to see, and collect training images at that size or larger.
How it works
- Load the image as an array and check its shape and data type.
- Confirm the channel order: RGB or BGR.
- Convert to the colour space that suits the task: grayscale for edges, HSV for colour picking.
- Scale values, for example dividing by 255, before feeding a model.
In code
import colorsys
import numpy as np
# A tiny 2 x 3 colour image: rows, columns, channels (R, G, B)
img = np.array([
[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
[[255, 165, 0], [128, 128, 128], [255, 255, 255]],
], dtype=np.uint8)
print("Shape:", img.shape, " dtype:", img.dtype)
print("Top-left pixel (row 0, col 0):", img[0, 0])
print("Red channel:\n", img[:, :, 0])
gray = (0.299 * img[:, :, 0] + 0.587 * img[:, :, 1] + 0.114 * img[:, :, 2]).round().astype(np.uint8)
print("Grayscale:\n", gray)
for name, (r, g, b) in [("orange", img[1, 0]), ("dark orange (in shade)", img[1, 0] // 2)]:
h, s, v = colorsys.rgb_to_hsv(r / 255, g / 255, b / 255)
print(f"{name:<23} RGB={tuple(int(c) for c in (r, g, b))} hue={h * 360:.0f} deg sat={s:.2f} value={v:.2f}")
print("Bytes for one uncompressed 1920x1080 colour photo:", 1920 * 1080 * 3)Output
Shape: (2, 3, 3) dtype: uint8 Top-left pixel (row 0, col 0): [255 0 0] Red channel: [[255 0 0] [255 128 255]] Grayscale: [[ 76 150 29] [173 128 255]] orange RGB=(255, 165, 0) hue=39 deg sat=1.00 value=1.00 dark orange (in shade) RGB=(127, 82, 0) hue=39 deg sat=1.00 value=0.50 Bytes for one uncompressed 1920x1080 colour photo: 6220800
Try it yourself
Sorting tomatoes by colour
A farmer's cooperative builds a camera system to separate ripe and unripe tomatoes on a conveyor belt.
Match the value to its meaning
What does each pixel or shape describe?
- [0, 0, 0]
- [255, 255, 255]
- (480, 640, 3)
- (480, 640)
Common mistakes
- Mixing up RGB and BGR channel order.
- Treating img[x, y] as column then row; arrays are indexed row first.
- Averaging channels equally for grayscale instead of using the weighted formula.
Check yourself
Summary
- An image is an array of pixels: height × width × channels.
- RGB, BGR, grayscale and HSV describe colour differently; pick the one that fits the task.
- Check shape, type and range, and scale values before modelling.
Finished reading?
Sources: Richard Szeliski, Computer Vision: Algorithms and Applications (2nd ed.) · OpenCV documentation and tutorials