Learn OpenCV

The standard library for computer vision and image processing

21 lessons · 4 hours · 4 levels · covers OpenCV 4.12+ / 5.x (cv2)

OpenCV is a large C++ computer vision library with first-class Python bindings (cv2) that work directly on NumPy arrays. It covers image I/O, filtering, geometry, feature detection, video, camera calibration and running neural networks. It is the default toolbox for preprocessing images, classic vision pipelines and real-time video work.

Before you start

OpenCV builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics6 lessons · 53 min in total
2. PracticalUse it productively at work, with the usual idioms13 lessons · 2 hours in total
3. AdvancedThe less common features, performance and trade-offs18 lessons · 3 hours in total
4. ExpertInternals and the edge cases experts know21 lessons · 4 hours in total

The OpenCV tree

OpenCVImages are NumPy arraysImages are NumPyarraysReading, writing and showing imagesReading, writing andshowing imagesColour spaces and cvtColorColour spaces andcvtColorDrawing shapes and textDrawing shapes andtextResize, crop, flip and rotateResize, crop, flip androtateThresholding and OtsuThresholding andOtsuBlurring and convolution filtersBlurring andconvolution filtersMorphological operationsMorphologicaloperationsGradients and Canny edgesGradients and CannyedgesHistograms and contrastHistograms andcontrastColour masks and bitwise operationsColour masks andbitwise operationsContours and shape measurementContours and shapemeasurementVideo files and camerasVideo files andcamerasThe ndarrayThe ndarrayData types (dtypes)Data types (dtypes)Reading and writing filesReading and writingfilesIndexing and slicingIndexing and slicingBoolean masksBoolean masksVectorized arithmeticVectorized arithmeticAggregations and the axis argumentAggregations and theaxis argumentfor and while loopsfor and while loops
Essentials and Practical shown (13 of 21 lessons); the full list is below. Click a lesson to read it.

All 21 lessons, in order

Images and I/O

  1. Images are NumPy arrays · Essentials · 8 min

    In cv2 an image is just an ndarray: height x width x channels, usually uint8, with channels in BGR order.

  2. Reading, writing and showing images · Essentials · 8 min

    Load images with cv2.imread, save with cv2.imwrite and display them with imshow plus waitKey.

  3. Colour spaces and cvtColor · Essentials · 10 min

    Convert between BGR, RGB, grayscale and HSV with cv2.cvtColor, and know why HSV is used for colour picking.

  4. Drawing shapes and text · Essentials · 7 min

    Annotate images in place with cv2.line, rectangle, circle, polylines and putText.

  5. Resize, crop, flip and rotate · Essentials · 10 min

    Crop with slicing, scale with cv2.resize and the right interpolation, and flip or rotate by 90 degrees.

  6. Thresholding and Otsu · Essentials · 10 min

    Turn grayscale images into binary masks with fixed, Otsu and adaptive thresholds.

Image processing

  1. Blurring and convolution filters · Practical · 12 min

    Smooth noise with box, Gaussian, median and bilateral filters, and apply custom kernels with filter2D.

  2. Morphological operations · Practical · 10 min

    Clean binary masks with erode, dilate, opening and closing using structuring elements.

  3. Gradients and Canny edges · Practical · 12 min

    Compute image gradients with Sobel and find clean, thin edges with the Canny detector.

  4. Histograms and contrast · Practical · 10 min

    Measure intensity distributions with calcHist and improve contrast with equalizeHist and CLAHE.

Shapes and segmentation

  1. Colour masks and bitwise operations · Practical · 10 min

    Select colour ranges with cv2.inRange and combine or apply masks with bitwise_and, or, not.

  2. Contours and shape measurement · Practical · 15 min

    Find object outlines in binary masks and measure their area, bounding boxes, centroids and shape.

Video and motion

  1. Video files and cameras · Practical · 12 min

    Read frames from files and webcams with VideoCapture and write processed video with VideoWriter.

  2. Background subtraction and optical flow · Advanced · 15 min

    Detect moving objects with MOG2/KNN background models and track points between frames with Lucas-Kanade flow.

Geometry and features

  1. Affine and perspective warps · Advanced · 15 min

    Rotate, scale and shear with warpAffine, and rectify planes such as documents with a perspective homography.

  2. Keypoints, descriptors and matching · Advanced · 15 min

    Detect ORB or SIFT keypoints, match descriptors between images and estimate a homography robustly with RANSAC.

  3. Camera calibration and undistortion · Advanced · 15 min

    Estimate a camera's intrinsic matrix and lens distortion from chessboard images, then undistort frames.

Deep learning

  1. Running neural networks with cv2.dnn · Advanced · 15 min

    Load ONNX models with cv2.dnn, prepare inputs with blobFromImage and run inference without a deep learning framework.

Performance and internals

  1. How cv2 maps NumPy arrays to cv::Mat · Expert · 15 min

    Understand how the bindings convert arrays, why non-contiguous or wrong-dtype inputs fail or copy, and how outputs are allocated.

  2. Performance: threads, UMat and vectorising · Expert · 12 min

    Measure and speed up cv2 pipelines with threading control, the T-API's UMat, and avoiding per-pixel Python loops.

  3. Packages, builds and contrib modules · Expert · 10 min

    Choose between opencv-python, -headless and -contrib wheels, inspect build options and know when to compile your own.