Two images show the same thing, but one is shifted against the other. How far? This is the simplest registration problem, and today’s tool is the most direct one: try every shift and score it.
Where shifts come from: mosaic tiles, drift in a time-lapse, offset colour channels.
Slide image B over image A and score the pixels on top of each other.
Three scores: SSD, cross-correlation (CC) and normalised cross-correlation (NCC), and how each one is fooled.
The cost of trying every shift: the question for the next unit.
The same machine with a small image: template matching.
Slides titled The math give the formulas and proofs; the ideas are complete without them.
Mosaic tiles. A microscope images a large sample as overlapping tiles. The stage is not exact: each overlap is off by a few pixels.
Drift. In a time-lapse, the sample or the stage creeps: frame 50 is a few pixels away from frame 1.
Colour channels. Two cameras, or two filters, see the sample slightly offset.
In all three: image B is image A moved by a vector t = (t_x, t_y).
Only the overlap can be compared; the rest of each image is new.
The brightness may differ (exposure, bleaching, another dye).
Find t, then place B at t on A.
Today: t in whole pixels, nothing but a shift (no rotation).
Slide and score
Place B at a candidate shift t on top of A. Every pixel of B now lies on a pixel of A.
Compare the pairs of pixels in the overlap and give the shift a score.
Do this for everyt: the scores form a curve in 1D, a surface in 2D.
The best score marks the shift. A clear, single peak is what we hope for.
The interesting part is the score. Three classics follow, each with its own way of being fooled.
Three scores
score
in words
best when
fooled by
SSD
sum of squared differences of the pixel pairs
smallest
a change of brightness or contrast between A and B
CC
sum of products of the pixel pairs
largest
bright regions: a bright blob scores high wherever it lands
NCC
correlation of the pairs after removing the mean and dividing by the spread, in the overlap
largest (at most 1)
little information: tiny overlaps, flat regions, repeated patterns
NCC does not care whether B is brighter or has more contrast than A: it compares patterns, not grey levels. It is the default score for registration and template matching.
Registration by NCC as pseudocode
for every shift t in the search window:
pairs = all (A(x + t), B(x)) with x + t inside A
if there are too few pairs: skip t
mA, mB = the means of the two sides
NCC(t) = Σ (A − mA)(B − mB) / sqrt( Σ (A − mA)² · Σ (B − mB)² )
best shift = the t with the largest NCC
One multiply-add per pixel pair: the cost is the sum of all overlaps.
A best shift on the edge of the window, or a low NCC, means “not found”.
The math: three scores
Over the overlap \Omega_t of A at x + t and B at x (with n_t pixels):
SSD and CC differ only by the two energies\sum f^2, \sum g^2 over the overlap, which change with t when the images are uneven: they are not equivalent.
with means over \Omega_t: the cosine of the angle between the two mean-free overlap vectors. Cauchy–Schwarz gives |\mathrm{NCC}| \le 1, with 1 exactly when g = a f + b, a > 0, on the overlap. Replacing g by a g + b (a > 0) leaves NCC unchanged.
Try it: slide B over A
By hand: three scores of one template
Template B = (1, 3, 1) placed at positions t = 0 \ldots 5 of A = (0, 1, 3, 1, 0, 2, 6, 2):
t
0
1
2
3
4
5
pixels of A under B
0 1 3
1 3 1
3 1 0
1 0 2
0 2 6
2 6 2
CC (sum of products)
6
11
6
3
12
22
SSD
9
0
9
10
27
11
NCC
−0.19
1
−0.19
−0.87
−0.19
1
A contains B twice: an exact copy at t = 1 and a copy twice as bright at t = 5.
CC prefers the bright copy, SSD the exact one, NCC calls both perfect.
For tiles that differ in exposure, which one would you trust?
The cost of trying every shift
Two N \times N images, every shift with any overlap: shift t costs one multiply-add per pixel of the overlap, (N - |t_x|)(N - |t_y|).
1024 \times 1024 tiles, every shift: 1024^4 \approx 1.1 \cdot 10^{12} multiply-adds. At about 10^9 per second: 18 minutes for one pair.
If the stage is good to \pm 32 pixels: 65^2 shifts of about 10^6 pixels each, 4.3 \cdot 10^9: about 4 seconds.
A mosaic of 20 \times 20 tiles has 760 neighbouring pairs.
In general: S shifts of about N pixels each cost about N \cdot S; all shifts, S \approx N, cost about N^2 for N pixels. Next unit: all shifts at once for little more than the cost of one.
The math: counting the multiply-adds
For signals of length N, shift t has an overlap of N - |t| samples, so all shifts together cost
\sum_{t=-(N-1)}^{N-1} (N - |t|) = N + 2\sum_{k=1}^{N-1} k = N + N(N-1) = N^2.
In 2D the overlap is (N - |t_x|)(N - |t_y|) and the sum factorises: \sum_{t_x}\sum_{t_y} (N - |t_x|)(N - |t_y|) = N^2 \cdot N^2 = N^4, i.e. the square of the number of pixels. A window |t_x|, |t_y| \le w costs \big(\sum_{|t| \le w}(N - |t|)\big)^2 = \big((2w+1)N - w(w+1)\big)^2 \approx (2w+1)^2 N^2.
Template matching
The same machine with a small image B (the template, e.g. a cell, a bead, a granule) slid over a large image A:
The NCC at every position is a map; its peaks are the places that look like the template, up to brightness and contrast.
Keep the peaks above a threshold: a simple detector. It finds every copy that has the same size and orientation.
The denominators of NCC need the mean and spread of A under the template at every position: local sums, cheap with an FFT (next unit) or with integral images (box filters, the integral-images day).
Try it: template matching
Two kinds of evidence
Template matching (here)
Every pixel of the template takes part in the score.