Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Python Libraries for Computer Vision
Several Python libraries are commonly used for Computer Vision. Knowing what each one does and when to use it helps you write better code.
The Core Libraries
OpenCV (cv2):
- The most widely used Computer Vision library
- Fast C++ backend with Python bindings
- Covers image loading, processing, filtering, detection, and video
- Used in both academic research and production systems
NumPy:
- OpenCV images are stored as NumPy arrays
- Provides fast array operations that work directly on image data
- Essential for any numerical manipulation of pixel values
Pillow (PIL):
- Pure Python image library
- Good for simple tasks: opening, resizing, format conversion
- Slower than OpenCV but easier to use for basic operations
Matplotlib:
- Data visualisation library — not CV-specific
- Used to display images in Jupyter notebooks
- Expects RGB channel order (convert from OpenCV's BGR first)
Machine Learning follows a structured pipeline from data to deployment
Library Quick Reference
import cv2 # Main CV operations
import numpy as np # Array/pixel manipulation
from PIL import Image # Simple image tasks
import matplotlib.pyplot as plt # Display in notebooks
# OpenCV: load, process, detect, transform
img_cv = cv2.imread("photo.jpg")
# NumPy: work directly with pixel arrays
print(img_cv.shape) # (H, W, 3)
print(img_cv.dtype) # uint8
print(img_cv.max()) # 255
# PIL: open and basic operations
img_pil = Image.open("photo.jpg")
img_pil_resized = img_pil.resize((200, 200))
# Matplotlib: display (expects RGB, not BGR)
img_rgb = cv2.cvtColor(img_cv, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt.axis("off")
plt.show()