Images as arrays
An image is a NumPy array of shape (height, width, 3) in BGR order. Reading, resizing, cropping and splitting channels are all array operations.
- Read an image and explain its shape, dtype and BGR channel order
- Resize by a target size or by a scale factor without mixing the two
- Crop and split channels with plain NumPy slicing
cv2.imread returns a NumPy array. For the introduction’s photo, image.shape is (563, 1000, 3): 563 rows, 1000 columns, three colour channels, each an 8-bit integer from 0 to 255. Two conventions catch everyone at least once:
- Rows first. Pixel
image[y, x]is rowy, columnx. Sizes passed to OpenCV functions go the other way:(width, height). - BGR, not RGB. OpenCV stores blue first. Showing an OpenCV image with Matplotlib, which expects RGB, swaps red and blue unless you convert with
cv2.cvtColor(image, cv2.COLOR_BGR2RGB).
imread also takes a flag: 0 loads grey (one channel), 1 colour, -1 the file unchanged, alpha channel included.
Splitting channels
The introduction builds a red-only, green-only and blue-only copy by zeroing the other two channels:
img_R = np.copy(image)
img_R[:, :, 0:2] = 0 # zero blue and green -> only red survives
img_G = np.copy(image)
img_G[:, :, ::2] = 0 # zero channels 0 and 2 (blue and red)
img_B = np.copy(image)
img_B[:, :, 1] = 0
img_B[:, :, 2] = 0
All three are correct for BGR order. The np.copy matters: slicing a NumPy array returns a view, so without it the first assignment would blank the original.
Resizing: size or scale, not both
cv2.resize takes either an output size dsize = (width, height) or scale factors fx, fy — and if dsize is given, the factors are ignored. The introduction mixes the two:
height, width, _ = image.shape
resized_image = cv2.resize(image, (height, width), fx=0.5, fy=0.5)
Here dsize = (563, 1000) is read as width 563 and height 1000, so the (563, 1000, 3) photo comes back as (1000, 563, 3): transposed in shape and stretched, not halved. Either form on its own does what was meant:
small = cv2.resize(image, None, fx=0.5, fy=0.5) # (282, 500, 3)
small = cv2.resize(image, (width // 2, height // 2)) # same
03_image_resize.py in the other repository shows the right patterns, including the interpolation choice: INTER_AREA for shrinking (it averages), INTER_LINEAR or INTER_CUBIC for enlarging.
Cropping is slicing
There is no crop function; 04_image_cropping.py slices:
cropped_image = image[80:280, 150:330] # rows 80-279, columns 150-329
The same script tiles an image into 76 × 104 patches with two nested loops — exactly how patch-based networks are fed. It skips the partial tiles at the right and bottom edges: the break fires as soon as fewer than a full tile’s pixels remain, so the branches written to clip a partial last tile only ever see tiles that fit exactly.
Paths
The introduction reads '..\data\img.png', a Windows path in an ordinary string. In the Harris script of lesson 5 the same habit breaks: in '..\data\blox.jpg', \b is Python’s escape for a backspace character, so the file is never found. os.path.join("..", "data", "blox.jpg") or a raw string r'..\data\blox.jpg' avoids both the escape and the platform dependence. Similarly, 01_image_basics.py loads Lenna.png once and Leena.png twice — the second spelling returns None instead of an image, which imread signals silently. Checking if image is None right after every imread catches all of these.
Try it yourself. Load any photo and print image.shape, image.dtype and image[0, 0]. Then show it with Matplotlib with and without COLOR_BGR2RGB. Finally resize it with cv2.resize(image, (h, w)) and with fx=0.5, fy=0.5, and predict both output shapes before printing them.