Image Representation - iffatAGheyas/computer-vision-handbook GitHub Wiki
๐ผ๏ธ Image Representation: Grayscale, RGB, etc.
When we talk about image representation, we mean how an image is stored inside a computerโin terms of data structures, colour channels and pixel values. This representation directly affects how we process, analyse and enhance the image later.
1. ๐ Grayscale Images
A grayscale image is represented as a 2D matrix of light intensity values:
- 0 = black
- 255 = white
- Intermediate values = shades of grey
Example (3ร3 matrix):
[ 0 127 255 ]
[ 64 128 192 ]
[ 30 60 90 ]
๐ก Use case: Faster processing when colour information isnโt required (e.g. edge detection).
2. ๐ RGB Images (Colour Images)
A colour image in the RGB model uses three channels:
- R = Red
- G = Green
- B = Blue
Each pixel is represented by three values (one per channel) and stored as a 3D array:
Height ร Width ร 3
Example (single pixel):
R: 120
G: 60
B: 30
โ This produces a brown-orange colour.
๐ฅ๏ธ Visual Layout
Conceptually, an RGB image is like stacking three grayscale images (one per channel) on top of each other.
๐ NumPy / OpenCV Shape
In Python/OpenCV, you can inspect the shape of loaded images:
import cv2
img_gray = cv2.imread("bird.jpg", cv2.IMREAD_GRAYSCALE)
img_rgb = cv2.imread("bird.jpg", cv2.IMREAD_COLOR)
print(img_gray.shape) # e.g. (300, 400)
print(img_rgb.shape) # e.g. (300, 400, 3)
3. ๐งฐ Other Formats (Preview Only)
Covered in the next topic on colour spaces.
Format | Description | Use Case |
---|---|---|
HSV | Hue, Saturation, Value | Colour filtering, tracking |
YCrCb | Luma (Y) and chroma (Cr, Cb) channels | Video compression |
Lab | Lightness and opponent-colour axes | Colour difference metrics |
๐ Summary
Image Type | Dimensions | Values | Common Use |
---|---|---|---|
Grayscale | H ร W | 0โ255 | Simpler, faster processing |
RGB | H ร W ร 3 | 3 channels | Full-colour analysis |
HSV/YCrCb | H ร W ร 3 | Converted | Colour filtering, compression |