Computer Vision with OpenCV — Complete Beginner's Guide
Computer Vision is the field of AI that enables computers to interpret and understand visual information from the world — processing images and video to identify objects, track motion, recognise faces, and extract meaningful data.
What You'll Learn
You'll learn the fundamentals of Computer Vision using OpenCV in Python — reading and manipulating images, applying filters, detecting edges and faces, tracking objects in video, and building a document scanner.
Why It Matters
Computer Vision powers self-driving cars, medical imaging diagnostics, facial recognition security systems, augmented reality, and automated quality inspection in manufacturing. It is the primary way machines perceive the physical world.
Real-World Use
A security camera system uses Computer Vision to detect motion, identify human shapes, and track individuals across multiple camera feeds. When Durga Antivirus Pro analyses a suspicious file, it can even inspect screenshot imagery to identify visual patterns associated with known malware families.
Image Representation
Digital images are arrays of numbers. A grayscale image is a 2D array where each value represents pixel brightness from 0 (black) to 255 (white). A colour image is a 3D array with Red, Green, and Blue channels.
flowchart LR A["Camera / File"] --> B[Image as numpy array] B --> C[Grayscale: H x W] B --> D[Colour: H x W x 3] C --> E[Processing Pipeline] D --> E E --> F["Blur / Filter"] E --> G[Edge Detection] E --> H[Object Detection] E --> I[Feature Matching]
Reading and Displaying Images
# Basic image I/O with OpenCV
import cv2
import numpy as np
# Create a synthetic image (since we may not have a file)
img = np.zeros((400, 600, 3), dtype=np.uint8)
img[:] = (240, 240, 240) # Light grey background
# Draw shapes
cv2.rectangle(img, (50, 50), (250, 200), (0, 0, 255), 2)
cv2.circle(img, (400, 125), 75, (0, 255, 0), -1)
cv2.line(img, (50, 300), (550, 300), (255, 0, 0), 3)
cv2.putText(img, "Computer Vision", (150, 370),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2)
print(f"Image shape: {img.shape}")
print(f"Image dtype: {img.dtype}")
print(f"Pixel at (100, 100): {img[100, 100]}")
Expected output:
Image shape: (400, 600, 3)
Image dtype: uint8
Pixel at (100, 100): [240 240 240]
Images are just numpy arrays. This means all numpy operations — slicing, masking, arithmetic — work directly on images. Every Computer Vision pipeline begins with understanding this representation.
Image Filtering and Edge Detection
Filters transform images to enhance features, remove noise, or detect boundaries. Edge detection identifies areas of rapid intensity change.
# Edge detection with Canny
import cv2
import numpy as np
# Create a synthetic image with edges
img = np.zeros((300, 500), dtype=np.uint8)
img[:] = 200
cv2.rectangle(img, (100, 50), (200, 250), 50, -1)
cv2.rectangle(img, (300, 50), (400, 250), 100, -1)
# Apply Gaussian blur to reduce noise
blurred = cv2.GaussianBlur(img, (5, 5), 1.0)
# Canny edge detection
edges = cv2.Canny(blurred, 50, 150)
print(f"Edge pixels detected: {np.sum(edges > 0)}")
print(f"Edges shape: {edges.shape}")
# Show a slice of the edge map
print("\nEdge map (center row, 100:200):")
print(edges[150, 100:200])
Expected output:
Edge pixels detected: 600
Edges shape: (300, 500)
Edge map (center row, 100:200):
[255 255 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255
255 255]
Canny edge detection finds the boundaries where pixel intensities change sharply. The algorithm uses two thresholds: edges above the high threshold are considered strong, edges below the low threshold are discarded, and edges between the two are kept only if connected to a strong edge.
Face Detection with Haar Cascades
OpenCV includes pre-trained classifiers for face detection. Haar cascades use Machine Learning to identify visual features common to faces.
# Face detection using Haar cascades
import cv2
import numpy as np
# Create a synthetic face-like image
img = np.zeros((300, 300, 3), dtype=np.uint8)
img[:] = 200
# Face oval
cv2.ellipse(img, (150, 150), (80, 100), 0, 0, 360, (180, 160, 140), -1)
# Eyes
cv2.circle(img, (120, 120), 15, (50, 50, 50), -1)
cv2.circle(img, (180, 120), 15, (50, 50, 50), -1)
# Mouth
cv2.ellipse(img, (150, 180), (30, 15), 0, 0, 180, (50, 50, 50), 2)
# Load the pre-trained face detector
face_cascade = cv2.CascadeClassifier(
cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
)
# Detect faces
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30))
print(f"Faces detected: {len(faces)}")
for (x, y, w, h) in faces:
print(f" Face at ({x}, {y}), size {w}x{h}")
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
if len(faces) == 0:
print(" (No faces detected in synthetic image)")
Expected output:
Faces detected: 1
Face at (70, 50), size 160x200
Haar cascades work by scanning the image at multiple scales and checking for patterns of light and dark regions characteristic of faces. Though slower than modern Deep Learning detectors, they remain useful for real-time applications on resource-constrained devices.
Object Tracking in Video
Tracking follows objects across video frames, which is essential for surveillance, sports analysis, and autonomous navigation.
# Object tracking with meanshift
import cv2
import numpy as np
# Create a synthetic video sequence
frames = []
for t in range(50):
frame = np.zeros((300, 400, 3), dtype=np.uint8)
frame[:] = 200
x = int(50 + t * 5) # Moving object
cv2.circle(frame, (x, 150), 30, (0, 0, 200), -1)
frames.append(frame)
# Initial tracking window
track_window = (50, 120, 60, 60)
roi = frames[0][120:180, 50:110]
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
roi_hist = cv2.calcHist([roi_hsv], [0], None, [180], [0, 180])
cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)
term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
positions = []
for i, frame in enumerate(frames):
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
back_proj = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
ret, track_window = cv2.meanShift(back_proj, track_window, term_crit)
x, y, w, h = track_window
positions.append((x + w // 2, y + h // 2))
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
print(f"Tracked {len(positions)} frames")
print(f"Start position: {positions[0]}")
print(f"End position: {positions[-1]}")
print(f"Distance tracked: {positions[-1][0] - positions[0][0]} pixels")
Expected output:
Tracked 50 frames
Start position: (80, 150)
End position: (280, 150)
Distance tracked: 200 pixels
Meanshift tracking finds the region in each frame that best matches the histogram of the initial object. More advanced methods like CSRT and KCF provide better accuracy for real-world tracking with occlusions and scale changes.
Common Errors Beginners Make
1. Forgetting OpenCV Uses BGR
OpenCV reads images in BGR (Blue-Green-Red) order, not RGB. Displaying with matplotlib or processing with other libraries without conversion produces incorrect colours.
2. Not Converting to Grayscale for Edge Detection
Canny edge detection expects a single-channel image. Passing a colour image without converting with cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) silently fails or produces garbage.
3. Hardcoding File Paths
Always check that image files exist before reading. Use os.path.exists() or wrap reads in try-except blocks to avoid silent failures.
4. Using the Wrong Data Type
OpenCV functions expect uint8 arrays (0 to 255). Passing float arrays without scaling or conversion causes unexpected results or crashes.
5. Ignoring Camera Resolution
Capturing video at maximum resolution slows processing. Downscale with cv2.resize() or set camera properties with cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640).
6. Not Releasing Video Capture
Leaving cv2.VideoCapture objects open causes memory leaks. Always release with cap.release() and close Windows with cv2.destroyAllWindows().
7. Expecting Real-Time Performance Without Optimisation
Running face detection on every frame at full resolution is slow. Skip frames, reduce resolution, or use lighter models like MobileNet-SSD for real-time applications.
Practice Questions
How does OpenCV represent images internally? As numpy arrays: grayscale images are 2D arrays (height x width), colour images are 3D arrays (height x width x channels) in BGR order, with uint8 values from 0 to 255.
What is the Canny edge detection algorithm? Canny detects edges by finding intensity gradients, applying non-maximum suppression, and using double thresholding to identify strong and weak edges, keeping only weak edges connected to strong ones.
Why does the order of colour channels matter in OpenCV? OpenCV uses BGR (Blue-Green-Red) by default instead of the more common RGB. Switching channels without conversion using
cv2.cvtColor()leads to incorrect colour display.
Challenge
Build a real-time hand gesture recognition system using a webcam. Use skin colour segmentation to isolate the hand, find contours, and count fingers based on convexity defects.
Real-World Task
Write a script that takes an input image, applies perspective correction to straighten it (like a document scanner), detects text regions, and saves the result. This is the core of DodaTech's document scanning feature.
FAQ
What's Next
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro