72 min total

Computer Vision & OpenCV

OpenCV: image dimensions

OpenCV: image dimensions

Image dimensions

OpenCV images are NumPy arrays. A color image’s shape is (height, width, channels), so a 1280-pixel-wide, 720-pixel-high image with three channels has shape (720, 1280, 3).

python
import cv2

image = cv2.imread("image.jpg")
if image is None:
    raise FileNotFoundError("image.jpg")

height, width = image.shape[:2]
channels = 1 if image.ndim == 2 else image.shape[2]
print(height, width, channels)

Grayscale images usually have two dimensions. Color images loaded with OpenCV’s default imread mode use BGR channel order.

Reference: OpenCV — Basic Operations on Images

Shape-order correction: for an image 1280 pixels wide and 720 pixels high, NumPy/OpenCV reports (720,1280,3): height, width, channels. The notebook reversed the first two entries.