Without padding, the FFT treats each tile as periodic: a peak at (p, q) only says the shift modulo the tile sizeN.
t_x \in \{p,\ p - N\} and t_y \in \{q,\ q - N\}: four ways tile B can sit against tile A, with very different overlaps.
Decide by computing NCC on the actual overlap of each candidate and keeping the best; ignore candidates whose overlap is too small to judge.
Resolving the candidates
For each candidate, cut out the overlapping parts of A and B and compute their NCC (the matching unit): a direct, cheap check.
Require a minimum overlap: a few dozen pixels always correlate by chance.
In practice, check the candidates of the few highest peaks, not only the first: on small overlaps, the true peak is not always the highest.
The best NCC is also the pair’s confidence.
Alternative: zero-pad the tiles (the cross-correlation unit): no ambiguity, but FFTs four times as large, and the overlap gets a smaller share of the padded image. Stitching programs usually test candidates instead.
From a pair of tiles to a shift, as pseudocode
(p, q) = position of the highest peak of the phase correlation of A and B
for (tx, ty) in (p, q), (p − N, q), (p, q − N), (p − N, q − N):
overlap = the pixels of B that land inside A at (tx, ty)
if the overlap is too small: skip
score = NCC of A and B on the overlap
shift = the candidate with the best score; confidence = that score
A low confidence (an empty or repetitive overlap) means: do not trust this pair.
Sub-pixel shifts
Stages, drift and chromatic shifts are rarely whole pixels. A drift of 0.3 px matters when single molecules are localised to 0.1 px.
Parabola: fit a parabola through the peak and its two neighbours (in x, and separately in y); its top is the estimate. Cheap, but biased towards whole pixels: pixel locking.
Centroid of the peak and its neighbours: simplest, strongly biased for broad or narrow peaks.
Upsampled DFT: evaluate the inverse DFT of the (normalised) cross-power spectrum on a fine grid (e.g. 1/20 px) near the peak only. This is the exact interpolation of the correlation, without a huge padded FFT (Guizar-Sicairos et al., 2008).
To apply a sub-pixel shift: interpolate (bilinear, the images unit) or multiply the spectrum by a phase ramp (the shift theorem, backwards).
The math: sub-pixel peaks
Parabola. Through (-1, c_{-1}), (0, c_0), (1, c_{+1}): c(\delta) = a\delta^2 + b\delta + c_0 with a = \tfrac12(c_{-1} + c_{+1}) - c_0, b = \tfrac12(c_{+1} - c_{-1}); the vertex is at \delta = -\frac{b}{2a} = \frac{c_{-1} - c_{+1}}{2(c_{-1} - 2c_0 + c_{+1})}.The true shape. For a non-integer shift, the phase-correlation peak is a Dirichlet kernel \frac{\sin(\pi(t - t_0))}{N\sin(\pi(t - t_0)/N)} (the phase correlation unit, B7), not a parabola: hence the bias. Fitting the kernel itself works too (Foroosh et al., 2002).
Upsampled DFT.c(x, y) = \frac{1}{N^2}\sum_{k_x,k_y} R_{k_y k_x}e^{2\pi i(k_xx + k_yy)/N} at U \times U points: C = E_y\,R\,E_x^{\top} with small DFT matrices E_x \in \mathbb C^{U\times N}, (E_x)_{jk} = e^{2\pi i k x_j/N}. Cost \mathcal O(N^2U) instead of an FFT of size (UN)^2.
Try it: sub-pixel estimates
Many tiles: the mosaic as a graph
A mosaic of tiles is a graph: one node per tile, one edge per overlapping pair, labelled with the measured shift and its confidence.
Stage positions alone: seams, off by several pixels.
Chaining pairwise shifts along a path: each step adds its error; tiles far down the chain inherit all of them, and one wrong pair moves everything after it.
Around a loop of four tiles, the measured shifts should add up to zero; with noise they do not.
Global alignment: find the tile positions that agree best with all measured shifts at once, weighting each by its confidence. One tile is fixed (the whole mosaic can be moved freely).
Pairs with low confidence (a blank tile, a repetitive pattern) are dropped or down-weighted; a pair whose shift disagrees strongly with the rest (a large residual) is suspicious.
The math: least squares on a graph
Tile positions \mathbf x_1, \ldots, \mathbf x_n, measured shifts \mathbf d_{ij} \approx \mathbf x_j - \mathbf x_i with weights w_{ij}. Minimise, separately in x and in y, E(\mathbf x) = \sum_{(i,j)} w_{ij}\,(x_j - x_i - d_{ij})^2. Setting \partial E/\partial x_m = 0 gives the normal equations L\,\mathbf x = \mathbf b with L_{mm} = \sum_{j \sim m} w_{mj}, \quad L_{mj} = -w_{mj}, \qquad b_m = \sum_{(i,m)} w_{im}d_{im} - \sum_{(m,j)} w_{mj}d_{mj}.L is the weighted graph Laplacian. It is singular: adding a constant to every x_m changes nothing (L\mathbf 1 = 0). Fixing x_1 = 0 (deleting its row and column) makes it invertible when the graph is connected. The graph-cuts day meets the same matrix in normalized cuts.
Try it: stitching a mosaic
Seams and blending
Even well-placed tiles show seams: each tile has its own brightness (exposure, bleaching) and darker corners (vignetting).
Feathering: in the overlap, blend the tiles with weights that fall off towards each tile’s border; the seam becomes a smooth transition.
Vignetting is better corrected before stitching: divide by a flat-field image (a picture of an empty, even sample).
Vignetting is also a registration hazard: the same dark corners in every tile do not move with the shift, like the border jumps of the phase correlation unit.
Drift in a time-lapse
Frame k is shifted by the drift \mathbf d_k since frame 1. Register every frame, then shift it back.
To the first frame: errors do not accumulate, but late frames may no longer look like the first one (the sample changes, cells move, bleaching makes them dim).
To the previous frame: neighbours look alike, but the drift is a sum of steps; each step’s error is added, and the error grows like a random walk (\sqrt{k} times one step’s error).
Key frames: register to a key frame every few frames, and the key frames to each other: a compromise.
Phase correlation ignores bleaching (a gain) and slow illumination changes; noise and real changes of the sample are the limit.
Try it: drift correction
Localisation microscopy and other uses
Single-molecule localisation: there are no images, only lists of molecule positions over thousands of frames. Render the positions of each time block as an image, correlate all pairs of blocks, and solve for one drift per block by least squares, the same graph problem as the mosaic (redundant cross-correlation). Alternative: fiducial beads.
Colour channels: the channels show different structures, so register a calibration image (beads) instead; chromatic aberration also scales the image, so a shift is only a first approximation (measured on beads in colocalisation).
3D stacks: the same formula with a 3D FFT. Report the shift in µm with the voxel spacing (\Delta x, \Delta y, \Delta z), which is usually larger in z (the 3D images unit of the marching-cubes day).
Motion estimation: register small windows separately (particle image velocimetry, video coding).
Try it: drift in localisation microscopy
Limits of a shift
Translation only. Rotation and scale: the Fourier–Mellin method resamples the magnitude spectrum in log-polar coordinates, where they become shifts that phase correlation can find (Reddy & Chatterji, 1996; further reading).
Empty or repeated texture gives no or many answers: check the confidence, use the stage positions as a fallback.
Summary
A circular peak at (p, q) means four shifts; test them by NCC on the overlap and keep the best (with a minimum overlap).
Sub-pixel: the parabola is quick but locks to pixels; the upsampled DFT evaluates the exact peak on a fine grid at little cost.
A mosaic is a graph; pairwise shifts are combined by weighted least squares (a graph Laplacian), which spreads errors instead of accumulating them and exposes wrong pairs by their residuals.
Drift: register to the first frame, to the previous one, or to key frames; the same graph solution corrects localisation microscopy.
A shift cannot describe rotation, scaling or deformation.