Computer vision from scratch

From pixels and kernels to corners, homographies, two-view geometry, stereo depth and optical flow — then detection and segmentation with deep networks.

A camera turns the 3D world into a grid of numbers. Computer vision runs the other way: from those numbers back to edges, objects, motion and depth. This course climbs that ladder one rung at a time — first the image as an array and the filters that clean and differentiate it, then features that can be found again in another photo, then the geometry that relates two views: homographies for panoramas, the fundamental matrix for structure from motion, disparity for stereo depth, and Lucas–Kanade for motion. The last lessons hand the problem to deep networks for classification, detection and segmentation.

The material comes from two repositories:

  • Image-and-Video-Processing-Problems — OpenCV basics, the DNN and torchvision projects, segmentation and background subtraction (lessons 1–4, 11 and 12).
  • Computer-Vision-Problems — Harris corners, panorama stitching, structure from motion, stereo and optical flow, written largely by hand in NumPy (lessons 5–10).

Every lesson has a live animation or figures produced by running the repository’s own code on its own data. Running it carefully also turned up bugs — a test that fails because a matrix is built transposed, a disparity map that stores the wrong quantity, loops that index past the image border. Each is explained where it occurs, with the fix.

Prerequisites

  • The linear algebra course, especially least squares and the SVD: almost every estimation step here is “stack the equations, take the last singular vector”.
  • Python and NumPy.

Running the code

git clone https://github.com/prajolshrestha/Computer-Vision-Problems.git
git clone https://github.com/prajolshrestha/Image-and-Video-Processing-Problems.git
pip install opencv-python numpy matplotlib
cd Computer-Vision-Problems/02_Panorama_Stitching/code
python -c "from AllFunctions.homography import testHomography; testHomography()"

The OpenCV-basics scripts expect a Resources/ folder of images that is not committed, and several scripts use Windows paths such as '..\data\img.png'; point them at your own files. The deep-learning notebooks in lesson 11 need PyTorch and torchvision and download pretrained weights on first use.

Lessons

  1. 1 Images as arraysAn image is a NumPy array of shape (height, width, 3) in BGR order. Reading, resizing, cropping and splitting channels are all array operations.
  2. 2 Geometric transforms and colour spacesRotation and translation as 2×3 matrices, and why HSV or LAB separate colour from brightness better than BGR.
  3. 3 Filtering and thresholdingSmoothing, sharpening and differentiating are one operation with different kernels — except the median, which is not linear at all.
  4. 4 Edges, contours and blobsSobel gradients, the four stages of Canny, and turning a binary mask into outlines and objects.
  5. 5 Harris cornersA corner is a place where the image changes in every direction. The structure tensor's eigenvalues say exactly that.
  6. 6 Features and matchingORB keypoints with binary descriptors, brute-force nearest neighbours under Hamming distance, and Lowe's ratio test.
  7. 7 Homography, RANSAC and panoramasFour point pairs fix a homography; RANSAC finds four good ones among many bad; chaining homographies stitches a panorama.
  8. 8 Two-view geometry and structure from motionThe fundamental matrix, the essential matrix, recovering the camera motion, and triangulating a 3D point cloud from two photos.
  9. 9 Stereo depthWith two rectified cameras, depth is inversely proportional to disparity. Finding the disparity is a matching problem along one row.
  10. 10 Optical flow with Lucas–KanadeBrightness constancy, one linear system per window, Gauss–Newton iterations — and the aperture problem that makes edges ambiguous.
  11. 11 Classification, detection and segmentation with deep networksPretrained networks through torchvision and the OpenCV DNN module — what each output means, and how boxes are cleaned up with IoU and non-maximum suppression.
  12. 12 Watershed segmentation and background subtractionSeparating touching objects with markers and a distance transform, and finding moving objects with a median background model.

Browse this course's code on GitHub →