Skip to content
Tutorials Center
My learning

Topics

Lesson 46 of 46 ยท Practice and Review

Computer Vision Cheat Sheet

A one-page computer vision cheat sheet: array shape and dtype rules, common kernels, key formulas, metric definitions, a pipeline checklist and frequent mistakes.

You have now met the main ideas of classical and neural computer vision. This page is the quick-reference version: tables you can scan in seconds, a few runnable checks, and links back to the lessons that explain the details. Use it when you remember that a tool exists but not its exact formula, shape or argument.

All code assumes import numpy as np and from scipy import ndimage as ndi. Images are arrays indexed [row, column] (row 0 is the top). Examples use tiny arrays and fixed seeds, so their output never changes.

flowchart LR
    A["Acquire<br/>array, dtype, color"] --> B["Clean<br/>denoise, normalize"]
    B --> C["Segment or describe<br/>threshold, edges, features"]
    C --> D["Measure or classify<br/>shapes, ML, CNN"]
    D --> E["Evaluate<br/>metrics, error analysis"]

The usual life of a vision pipeline. The checklist near the end maps to these five stages.

Shapes, dtypes and coordinates

Fact Rule
Grayscale image shape (H, W)
Color image shape (H, W, 3) for RGB (OpenCV-style libraries use BGR order)
Indexing img[row, col], a crop is img[r0:r1, c0:c1] (stop excluded)
Point order image arrays use (row, col); drawing and geometry APIs often use (x, y) = (col, row)
8-bit images uint8, range 0 to 255; arithmetic wraps modulo 256
Float images usually 0.0 to 1.0; check the range before mixing with uint8
Batch of images (N, H, W, C) in NumPy and TensorFlow, (N, C, H, W) in PyTorch
Flatten for ML imgs.reshape(len(imgs), -1) gives one row per image
Flip img[::-1] vertical, img[:, ::-1] horizontal, img[..., ::-1] reverses channels
Output size of a convolution floor((N + 2P - K) / S) + 1 for input N, padding P, kernel K, stride S
Python Shape and dtype checks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Color and intensity

Task Formula or call
Gray from RGB (luminance) 0.299 R + 0.587 G + 0.114 B
Scale to 0..1 img / 255.0
Negative 255 - img
Linear stretch (img - lo) / (hi - lo) * 255
Gamma 255 * (img / 255) ** gamma; gamma < 1 brightens, gamma > 1 darkens
Histogram np.bincount(img.ravel(), minlength=256)
Histogram equalization map each level through the normalized cumulative histogram (CDF)
Global threshold mask = img > t (flip for dark objects)
Otsu t maximizing w0 * w1 * (m0 - m1)^2
Adaptive threshold img < local_mean - offset
Hue-based mask convert to HSV and compare the hue channel, which ignores brightness

See Color Channels and Color Spaces, Brightness, Contrast and Gamma, Histograms and Equalization and Thresholding.

Common kernels

Kernel Matrix (rows) Effect
Box blur 3x3 [1 1 1; 1 1 1; 1 1 1] / 9 average of the neighbourhood
Gaussian 3x3 [1 2 1; 2 4 2; 1 2 1] / 16 smooth blur, weights fall off with distance
Sharpen [0 -1 0; -1 5 -1; 0 -1 0] boosts the center against its neighbours
Sobel x [-1 0 1; -2 0 2; -1 0 1] horizontal change, finds vertical edges
Sobel y [-1 -2 -1; 0 0 0; 1 2 1] vertical change, finds horizontal edges
Laplacian [0 1 0; 1 -4 1; 0 1 0] second derivative, responds to blobs and edges
Identity [0 0 0; 0 1 0; 0 0 0] leaves the image unchanged

Smoothing kernels sum to 1 (brightness is preserved); derivative kernels sum to 0 (flat areas give 0). Gradient magnitude is sqrt(gx^2 + gy^2) and direction is arctan2(gy, gx). Median, erosion and dilation are not kernels in this sense; they pick a rank value or min/max. Details: Convolution and Correlation, Blurring and Denoising and Gradients and Edge Detection.

Python Check a kernel table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Geometry

Transform Matrix (homogeneous, 3 by 3) Preserves
Translation by (tx, ty) [1 0 tx; 0 1 ty; 0 0 1] shape, size, angles
Scaling by (sx, sy) [sx 0 0; 0 sy 0; 0 0 1] angles (if sx = sy)
Rotation by a [cos a, -sin a, 0; sin a, cos a, 0; 0 0 1] lengths and angles
Shear [1 kx 0; ky 1 0; 0 0 1] parallel lines
Affine last row [0 0 1], 6 free numbers parallel lines, ratios along a line
Perspective (homography) full 3 by 3, 8 free numbers (divide by the last coordinate) straight lines only

Warp with inverse mapping: for every output pixel compute where it comes from in the input and interpolate (nearest, bilinear). Downsampling needs a blur first, otherwise you get aliasing. See Geometric Transforms and Resolution, Sampling and Noise.

Morphology, regions and shapes

Operation Meaning
Erosion pixel stays only if the whole structuring element fits; shrinks objects
Dilation pixel is set if the element touches any object pixel; grows objects
Opening erosion then dilation; removes small specks
Closing dilation then erosion; fills small holes and gaps
Gradient dilation minus erosion; the outline
Connected components ndi.label(mask); default is 4-connectivity, np.ones((3, 3)) gives 8
Area number of pixels in the region
Centroid mean row and mean column of the region's pixels
Circularity 4 * pi * area / perimeter^2 (1 for a perfect circle)
Bounding box ndi.find_objects(labels) gives slices per region

See Morphological Operations, Connected Components and Labeling and Contours and Shape Measurements.

Features and matching

Idea One-line summary
Harris corner large eigenvalues of the structure tensor in both directions; response det(M) - k * trace(M)^2, with k about 0.04 to 0.06
Scale space blur and downsample into pyramids; blobs peak in the Laplacian of Gaussian at their scale
Descriptor distance Euclidean for float vectors, Hamming for binary descriptors
Ratio test accept a match when the best distance is below about 0.75 of the second best
RANSAC fit on minimal random samples, count inliers, keep the model with the most
Hough transform each edge pixel votes for all lines rho = x cos(theta) + y sin(theta) through it
Template matching SSD (lower is better) or normalized cross-correlation (higher, from -1 to 1)
HOG histograms of gradient orientation per cell, normalized in blocks

Metrics

Metric Definition
Accuracy (TP + TN) / all
Precision TP / (TP + FP): of the things I flagged, how many were right
Recall TP / (TP + FN): of the real things, how many did I find
F1 2 * P * R / (P + R)
IoU (Jaccard) TP / (TP + FP + FN), or intersection area over union area
Dice 2 * TP / (2 * TP + FP + FN); equals 2 * IoU / (1 + IoU)
Detection hit IoU with an unmatched ground-truth box at or above a threshold (often 0.5)
AP / mAP area under the precision-recall curve per class / mean over classes
Confusion matrix rows = true class, columns = predicted class
Python Metrics from a confusion matrix

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For skewed classes prefer precision, recall, F1, IoU or Dice over accuracy. See Evaluating Vision Models and Semantic Segmentation and Its Metrics.

Neural network quick facts

Fact Formula
Conv parameters K * K * C_in * C_out + C_out (weights plus biases)
Dense parameters inputs * outputs + outputs
Receptive field per layer: rf += (k - 1) * jump, then jump *= stride
Max pooling 2x2, stride 2 halves height and width, no parameters
Residual block output = F(x) + x, which keeps gradients flowing in deep nets
Augmentation flips, shifts, crops, noise, brightness changes; only on the training set
Transfer learning freeze a pretrained backbone and train a head, or fine-tune with a small learning rate

See The Convolutional Layer, Pooling and CNN Architectures and Transfer Learning and Modern Vision Models.

Pipeline checklist

  1. Inspect: print shape, dtype, min and max; view a few samples.
  2. Normalize: consistent size, color order and value range, identical in training and production.
  3. Split first: hold out a test set (by group, not by frame) before tuning anything.
  4. Baseline: a simple method (threshold, raw pixels with logistic regression) to beat.
  5. Clean: denoise before thresholding or differentiating; fix lighting; remove speckles from masks.
  6. Validate: cross-validate on the training part; compare models on identical folds.
  7. Evaluate: per-class and per-slice metrics, confusion matrix, an error gallery.
  8. Report limits: state what the data did not cover.

Common mistakes

Mistake Fix
uint8 overflow after adding or multiplying widen the dtype, compute, then np.clip
Mixing up (row, col) and (x, y) write the order in a comment; check with a lopsided test image
BGR versus RGB one conversion at the boundary of each library
Thresholding a noisy image directly smooth first, then threshold, then clean with morphology
Global threshold on unevenly lit scenes adaptive threshold or flatten the lighting
Downsampling without a blur blur (or area interpolation) first to avoid aliasing
Forgetting padding effects at borders choose mode="nearest", reflect or zero padding on purpose
Tuning on the test set tune with cross-validation; touch the test set once
Reporting only accuracy on imbalanced data add per-class precision, recall and IoU or Dice
Different preprocessing in training and production share one preprocessing function

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A 28 by 28 image goes through a 3x3 convolution with padding 1 and stride 2. What is the output height?

    floor((28 + 2*1 - 3) / 2) + 1 = floor(27 / 2) + 1 = 13 + 1 = 14.
  2. 2. Which of these kernels should sum to 0?

    Derivative kernels return 0 on flat regions, so their weights sum to 0. Smoothing kernels sum to 1 to preserve brightness.
  3. 3. Which metric equals 2 * TP / (2 * TP + FP + FN)?

    That is the Dice coefficient. IoU uses TP / (TP + FP + FN) in the same terms.
  4. 4. A model gets 99% accuracy on a dataset where 99% of the images belong to one class. What do you conclude?

    With a dominant class, accuracy hides the failure on the rare class. Look at per-class recall, precision and the confusion matrix.

Summary

  • Images are [row, col] arrays; know your shape, dtype, value range and channel order before every operation.
  • Smoothing kernels sum to 1, derivative kernels sum to 0; Sobel gives gradients, Laplacian the second derivative.
  • Warp with inverse mapping; blur before you downsample.
  • Open to remove specks, close to fill gaps; label components, then measure area, centroid and box.
  • Precision, recall, F1, IoU and Dice describe results better than accuracy on imbalanced data.
  • Follow the pipeline checklist, and keep one shared preprocessing function for training and production.

That is the end of the course. Go back to the beginning with What Is Computer Vision? or build something with the digit recognizer project.