OpenCV Tutorial: Learn Computer Vision from Scratch (2026)
Computer vision is how machines see the world, and OpenCV is the library that makes it accessible to everyone. I started with OpenCV to build a simple face detector and ended up spending years exploring image processing, object tracking, and 3D reconstruction. By 2026, OpenCV has grown to over 2500 optimized algorithms covering every area of computer vision. The library has been around since 2000 and remains the most widely used computer vision toolkit, powering everything from smartphone cameras to autonomous vehicles.
Installing OpenCV and Reading Images
OpenCV-Python is installed via pip and provides both the classic cv2 module and the newer cv2 module with additional functionality. The first thing you learn is how to read, display, and save images. OpenCV loads images as NumPy arrays, with BGR channel ordering rather than the RGB you might expect from other libraries.
I spent an hour debugging why my red car appeared blue in the display. OpenCV stores images in BGR format by default, while matplotlib expects RGB. The cvtColor function with COLOR_BGR2RGB is your friend. Once you internalize this quirk, the rest of OpenCV is remarkably intuitive and well-documented.
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('photo.jpg')
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
cv2.imwrite('output.jpg', img)
Image Processing Fundamentals
Image processing transforms images to enhance features or extract information. Core operations include resizing, cropping, rotating, filtering, and thresholding. Gaussian blur reduces noise, Canny edge detection finds boundaries, and morphological operations like erosion and dilation refine shapes. These operations are the building blocks of every computer vision pipeline.
I built a document scanner that uses thresholding to binarize the image, finds contours to detect the document edges, then applies a perspective transform to produce a clean scan-like output. The entire pipeline is 30 lines of code using standard OpenCV functions. Understanding the basics of image processing lets you solve surprisingly complex problems without machine learning.
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edges = cv2.Canny(blurred, 50, 150)
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
Feature Detection and Matching
Feature detection finds distinctive points in images that can be matched across different views. SIFT, SURF, and ORB are popular algorithms. ORB is free to use and works well for real-time applications. Each feature has a keypoint location and a descriptor that encodes the local image information. Matching descriptors between images enables panorama stitching, object recognition, and motion tracking.
I used ORB feature matching to build a panorama stitcher for vacation photos. The algorithm detects features in overlapping images, finds corresponding points using FLANN-based matching, computes a homography matrix, and warps the images into a seamless panorama. OpenCV's Stitcher class abstracts all this into a single function call, but understanding the underlying steps is essential when the automatic mode fails.
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1, des2)
Video Processing and Object Tracking
Video is just a sequence of images, and OpenCV processes video frames in a loop. You can read from video files or from a webcam in real time. Object tracking algorithms like CSRT, KCF, and MOSSE follow a selected object across frames. For more advanced tracking, OpenCV's DNN module runs deep learning-based trackers.
I built a traffic monitoring system that reads a video stream, applies background subtraction to detect moving vehicles, draws bounding boxes around them, and counts vehicles crossing a virtual line. The system runs at 30 FPS on modest hardware. The key insight was using morphological operations to clean up the foreground mask before contour detection, which eliminated false positives from shadows and noise.
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret: break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('Webcam', gray)
if cv2.waitKey(1) & 0xFF == ord('q'): break
cap.release()
cv2.destroyAllWindows()
Deep Learning-Based Object Detection
OpenCV's DNN module supports loading models from TensorFlow, PyTorch, Caffe, and ONNX. You can run state-of-the-art object detectors like YOLO, SSD, and Faster R-CNN without leaving the OpenCV ecosystem. The module handles preprocessing, inference, and post-processing, giving you bounding boxes, class labels, and confidence scores.
I replaced a traditional background subtraction system with YOLOv8 for pedestrian detection and saw accuracy jump from 60% to 95%. The DNN module handles all the complexity: loading the model, preprocessing the input to match the model's expected format, running inference, and parsing the output. OpenCV's DNN functions are optimized to run efficiently on CPUs, but GPU acceleration is available with CUDA support.
net = cv2.dnn.readNet('yolov8.weights', 'yolov8.cfg')
blob = cv2.dnn.blobFromImage(frame, 1/255.0, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
outputs = net.forward()
boxes, confidences, class_ids = [], [], []
Camera Calibration and 3D Reconstruction
Camera calibration corrects lens distortion and determines the camera matrix, which maps 3D world points to 2D image points. You photograph a checkerboard pattern from multiple angles, detect the corners, and compute the intrinsic and extrinsic parameters. Once calibrated, you can remove barrel distortion, measure real-world distances, and reconstruct 3D scenes from multiple views.
I built a 3D scanning system using a smartphone camera and OpenCV. After calibrating the camera with a checkerboard, I captured multiple angles of an object, extracted features, matched them across views, computed the 3D point cloud using Structure from Motion, and generated a mesh. The result was a passable 3D scan of a coffee mug. The entire pipeline from calibration to mesh took about 200 lines of OpenCV code.
# Find checkerboard corners for calibration
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
ret, corners = cv2.findChessboardCorners(gray, (9, 6), None)
if ret:
corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
objpoints.append(objp)
imgpoints.append(corners2)
Frequently Asked Questions
What is the difference between OpenCV and PIL/Pillow?
OpenCV is designed for computer vision tasks with hundreds of algorithms for feature detection, object tracking, and machine learning. Pillow is a Python imaging library focused on basic image manipulation. OpenCV is faster and more feature-rich for vision tasks.
Does OpenCV support GPU acceleration?
Yes, OpenCV can be built with CUDA support for GPU acceleration. The cv2.cuda module provides GPU-optimized versions of many algorithms. Pre-built pip packages usually do not include CUDA, so you need to build from source or use conda.
How do I detect faces with OpenCV?
Use the pre-trained Haar cascade classifier: face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') then faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5). For better accuracy, use the DNN module with a deep learning face detector.
Can OpenCV read video from IP cameras?
Yes, replace the video source with the RTSP URL: cv2.VideoCapture('rtsp://username:password@ip:port/stream'). OpenCV supports most IP camera protocols including RTSP, HTTP, and MJPEG streams.
Originally published on Ayodhyyya. Last updated June 1, 2026.