Parametric alignment

When the whole image moves in one way, a few parameters describe the motion of every pixel: a shift, a rotation, an affine or a projective transformation. Estimate them all at once, with the same steps as Lucas–Kanade, and watch the error image shrink. Then pick a model that is too simple for the motion and see what is left over.

Frame I0I_0 the pixels xi\mathbf{x}_i that are compared
Frame I1I_1 frame of I0I_0 moved by f(x;pk)f(\mathbf{x}; \mathbf{p}_k) true motion
Step : I1I_1 warped back and the error gray = zero error xi′\mathbf{x}'_i outside I1I_1, not counted
I1(f(xi;pk))I_1(f(\mathbf{x}_i; \mathbf{p}_k))
error eie_i at step kk
error at p0=0\mathbf{p}_0 = \mathbf{0}
Steepest-descent images J1=∇I1 ∂x′/∂p\mathbf{J}_1 = \nabla I_1 \, \partial \mathbf{x}' / \partial \mathbf{p} at step one image per parameter, gray = 0, each scaled to its own range
Corner error over the iterations kk distance of the moved image corners to the true ones, in pixels
Intensity error E(pk)\sqrt{E(\mathbf{p}_k)} root mean square of eie_i over the pixels that count

One motion for the whole image

Lucas–Kanade moves a small window by a shift u\mathbf{u}. When the camera pans, rotates or zooms, or when the scene is a single plane, every pixel follows one transformation x′=f(x;p)\mathbf{x}' = f(\mathbf{x}; \mathbf{p}) with a few parameters p\mathbf{p}. They are estimated from all pixels at once by minimizing

E(p)=∑i[I1(f(xi;p))−I0(xi)]2=∑iei2.E(\mathbf{p}) = \sum_i \left[ I_1(f(\mathbf{x}_i; \mathbf{p})) - I_0(\mathbf{x}_i) \right]^2 = \sum_i e_i^2 .

Pixels whose xi′\mathbf{x}'_i falls outside I1I_1 have nothing to be compared with and are left out. Since their number changes with p\mathbf{p}, the demo reports the mean of ei2e_i^2 over the pixels that count.

Motion models

The models are the 2D transformations, written so that p=0\mathbf{p} = \mathbf{0} is the identity. Coordinates are measured from the image center, so that rotation and scaling turn around the middle of the image:

The chain rule

As in Lucas–Kanade, I1I_1 is linearized around the current estimate, ei(p+Δp)≈ei+J1(xi) Δpe_i(\mathbf{p} + \Delta\mathbf{p}) \approx e_i + \mathbf{J}_1(\mathbf{x}_i)\, \Delta\mathbf{p}. The derivative of the error with respect to the parameters has two factors: how the image changes when x′\mathbf{x}' moves, and how x′\mathbf{x}' moves when p\mathbf{p} changes,

J1(xi)=∂I1∂x′∂x′∂p=∇I1(xi′) ∂f∂p(xi;p)(1×2 times 2×n).\mathbf{J}_1(\mathbf{x}_i) = \frac{\partial I_1}{\partial \mathbf{x}'} \frac{\partial \mathbf{x}'}{\partial \mathbf{p}} = \nabla I_1(\mathbf{x}'_i)\, \frac{\partial f}{\partial \mathbf{p}}(\mathbf{x}_i; \mathbf{p}) \qquad (1 \times 2 \text{ times } 2 \times n) .

For the affine model, for example,

∂x′∂p=[10xy000100xy],J1=[IxIyxIxyIxxIyyIy].\frac{\partial \mathbf{x}'}{\partial \mathbf{p}} = \begin{bmatrix} 1 & 0 & x & y & 0 & 0 \\ 0 & 1 & 0 & 0 & x & y \end{bmatrix}, \qquad \mathbf{J}_1 = \begin{bmatrix} I_x & I_y & x I_x & y I_x & x I_y & y I_y \end{bmatrix} .

Each entry of J1\mathbf{J}_1, taken over the whole image, is one of the steepest-descent images: the change of the warped image when one parameter grows a little. The images for txt_x and tyt_y are just the gradients; the others are the gradients weighted by the position, which is why the corners of the image carry most of the information about rotation and scaling.

Gauss–Newton with nn unknowns

Minimizing ∑i[J1(xi) Δp+ei]2\sum_i [\mathbf{J}_1(\mathbf{x}_i)\, \Delta\mathbf{p} + e_i]^2 is a least-squares problem in Δp\Delta\mathbf{p}. Its normal equations are an n×nn \times n system, no matter how many pixels there are:

A Δp=b,A=∑iJ1⊤J1,b=−∑iei J1⊤,pk+1=pk+Δp.A\, \Delta\mathbf{p} = \mathbf{b}, \qquad A = \sum_i \mathbf{J}_1^\top \mathbf{J}_1, \qquad \mathbf{b} = -\sum_i e_i\, \mathbf{J}_1^\top , \qquad \mathbf{p}_{k+1} = \mathbf{p}_k + \Delta\mathbf{p} .

For translation, AA is the 2×22 \times 2 matrix of Lucas–Kanade summed over the whole image. The iteration stops when an update moves the image corners by less than 0.001 px.

Choosing the model

A model with too few parameters cannot reach zero error: the best it can do is a compromise, and the rest stays visible in the error image as a pattern that grows towards the image border. A model with more parameters than the motion needs can express the true motion too, but its extra parameters also follow whatever the images do not share, such as noise and interpolation errors, and every step costs more. The simplest model that describes the motion is usually the most accurate.

Limits

Try this