A nucleus is a nucleus wherever it lies, however large and however turned. Can we describe its shape with numbers that do not change when it moves, grows or rotates?
Translation: central moments. Scale: normalised moments \eta_{pq}.
Rotation: start with order 2 (trace and eigenvalues), then complex moments make it systematic: Hu’s seven invariants.
Mirror images: one invariant changes sign.
In practice: huge ranges of values, noise, pixels. And the alternative: normalise the pose instead.
Slides titled The math give the formulas and proofs; the ideas are complete without them.
Measure positions from the centroid: \mu_{pq} = \sum (x - \bar x)^p (y - \bar y)^q I (labelling day). Moving the object moves the centroid with it: every \mu_{pq} stays.
No second pass over the image is needed: expand the powers (binomial theorem), for example
Example p = 3, q = 0: \mu_{30} = m_{30} - 3\bar x m_{20} + 3\bar x^2 m_{10} - \bar x^3 m_{00}; with m_{10} = \bar x m_{00} the last two terms combine to 2\bar x^2 m_{10}.
Numerically: the terms are large and nearly cancel, which is one more reason to use coordinates in [-1, 1].
Scale: normalised moments
Make the object s times larger. Each pixel’s distance from the centroid grows by s, and there are s^2 times as many pixels:
\mu'_{pq} = s^{p+q+2}\,\mu_{pq}.
Divide by the right power of the area \mu_{00} (which grows by s^2):
\eta_{pq} = \frac{\mu_{pq}}{\mu_{00}^{\,1 + (p+q)/2}} \quad\text{does not change.}
Rectangle 4 \times 2: \eta_{20} = 1/6. Rectangle 8 \times 4 or 12 \times 6: still 1/6.
Grey images: a contrast change a\,I multiplies every \mu_{pq} by a, so \eta_{pq} changes by a^{-(p+q)/2}. Dividing instead by \mu_{00}\,\sigma^{p+q}, with the spread \sigma^2 = (\mu_{20} + \mu_{02})/\mu_{00}, cancels scale and contrast.
The math: the scaling law
f'(x, y) = f(x/s, y/s) (the image f enlarged s times, about the origin). Substitute x = s u, y = s v, dx\,dy = s^2\,du\,dv:
Linear algebra names them: \phi_1 is the trace of S (scaled), \phi_2 the squared difference of the eigenvalues. Rotation turns the eigenvectors, never the eigenvalues.
Example with \mu_{20} = 10, \mu_{11} = 3, \mu_{02} = 4: rotated by 30° they become 5.90, 4.10, 8.10. The trace stays 14, and (\mu_{20} - \mu_{02})^2 + 4\mu_{11}^2 stays 72.
The disc has the smallest \phi_1 of all shapes: 1/(2\pi) = 0.159. A square has 1/6 = 0.167, the 4 \times 2 rectangle 0.208.
The math: why the trace and the eigenvalue gap survive
Rotating the object by \alpha rotates every pixel position by Q = \begin{psmallmatrix}\cos\alpha & -\sin\alpha\\ \sin\alpha & \cos\alpha\end{psmallmatrix}, and the second-moment matrix becomes M' = Q M Q^\top with M = \begin{psmallmatrix}\mu_{20} & \mu_{11}\\ \mu_{11} & \mu_{02}\end{psmallmatrix}.
\operatorname{tr}(QMQ^\top) = \operatorname{tr}(M Q^\top Q) = \operatorname{tr} M.
QMQ^\top has the same eigenvalues \lambda_1 \ge \lambda_2 as M. With \operatorname{tr} = \lambda_1 + \lambda_2 and \det = \lambda_1\lambda_2: (\mu_{20} - \mu_{02})^2 + 4\mu_{11}^2 = \operatorname{tr}^2 - 4\det = (\lambda_1 - \lambda_2)^2.
Rotation by \alpha multiplies c_{pq} by e^{i(p - q)\alpha}: the arrow c_{pq} in the complex plane turns (p - q) times as fast as the object, and keeps its length.
c_{11} = \sum (x^2 + y^2) I: never turns (p = q). It is \mu_{20} + \mu_{02}.
|c_{pq}|: invariant. So is any product whose turns cancel, e.g. c_{30}\,c_{12}^3 (turns 3\alpha - 3\alpha = 0), c_{20}\,c_{12}^2.
Complex moments as pseudocode
x̄, ȳ = centroid; μ00 = sum of I
for every pixel (x, y):
z = (x − x̄) + i·(ȳ − y) # y up
c[p][q] = c[p][q] + z^p · conj(z)^q · I(x, y)
c[p][q] = c[p][q] / μ00^(1 + (p+q)/2) # scale
invariants: |c20|², |c30|², Re and Im of c30 · c12³, …
One pass over the image for all the c_{pq} needed (orders 2 and 3).
Invariant: magnitudes, and products whose turns add up to 0.
Try it: complex moments as arrows
The math: complex moments and Hu’s invariants
In polar form x + iy = \rho e^{i\theta}: c_{pq} = \iint \rho^{p+q} e^{i(p-q)\theta} f\,dx\,dy. Rotating the image by \alpha replaces \theta by \theta + \alpha for every pixel: c'_{pq} = e^{i(p-q)\alpha} c_{pq}. Real image: c_{qp} = \overline{c_{pq}}.
Hu’s seven invariants (Hu, 1962), with c computed from the normalised moments \eta:
\phi_1 = c_{11}
\phi_2 = \lvert c_{20}\rvert^2
\phi_3 = \lvert c_{30}\rvert^2
\phi_4 = \lvert c_{21}\rvert^2
\phi_5 = \operatorname{Re}(c_{30}\,c_{12}^3)
\phi_6 = \operatorname{Re}(c_{20}\,c_{12}^2)
\phi_7 = \operatorname{Im}(c_{30}\,c_{12}^3)
In real form, e.g. \phi_3 = (\eta_{30} - 3\eta_{12})^2 + (3\eta_{21} - \eta_{03})^2, and \phi_5, \phi_7 are long polynomials of order 12 in \eta; image libraries list them in that form.
Mirror images
A reflection y \to -y turns x + iy into x - iy: every c_{pq} is conjugated.
|c_{pq}| and every real part stay: \phi_1, \dots, \phi_6 do not change.
\phi_7 = \operatorname{Im}(c_{30} c_{12}^3)changes sign: a “skew invariant”.
Use: \phi_7 tells a shape from its mirror image (b from d, a left from a right hand). If mirror images should count as the same, use |\phi_7|.
Three points (3, 1), (-1, 2), (-2, -3): c_{20} = 14i. Rotated by 90°: -14i. Mirrored: also -14i! But c_{30}: 75 + 15i → 15 - 75i (rotated) vs 75 - 15i (mirrored).
Try it: which numbers survive?
In practice
Huge range of values. For the L of the demo: \phi_1 = 0.27, \phi_5 = -5 \times 10^{-7}. Compare features as -\operatorname{sign}(\phi)\log_{10}|\phi|: 0.58 and −6.27.
Noise and pixels. High orders weight the boundary heavily; small or noisy shapes change their high invariants by tens of per cent when rotated. Invariance holds for the continuous image; a digitised shape is only approximately invariant.
Neither independent nor complete.\phi_3 follows from \phi_4, \phi_5, \phi_7; and Hu’s set does not use all the information of order 3. Flusser (2000) gives an independent, complete set built the same way from c_{pq}.
Slanted views: an affine transformation (shear) needs affine moment invariants (Flusser & Suk), named here only.
Pose normalisation instead of invariants
Bring every shape into a standard pose, then compare anything (moments, pixels):
translate the centroid to the origin;
scale to area 1;
rotate the major axis (the labelling day’s orientation) onto x;
fix the remaining 180° ambiguity by the sign of \mu_{30} (heavier side to the left, say).
Fails when \lambda_1 \approx \lambda_2 (square, star, gear: no major axis) or \mu_{30} \approx 0. For a shape with k-fold symmetry, use the phase of c_{k0} instead: it turns by k\alpha.
The snakes day aligns shapes by Procrustes, which needs corresponding landmarks; pose normalisation needs none, at the price of these failure cases.
Major axis angle (labelling day): \theta = \tfrac12 \operatorname{atan2}(2\mu_{11}, \mu_{20} - \mu_{02}); rotating by -\theta makes \mu_{11} = 0 and \mu_{20} \ge \mu_{02}.
After that rotation, if \mu_{30} < 0 rotate by another 180° (\mu_{30} \to -\mu_{30}).
In complex terms: \theta = \tfrac12 \arg c_{20} (defined only when c_{20} \ne 0, i.e. \lambda_1 \ne \lambda_2). For k-fold symmetry all c_{pq} with p - q not a multiple of k vanish; \tfrac1k \arg c_{k0} gives the pose up to the symmetry.
Summary
Translation: central moments \mu_{pq} (from the raw ones by the binomial theorem).
Rotation: complex moments c_{pq} turn by (p - q)\alpha; magnitudes and phase-cancelling products are invariant: Hu’s \phi_1 \dots \phi_7. Order 2: trace and eigenvalue gap of S.
Mirror: c_{pq} is conjugated; only \phi_7 changes sign.
In practice: log-scale the values, expect discretisation errors for small shapes and high orders, or normalise the pose (fails for \lambda_1 \approx \lambda_2).