Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Correlation and deformable templates

Correlation measures similarity by sliding a template over data and computing inner products; deformable templates extend rigid templates by adding parameters or control points and an energy to optimize for flexible matching.

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

Pattern recognition engineers use correlation for fast template matching in images and signals (for example, locating a logo on a page or a feature in an image). Deformable templates are used when the object can change shape (for example, matching hand shapes, letters with variable handwriting, or biological shapes); they let you model expected deformations and find the best fit by optimization.

Key ideas

Start from vectors and inner products. Let x and y be finite-length vectors (for images, flatten a local patch into a vector). The inner product x·y = sum_i x_i y_i measures linear similarity. Important properties: bilinearity, symmetry and relation to length (||x||^2 = x·x).

Define cross-correlation (discrete) between a signal f[n] and a template t[n] as the sliding inner product:

  • For 1-D signals, r[k] = sum_n f[n+k] t[n]. Correlation checks how similar f is to shifted copies of t.
  • For 2-D images, correlation between image I(x,y) and template T(u,v) at shift (dx,dy) is C(dx,dy) = sum_{u,v} I(u+dx, v+dy) T(u,v). Describe to draw: a small square template over a larger image, arrow to indicate shift (dx,dy), show overlapping region and label sums.

Define convolution for signals g * h: (g*h)[k] = sum_n g[n] h[k-n]. Relation: convolution is correlation with one function reversed; for symmetric templates convolution = correlation.

Define template as a fixed pattern (array) we look for in data; template matching is the process of searching an image for regions similar to the template.

Problems with raw correlation: it is sensitive to mean intensity and contrast. Define normalized cross-correlation (NCC): subtract means and divide by standard deviations to measure correlation independent of brightness/contrast. NCC ranges from -1 to 1; +1 indicates perfect positive linear match of the window to the template (up to affine intensity changes), 0 no linear correlation, and -1 perfect negative match (contrast inverted). In practice for sliding windows, compute local means and sums of squares using integral images to evaluate NCC in O(1) per shift.

Alternative measure: sum of squared differences (SSD): SSD(dx,dy)=sum_{u,v} (I(u+dx,v+dy) - T(u,v))^2. SSD is a distance measure; small SSD means good match. Note relation: for vectors f and t, SSD = ||f||^2 + ||t||^2 - 2 f·t. For mean-subtracted and L2-normalized vectors, SSD = 2(1 - NCC). State assumptions clearly when this identity holds.

Fourier methods: using the correlation theorem, correlation (and convolution) can be computed by multiplication in the frequency domain. Assumptions: finite discrete signals, implement with FFT (implicitly assuming periodic extension).

Deformable templates: two main types

  • Parametric deformable template: template described by a finite set of parameters p (translation, rotation, scale, local warps). The shape is T(x; p).
  • Free-form (active contour / spline) deformable template: controlled by control points or splines; the contour moves under internal and external forces.

Define an energy function E(p) = E_data(p) + λ E_reg(p). Here E_data measures mismatch between deformed template and image (for example SSD or negative NCC), E_reg penalizes unlikely deformations (bending energy, length), and λ balances them. The goal: find p* = argmin_p E(p). Optimization methods: gradient descent (p ← p - η ∇E), conjugate gradient, simulated annealing, dynamic programming (for chain-structured templates). When sampling image evidence at non-integer locations during deformation, use interpolation (e.g., bilinear) and apply the chain rule to differentiate E_data with respect to p.

Describe a diagram for deformable template: draw original template shape (dashed), deformed shape (solid) over an image edge; label control points and forces: internal (smoothness) and external (image gradient) energies.

Assumptions and limits: correlation assumes linear similarity and fixed template; normalized versions assume non-zero variance; Fourier methods assume periodicity; deformable template optimization may get stuck in local minima and needs initialization close to solution.

Formulas

C(dx,dy) = sum_{u,v} I(u+dx, v+dy) * T(u,v) — I: image intensity (unitless), T: template intensity (unitless), applies for discrete 2-D correlation.

r[k] = sum_n f[n+k] * t[n] — 1-D discrete cross-correlation; signals unitless; valid for finite-length sequences.

(g * h)[k] = sum_n g[n] * h[k - n] — discrete convolution; unitless; used when template is reversed.

NCC(dx,dy) = ( sum_{u,v} (I(u+dx,v+dy) - μ_I) * (T(u,v) - μ_T) ) / ( sqrt(sum (I(u+dx,v+dy)-μ_I)^2) * sqrt(sum (T-μ_T)^2) ) — μ are local means; unitless; valid when denominators ≠ 0.

SSD(dx,dy) = sum_{u,v} (I(u+dx,v+dy) - T(u,v))^2 — unitless; lower is better.

SSD = ||f||^2 + ||t||^2 - 2 f·t — vector form; unitless; valid for same-length vectors.

If mean-subtracted and L2-normalized: SSD = 2 * (1 - NCC) — assumes vectors f and t have zero mean and unit L2 norm.

correlation via FFT: corr = IFFT( FFT(I) * conj(FFT(T)) ) — use complex conjugate; assumes signals padded appropriately and discrete periodic extension.

E(p) = E_data(p) + λ E_reg(p) — energy to minimize for deformable template; energy units consistent with chosen data term.

Gradient descent update: p <- p - η * ∂E/∂p — η is step size (unit: same as p per energy); convergence requires small enough η.

Worked examples

Example 1 — numerical 2-D correlation (sliding match): Given image I (3×3): I = [[2, 3, 1], [4, 5, 0], [1, 2, 3]] (unitless), template T (2×2): T = [[5, 0], [2, 1]]. Find the correlation C(dx,dy) for all valid shifts and pick best match. Steps:

  1. Possible top-left shifts (dx,dy) are (0,0), (0,1), (1,0), (1,1). Compute C at each.
  2. C(0,0) = 25 + 30 + 42 + 51 = 10 + 0 + 8 + 5 = 23.
  3. C(1,0) uses image subpatch [[3,1],[5,0]]: 35 + 10 + 52 + 01 = 15 + 0 +10 +0 =25.
  4. C(0,1) uses [[4,5],[1,2]]: 45 +50 +12 +21 =20+0+2+2=24.
  5. C(1,1) uses [[5,0],[2,3]]: 55 +00 +22 +31 =25+0+4+3=32. Best match at (dx,dy) = (1,1) with correlation 32 (unitless). Conclusion: template best aligns with image subpatch whose top-left is (1,1).

Example 2 — relation between NCC and SSD (derivation exam style): Question: Show the relation SSD = 2(1 - NCC) when f and t are mean-subtracted and L2-normalized. Answer (model): Let f and t be vectors with mean zero (sum components = 0) and ||f|| = ||t|| = 1. Define NCC = f·t (dot product because both zero-mean and unit-norm). Then SSD = ||f - t||^2 = ||f||^2 + ||t||^2 - 2 f·t = 1 + 1 - 2 NCC = 2(1 - NCC). Assumptions: both vectors mean-subtracted and normalized to unit L2 norm. This derivation is exact; draw diagram showing two unit vectors and angle θ where f·t = cos θ and SSD = 2(1 - cos θ).

Example 3 — short model answer: Describe how to use deformable template for matching a handwritten character. Model answer: (1) Choose parametric form: control points along contour represented by spline with parameter vector p (positions of control points). (2) Define E_data(p) = - sum of the image edge strength sampled along the template boundary mapped by p (strong edges along the boundary reduce energy). (3) Define E_reg(p) = sum of squared second differences of control point positions (bending energy). (4) Minimize E = E_data + λ E_reg using gradient descent starting from coarse alignment; mention multi-resolution strategy and risk of local minima; mention validation by visual inspection or energy threshold.

Common mistakes

  • Confusing convolution and correlation signs (reversal in convolution). State clearly that convolution flips one function.
  • Applying FFT without zero-padding: circular correlation artifacts appear because of implicit periodic extension.
  • Using SSD when illumination varies — SSD is sensitive to brightness/contrast.
  • For deformable templates, ignoring the need for regularization (E_reg) and reporting only data fit, leading to unrealistic deformations.
  • Wrong gradient sign in gradient descent update (should subtract gradient for minimization).

For your exam

Typical questions:

  • Define correlation and normalized cross-correlation (short definitions). Full marks: explicit formulas, explanation of mean subtraction and normalization, conditions when denominator is zero.
  • Derive relation between SSD and correlation (derivation). Full marks: vector algebra steps and clear statement of assumptions (zero-mean or normalization) that make relation exact.
  • Compute correlation by hand for a small image and template (numerical). Full marks: show each sum, list shifts, choose best match, label coordinates.
  • Explain correlation theorem and how to compute correlation with FFT (short note). Full marks: state formula with conjugation, mention padding and periodicity assumption, and complexity improvement (O(n log n)).
  • Describe deformable templates and write energy function (theory and diagram). Full marks: give E = data + λ regularizer, explain terms, optimization method, and draw diagram labeling forces and control points.
  • Sketch algorithm and pitfalls for matching with deformable templates. Full marks: discuss initialization, multi-resolution, local minima, choice of λ, and evaluation.

Quick check

  1. Write formula for 2-D discrete correlation at shift (dx,dy).
  2. State the correlation theorem using FFT (what operation in frequency domain).
  3. If two mean-subtracted vectors f and t have unit L2 norm and dot product 0.8, compute SSD between f and t. Answers: C(dx,dy) = sum_{u,v} I(u+dx, v+dy) * T(u,v); corr = IFFT( FFT(I) * conj(FFT(T)) ) (use complex conjugate); SSD = 2*(1 - 0.8) = 0.4.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?