Images as Arrays
What you’ll learn
- How a grayscale or color image maps to a NumPy array
- What
shape,dtypeand channel order mean in practice - The two pitfalls that bite everyone: BGR order and
uint8overflow
Intuition
A digital image is a grid of samples. Each sample, or pixel, stores a brightness value. A grayscale image is a 2-D array of shape . A color image adds a third axis for channels, giving shape .
The first index is the row (, top to bottom) and the second is the column (, left to right). That is the opposite of the order you know from math class.
Code
import cv2
import numpy as np
img = cv2.imread("cat.jpg") # shape (H, W, 3), dtype uint8, channels in B, G, R order
print(img.shape, img.dtype)
rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # convert before plotting with matplotlib
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # shape (H, W)
y, x = 120, 200
print(gray[y, x]) # row first, then column
Exercises
- Load an image and print the mean value of each color channel. Which one is largest?
- Flip an image upside down using only array slicing.