Sub topic 2
Reading an Image and Checking That It Loaded
Assume you know basic Python functions, can install packages with pip, and understand simple NumPy array indexing. OpenCV is a computer-vision library. In Python, you import it as cv2.
Install the package if needed:
pip install opencv-pythonLoading an Image
Use cv2.imread to read an image file from disk. It returns the image as a NumPy array. A NumPy array is a structured collection of numbers that represents the image’s pixels.
import cv2
img = cv2.imread("photo.jpg")
if img is None:
raise FileNotFoundError("Could not read photo.jpg")
print(img.shape)
print(img.dtype)The path can be relative, such as "photo.jpg" or "images/photo.jpg", or absolute, such as "/home/alex/images/photo.jpg". A relative path is resolved from the program’s current working directory, which might not be the directory containing your Python file.
cv2.imread does not normally raise an exception when it cannot read a file. It returns None. The file might be missing, the path might be wrong, the format might be unsupported, or the file might be damaged. Always check the result before using it.
Understanding the Loaded Array
For a color image, img.shape usually has three values:
(height, width, channels)For example, (720, 1280, 3) means 720 pixel rows, 1280 pixel columns, and three color channels.
The first two values describe the image’s height and width. This order is important. It differs from coordinate pairs, which usually use (x, y) or (width, height).
The common uint8 data type stores each channel value as an integer from 0 through 255. Zero represents no intensity, and 255 represents the maximum intensity.
OpenCV stores color channels in BGR order:
import cv2
img = cv2.imread("photo.jpg")
if img is None:
raise FileNotFoundError("Could not read photo.jpg")
print(f"height={img.shape[0]}, width={img.shape[1]}")
print(f"dtype={img.dtype}")
print(f"channels={img.shape[2]}")
b, g, r = img[0, 0]
print(f"top-left pixel: blue={b}, green={g}, red={r}")The expression img[0, 0] accesses the pixel at row 0 and column 0. Its three values are blue, green, and red, in that order. This BGR convention is one of the most common sources of color bugs when OpenCV code is combined with libraries that expect RGB order.
A grayscale image has one channel, so its shape is usually (height, width), not (height, width, 1). The imread flag controls how OpenCV loads the file:
color = cv2.imread("photo.jpg", cv2.IMREAD_COLOR)
gray = cv2.imread("photo.jpg", cv2.IMREAD_GRAYSCALE)
unchanged = cv2.imread("photo.jpg", cv2.IMREAD_UNCHANGED)The color flag loads a three-channel BGR image. The grayscale flag loads one channel. The unchanged flag preserves properties such as an alpha channel when the file contains one.
The Loading Workflow
Use a clear validation step near the point where you load the image. This makes later failures easier to understand and prevents confusing errors.
Exercise
What does a shape of (600, 800, 3) mean for an image loaded with cv2.imread?
Show answer
It means the image is 600 pixels high, 800 pixels wide, and has three BGR color channels.