Computer vision enables machines to interpret and understand images and video. OpenCV (Open Source Computer Vision Library) is the most widely used library for computer vision in Python, powering everything from industrial quality control to self-driving cars. This guide takes you from image basics to deep learning-based detection.
Installing OpenCV
pip install opencv-python opencv-python-headless numpy
Use opencv-python for environments with a display (development). Use opencv-python-headless in production servers without a screen.
Reading, Displaying, and Writing Images
import cv2
import numpy as np
# Read image (BGR format, not RGB!)
img = cv2.imread("photo.jpg")
print(img.shape) # (height, width, channels)
# Convert BGR → RGB for matplotlib display
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Resize
resized = cv2.resize(img, (224, 224))
# Write image
cv2.imwrite("output.jpg", resized)
OpenCV reads images in BGR (not RGB) by default — a common gotcha when mixing with Matplotlib or Pillow.
Image Processing Fundamentals
# Grayscale conversion
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Gaussian blur (noise reduction)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# Thresholding
_, thresh = cv2.threshold(blurred, 127, 255, cv2.THRESH_BINARY)
# Adaptive thresholding (better for uneven lighting)
adaptive = cv2.adaptiveThreshold(blurred, 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
Edge Detection
# Canny edge detector
edges = cv2.Canny(blurred, threshold1=50, threshold2=150)
# Sobel (gradient-based)
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
magnitude = np.sqrt(sobelx**2 + sobely**2)
Contour Detection and Shape Analysis
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
area = cv2.contourArea(cnt)
if area > 500: # filter small noise
x, y, w, h = cv2.boundingRect(cnt)
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
Face Detection with Haar Cascades
face_cascade = cv2.CascadeClassifier(
cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
print(f"Found {len(faces)} faces")
Object Detection with Deep Learning (DNN Module)
import cv2
# Load a pre-trained YOLO model
net = cv2.dnn.readNet("yolov4.weights", "yolov4.cfg")
layer_names = net.getLayerNames()
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
# Prepare image
blob = cv2.dnn.blobFromImage(img, 1/255.0, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
outputs = net.forward(output_layers)
# Parse detections
for output in outputs:
for detection in output:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
# Draw bounding box
center_x = int(detection[0] * img.shape[1])
center_y = int(detection[1] * img.shape[0])
w = int(detection[2] * img.shape[1])
h = int(detection[3] * img.shape[0])
cv2.rectangle(img,
(center_x - w//2, center_y - h//2),
(center_x + w//2, center_y + h//2),
(0, 255, 0), 2)
Optical Flow and Video Processing
cap = cv2.VideoCapture("video.mp4")
ret, prev_frame = cap.read()
prev_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)
while cap.isOpened():
ret, frame = cap.read()
if not ret: break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
flow = cv2.calcOpticalFlowFarneback(prev_gray, gray, None, 0.5, 3, 15, 3, 5, 1.2, 0)
prev_gray = gray
cap.release()
Conclusion
OpenCV is an incredibly deep library — this guide covered the most practical 20% that handles 80% of real use cases. From basic image processing to deep learning inference with YOLO, OpenCV gives Python developers the tools to build production-grade computer vision applications. Combine it with PyTorch or TensorFlow for custom model training, and with FastAPI or Flask for deploying vision APIs.



