Sub topic 2
A Frame Is a NumPy Array (and OpenCV Uses BGR)
This chapter assumes that you know basic Python loops and functions, can read NumPy array shapes and slices, and have installed opencv-python. The next chapters will use cv2.VideoCapture; here, the focus is the data that VideoCapture.read() returns.
The frame data model
A video frame is one still image. OpenCV represents that image as a NumPy array. For a normal color frame, the array has three dimensions:
(height, width, channels)For example, a frame with 720 rows and 1280 columns usually has shape (720, 1280, 3). The first index selects a row, the second selects a column, and the third selects a color channel.
Most camera and video frames use the uint8 data type. Each channel value is an integer from 0 through 255:
0means no intensity for that channel.255means full intensity.- A pixel is accessed as
frame[row, column]. - A channel is accessed as
frame[row, column, channel].
OpenCV stores color channels in BGR order: blue, green, red. This differs from the RGB order used by many image libraries and plotting tools.
| Item | Meaning |
|---|---|
| `frame.shape` | Height, width, and channel count |
| `frame.dtype` | Usually `uint8` for decoded video |
| `frame[10, 20]` | The BGR value of one pixel |
| `frame[:, :, 0]` | The blue channel |
| `frame[:, :, 2]` | The red channel |
The value at frame[10, 20] is an array such as [255, 0, 0]. In OpenCV, that means blue, not red.
Inspecting and changing pixels
The following program creates a small BGR image, inspects its array representation, and saves it. It does not need an input file.
import cv2
import numpy as np
frame = np.zeros((120, 200, 3), dtype=np.uint8)
# OpenCV drawing functions also interpret colors as BGR.
cv2.rectangle(frame, (20, 20), (180, 100), (0, 0, 255), thickness=-1)
print("shape:", frame.shape)
print("dtype:", frame.dtype)
print("pixel at the center:", frame[60, 100])
cv2.imwrite("red_rectangle.png", frame)The rectangle color (0, 0, 255) is red because the values are ordered blue, green, red. cv2.imwrite also expects the OpenCV BGR convention, so the saved image appears red.
A frame is mutable. You can change a region with a slice:
frame[0:20, :, :] = (0, 255, 0)This changes the top 20 rows to green. NumPy slices usually produce views into the original array, so editing a slice can edit the original frame. Use .copy() when you need an independent image:
top = frame[0:20, :, :].copy()Converting between BGR and RGB
Do not reorder channels by guessing. Use cv2.cvtColor, which applies a named color conversion:
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
bgr = cv2.cvtColor(rgb, cv2.COLOR_RGB2BGR)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
print(rgb.shape) # (120, 200, 3)
print(gray.shape) # (120, 200)A grayscale frame has one channel, so its shape is (height, width), not (height, width, 1). Some APIs accept either representation, but OpenCV's grayscale conversion normally produces the two-dimensional form.
Practical checks
When a frame comes from a camera or file, inspect it before processing:
print(frame.shape, frame.dtype)
if frame.ndim == 3 and frame.shape[2] == 3:
print("Color BGR frame")
elif frame.ndim == 2:
print("Grayscale frame")
else:
raise ValueError("Unexpected frame layout")These checks help catch assumptions about resolution, channel count, or data type. Operations such as blurring, thresholding, and edge detection consume this array and return another array. The read loop determines when each array is produced; per-frame processing determines how it changes.
Exercise
A pixel is [12, 200, 250] in an OpenCV color frame. Which channel has the greatest value, and which visible color does that pixel contain most strongly?
Show answer
The red channel has the greatest value because OpenCV uses BGR order. The pixel is mostly red, with a strong green component as well.