Object detection, tracking and recognition
Clear, exam-focused notes on object detection, tracking and recognition: definitions, core algorithms (detection metrics, data association, Kalman filter), formulas, worked examples (IoU numeric, Python metric code, Kalman derivation), common exam mistakes and quick checks.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Object detection, tracking and recognition are the building blocks of many machine-vision systems used by engineers: automated inspection on a factory line, driver-assistance systems (detecting and tracking vehicles and pedestrians), and surveillance or robotics. A good engineer must know how detectors report objects, how trackers maintain identity across frames, and how recognition differs from detection.
Key ideas
Assumption often used: rectangular boxes aligned with image axes (axis-aligned bounding boxes).
Bounding box: a rectangle that localises an object. Representations commonly used: (x1,y1,x2,y2) or (cx,cy,w,h) where (cx,cy) is center; all pixel coordinates in image plane (unit: pixels). Oriented boxes may use (cx,cy,w,h,θ) with rotation θ. Bold: bounding box.
Object recognition (classification): assigning a class label to an entire image or to a detected region. Bold: recognition means identifying what the object is (e.g., car, person), not where it is.
Object tracking: the problem of following one or more detected objects over time in a video while maintaining identity across frames. Bold: tracking means producing trajectories (object ID + bounding box per frame).
Detection metrics (exact definitions used in exams):
- Intersection over Union (IoU): measure of overlap between predicted and ground-truth bounding boxes. Defined as area(intersection)/area(union). (Unitless.)
- Precision: fraction of predicted positives that are true positives: TP / (TP + FP).
- Recall: fraction of true positives found: TP / (TP + FN).
- mAP (mean Average Precision): average of interpolated precisions across recall levels and across classes; common practice: compute AP per class by integrating precision–recall curve and then average. (Precise implementation depends on the evaluation protocol: VOC-style AP uses 11-point or integral; COCO averages over IoU thresholds 0.50:0.95 in steps of 0.05, and often over object sizes.) Tracking metrics you should know: MOTA = 1 − (FN + FP + IDsw)/GT; MOTP measures localization error over matched pairs; IDF1 = 2IDTP / (2IDTP + IDFP + IDFN); also report ID switches (IDsw) and HOTA which balances detection, association, and localization.
Detector families (short definitions):
- Sliding-window + classifier: test a classifier at many windows and scales (older approach).
- Feature-based detectors: HOG + SVM, Viola–Jones cascade for faces (real-time with Haar-like features + integral image).
- Region proposal + CNN: R-CNN family—generate region proposals, extract CNN features per region, classify and refine box.
- Single-shot: anchor-based (YOLO, SSD) or anchor-free (FCOS, CenterNet) predict boxes and classes directly from feature maps (fast, end-to-end).
- For exams: know the pipeline differences and relative speed/accuracy trade-offs. Most detectors apply Non-Maximum Suppression (NMS) or Soft-NMS to remove duplicate overlapping boxes; the NMS IoU threshold impacts precision/recall.
Tracking methods:
- Detection-based tracking (tracking-by-detection): run a detector per frame; then solve data association to link detections to existing tracks. Bold: data association = assignment of detections to track identities.
- Association methods: greedy IoU matching, Hungarian algorithm for global optimal assignment minimizing a cost matrix, or graph-based global association.
- Motion models: constant-velocity or constant-acceleration linear models, used inside filters.
- Kalman filter (linear Gaussian) and particle filter (nonlinear / non-Gaussian) are two common state estimators used with trackers.
Appearance models: using visual features (color histogram, deep features) to help association when motion is ambiguous.
Mahalanobis distance: used to measure how well a detection fits a track's predicted state considering covariance; useful for gating and association. For gating, compare the squared distance d^2 = (x − μ)^T S^{-1} (x − μ) to a chi-square threshold χ^2_{m,α} (m = measurement dimension, α = confidence, e.g., 0.95).
Typical exam diagrams to draw and label:
- Bounding-box overlap: two rectangles showing intersection, union areas, and the IoU formula.
- Kalman filter block diagram: Prediction step (state x̂_{k|k-1}, covariance P_{k|k-1}) leading to update with measurement z_k and Kalman gain K_k producing x̂_{k|k} and P_{k|k}.
Formulas
IoU = area(B_pred ∩ B_gt) / area(B_pred ∪ B_gt) — IoU is unitless; B_pred and B_gt are bounding boxes; definition holds for any boxes. For rotated boxes, compute intersection/union via polygon clipping areas; for axis-aligned boxes, use rectangle overlaps.
Precision = TP / (TP + FP) — TP (true positives), FP (false positives); unitless; applies when detections are labeled true/false by IoU threshold.
Recall = TP / (TP + FN) — FN (false negatives); unitless.
F1 = 2 * (Precision * Recall) / (Precision + Recall) — harmonic mean of precision and recall; unitless.
mAP = (1 / C) * sum_{c=1..C} AP_c — C = number of classes; AP_c = area under precision–recall curve for class c; unitless; depends on evaluation protocol.
Kalman filter (discrete linear Gaussian) prediction and update:
x̂_{k|k-1} = A * x̂_{k-1|k-1} + B * u_k — x̂ is state estimate (units depend on state, e.g., meters and meters/second), A is state transition matrix, B is input model, u_k control input.
P_{k|k-1} = A * P_{k-1|k-1} * A^T + Q — P is state covariance (units = squared units of state), Q is process noise covariance.
K_k = P_{k|k-1} * H^T * (H * P_{k|k-1} * H^T + R)^{-1} — Kalman gain; H is measurement matrix, R is measurement noise covariance.
x̂_{k|k} = x̂_{k|k-1} + K_k * (z_k - H * x̂_{k|k-1}) — z_k is measurement vector (units of measured quantities, e.g., pixels).
P_{k|k} = (I - K_k * H) * P_{k|k-1} — posterior covariance.
d_Mahalanobis(x, μ) = sqrt((x - μ)^T * S^{-1} * (x - μ)) — S is covariance matrix; distance unitless; used for gating and association.
Notes: each matrix equation assumes dimensions match (verify A, H sizes), and that inverses exist (HPH^T+R must be invertible). SI units depend on the state variables chosen (e.g., meters, pixels, seconds). For numerical stability, a common choice is the Joseph form P_{k|k} = (I - K_k H) P_{k|k-1} (I - K_k H)^T + K_k R K_k^T, which preserves symmetry and positive semi-definiteness.
Worked examples
- Numerical IoU and basic metrics (step-by-step)
Given ground-truth box GT = (x1=10,y1=10,x2=50,y2=50) pixels and predicted box P = (30,30,70,70) pixels. Compute IoU.
- Intersection rectangle: left = max(10,30)=30, top = max(10,30)=30, right = min(50,70)=50, bottom = min(50,70)=50.
- Intersection width = right - left = 50 - 30 = 20 px, height = 50 - 30 = 20 px → area_inter = 20 * 20 = 400 px^2.
- Area_GT = (50 - 10) * (50 - 10) = 40 * 40 = 1600 px^2. Area_P = (70 - 30) * (70 - 30) = 40 * 40 = 1600 px^2.
- Union = 1600 + 1600 - 400 = 2800 px^2.
- IoU = 400 / 2800 = 0.142857... → 0.1429 (unitless).
Suppose in an image there are 3 ground truths and detector produced 3 detections. With IoU threshold 0.5, matches found: 2 true positives, 1 detection does not match any GT (false positive), so TP=2, FP=1, FN=1. Then
- Precision = 2 / (2 + 1) = 0.6667
- Recall = 2 / (2 + 1) = 0.6667
- F1 = 2*(0.6667*0.6667)/(0.6667+0.6667) = 0.6667 Final answers: Precision=0.6667, Recall=0.6667, F1=0.6667.
- Python program: compute IoU and precision/recall for simple lists (runnable)
- Code:
def iou(boxA, boxB):
xA = max(boxA[0], boxB[0])
yA = max(boxA[1], boxB[1])
xB = min(boxA[2], boxB[2])
yB = min(boxA[3], boxB[3])
interW = max(0, xB - xA)
interH = max(0, yB - yA)
interArea = interW * interH
areaA = max(0, (boxA[2] - boxA[0])) * max(0, (boxA[3] - boxA[1]))
areaB = max(0, (boxB[2] - boxB[0])) * max(0, (boxB[3] - boxB[1]))
union = areaA + areaB - interArea
return interArea / union if union > 0 else 0.0
# Example data
gt_boxes = [(10,10,50,50), (100,100,140,140), (200,200,240,240)]
det_boxes = [(30,30,70,70), (100,100,140,140), (210,210,240,240)]
threshold = 0.5
# simple greedy matching by highest IoU per GT
matches = 0
used_det = set()
for i, gt in enumerate(gt_boxes):
best_iou = 0
best_j = -1
for j, det in enumerate(det_boxes):
if j in used_det: continue
val = iou(gt, det)
if val > best_iou:
best_iou = val
best_j = j
if best_iou >= threshold:
matches += 1
used_det.add(best_j)
TP = matches
FP = len(det_boxes) - TP
FN = len(gt_boxes) - TP
precision = TP / (TP + FP) if (TP + FP) > 0 else 0
recall = TP / (TP + FN) if (TP + FN) > 0 else 0
print(f"TP={TP}, FP={FP}, FN={FN}")
print(f"Precision={precision:.4f}, Recall={recall:.4f}")
- Sample exact output when run with the example data above: TP=2, FP=1, FN=1 Precision=0.6667, Recall=0.6667
- Theoretical model answer (Derive Kalman gain formula) — common exam derivation
- System: x_k = A x_{k-1} + w_{k-1}, w
N(0,Q). Measurement: z_k = H x_k + v_k, vN(0,R). Assume prior estimate x̂_{k|k-1} with covariance P_{k|k-1}. - We seek x̂_{k|k} = x̂_{k|k-1} + K (z_k - H x̂_{k|k-1}), choose K to minimize trace(P_{k|k}) where P_{k|k} = E[(x_k - x̂_{k|k})(x_k - x̂_{k|k})^T].
- Substitute, expand, take derivative of trace(P_{k|k}) w.r.t K, set to zero and solve, which yields K = P_{k|k-1} H^T (H P_{k|k-1} H^T + R)^{-1}.
- State the assumptions: linear measurement, Gaussian noises, covariances known.
Common mistakes
- Not carrying units in state for Kalman filter answers (specify if state uses pixels or meters and time step units).
- Using greedy matching without stating threshold or tie-breaking rule in exam answers when asked about association.
- Giving mAP value without specifying IoU threshold or evaluation protocol (VOC vs COCO) — always state the protocol.
- In Kalman derivation: forgetting to assume zero-mean Gaussian process/measurement noise; without this the optimality claim fails. Omitting NMS or mis-setting the NMS IoU threshold leading to many duplicate detections (FPs). Using unsquared Mahalanobis distance with chi-square gating instead of the squared form d^2.
For your exam
Kinds of questions you will see:
- Definitions: define bounding box, IoU, precision/recall, tracking-by-detection, Kalman filter.
- Short notes: compare R-CNN vs YOLO; explain data association methods (greedy vs Hungarian); describe appearance vs motion models.
- Derivations: derive Kalman update and Kalman gain; show IoU derivation for rotated vs axis-aligned boxes (conceptually).
- Numericals: compute IoU, precision/recall, given small sets of boxes; compute Kalman predict/update in 1D with numbers.
- Programs/algorithms: write pseudocode for greedy IoU matching; or small Python to compute IoU and metrics; or explain steps of a tracker pipeline. Implement NMS (or Soft-NMS) and compute tracking metrics (e.g., MOTA/IDF1) from per-frame associations.
- Diagrams: draw two bounding boxes and label intersection/union; draw Kalman filter block diagram and label matrices.
What a full-marks answer includes:
- Clear definitions with bold terms, correct formulas, stated assumptions (noise Gaussian? linear model?), step-by-step derivation or algorithm, and for numericals: show intermediate arithmetic and units.
Quick check
- What is IoU between two identical boxes?
- Name two families of detectors and one pro/cons of each.
- Write the Kalman prediction equation for covariance P. Answers: 1. IoU = 1. 2. Region-proposal + CNN (high accuracy, slower) and single-shot like YOLO (fast, somewhat lower accuracy). 3. P_{k|k-1} = A P_{k-1|k-1} A^T + Q.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?