Exercise: normalisation and moment invariants

Goal

By the end of this exercise you should be able to:

  • say which moments change under translation, scaling, rotation and mirroring, and which do not,
  • compute normalised moments and the first two Hu invariants by hand and check their invariance,
  • use complex moments: predict how fast each one turns when the object rotates, and what a mirror does,
  • explain the practical problems: ranges of values, digitisation, dependence, and when pose normalisation fails,
  • (math) derive the binomial formula for central moments, the scaling law, the rotation rule for c_{pq} and the mirror behaviour of \phi_7.

Background

Idea What it means
Central moments \mu_{pq}: positions measured from the centroid; translation invariant
Normalised moments \eta_{pq} = \mu_{pq}/\mu_{00}^{1 + (p+q)/2}; translation and scale invariant
Order-2 invariants \phi_1 = \eta_{20} + \eta_{02} (trace), \phi_2 = (\eta_{20} - \eta_{02})^2 + 4\eta_{11}^2 (squared eigenvalue gap)
Complex moments c_{pq} = \sum (x + iy)^p (x - iy)^q I, central coordinates, y up; rotation by \alpha: c_{pq} \to e^{i(p-q)\alpha} c_{pq}; mirror y \to -y: c_{pq} \to \overline{c_{pq}}
Hu’s invariants (from \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)
Pose normalisation centroid → origin, area → 1, major axis → x, \mu_{30} > 0

By hand

B1. The filled rectangle 4 \times 2 (the previous unit: \mu_{20} = a^3 b/12, \mu_{02} = a b^3/12, \mu_{00} = ab). (a) Compute \eta_{20}, \eta_{02}, \phi_1 and \phi_2. (b) The rectangle is enlarged 3 times (12 \times 6). Compute \mu_{20} and \eta_{20} again.

B2. An object has \mu_{20} = 10, \mu_{11} = 3, \mu_{02} = 4 (any units). A rotation by \alpha gives \mu'_{20} = c^2\mu_{20} - 2cs\,\mu_{11} + s^2\mu_{02}, \mu'_{02} = s^2\mu_{20} + 2cs\,\mu_{11} + c^2\mu_{02}, \mu'_{11} = cs(\mu_{20} - \mu_{02}) + (c^2 - s^2)\mu_{11} with c = \cos\alpha, s = \sin\alpha. (a) Compute the rotated moments for \alpha = 90° and \alpha = 30°. (b) Check that \mu_{20} + \mu_{02} and (\mu_{20} - \mu_{02})^2 + 4\mu_{11}^2 are the same in all three cases. (c) Compute the eigenvalues of \begin{psmallmatrix}10 & 3\\3 & 4\end{psmallmatrix} and relate them to (b).

B3. Three equal point masses at (3, 1), (-1, 2), (-2, -3) (y up). (a) Check that the centroid is the origin. Compute \mu_{20}, \mu_{11}, \mu_{02}, then c_{11} and c_{20} = \mu_{20} - \mu_{02} + 2i\mu_{11}. (b) Rotate the points by 90° counter-clockwise, (x, y) \to (-y, x). Compute c_{20} directly and compare with the rule c_{20} \to e^{2i\alpha} c_{20}. (c) Mirror the original points, (x, y) \to (x, -y). Compute c_{20}. Can c_{20} tell the rotated set from the mirrored set? (d) Given c_{30} = 75 + 15i for the original points, predict c_{30} of the rotated and of the mirrored set. Which of the two sets has the same c_{30}c_{12}^3 as the original?

B4. \phi_1 for a disc is 1/(2\pi), for a square 1/6, for an ellipse with axes ratio 2:1 it is 0.199, for 4:1 0.338. Order the shapes by \phi_1 and explain the order in one sentence. Which \phi_2 is zero?

B5. Four invariants have the values 0.2, 3 \times 10^{-3}, 10^{-6} and -2 \times 10^{-10}. Compute -\operatorname{sign}(\phi)\log_{10}|\phi| for each. Why is this transform used before computing distances between shapes?

B6. A classifier uses \phi_1, \dots, \phi_6 to recognise the digits 0–9 written in any orientation. Which digits will it confuse because of the invariance alone? Would adding \phi_7 help?

B7. Pose normalisation (centroid, area, major axis, sign of \mu_{30}). (a) Which steps work for a 4 \times 2 rectangle, and which ambiguity remains? (b) Why does it fail for a square? For a three-armed star, which complex moment would you use to fix the angle, and why?

Math questions

B8. (math) Derive \mu_{30} = m_{30} - 3\bar x m_{20} + 2\bar x^2 m_{10} from the binomial theorem.

B9. (math) Show \mu'_{pq} = s^{p+q+2}\mu_{pq} for f'(x, y) = f(x/s, y/s) and deduce that \eta_{pq} is scale invariant. Under which normalisation are moments of a grey image also invariant to a contrast change f \to af?

B10. (math) Write x + iy = \rho e^{i\theta} and show that a rotation of the image by \alpha multiplies c_{pq} by e^{i(p-q)\alpha}. Show c_{qp} = \overline{c_{pq}} for a real image.

B11. (math) Show that a reflection y \to -y conjugates every c_{pq}. Deduce that \phi_1, \dots, \phi_6 are unchanged and \phi_7 changes sign. Then show \phi_3\,\phi_4^3 = \phi_5^2 + \phi_7^2: Hu’s set is not independent.

B12. (math) Show that \phi_2 = (\lambda_1 - \lambda_2)^2/\mu_{00}^2, where \lambda_1, \lambda_2 are the eigenvalues of the matrix S = \frac{1}{\mu_{00}}\begin{psmallmatrix}\mu_{20} & \mu_{11}\\ \mu_{11} & \mu_{02}\end{psmallmatrix}.

B1. (a) \mu_{20} = 64 \cdot 2/12 = 10.67, \mu_{02} = 4 \cdot 8/12 = 2.67, \mu_{00} = 8. \eta_{20} = 10.67/64 = 1/6 = 0.167, \eta_{02} = 2.67/64 = 1/24 = 0.042. \phi_1 = 5/24 = 0.208, \phi_2 = (1/6 - 1/24)^2 = (1/8)^2 = 1/64 = 0.0156. (b) \mu_{20} = 12^3 \cdot 6/12 = 864 (= 3^4 \cdot 10.67), \mu_{00} = 72, \eta_{20} = 864/72^2 = 1/6: unchanged.

B2. (a) 90° (c = 0, s = 1): \mu'_{20} = 4, \mu'_{02} = 10, \mu'_{11} = -3. 30°: \mu'_{20} = 5.90, \mu'_{02} = 8.10, \mu'_{11} = 4.10. (b) Trace: 14, 14, 14. (\mu_{20} - \mu_{02})^2 + 4\mu_{11}^2: 36 + 36 = 72; 36 + 36 = 72; (-2.20)^2 + 4 \cdot 4.10^2 = 4.83 + 67.17 = 72. (c) \lambda = 7 \pm \sqrt{9 + 9} = 7 \pm 4.243: 11.24 and 2.76. Their sum is the trace 14; (\lambda_1 - \lambda_2)^2 = 8.485^2 = 72.

B3. (a) (3 - 1 - 2, 1 + 2 - 3)/3 = (0, 0). \mu_{20} = 9 + 1 + 4 = 14, \mu_{02} = 1 + 4 + 9 = 14, \mu_{11} = 3 - 2 + 6 = 7. c_{11} = 28, c_{20} = 0 + 14i = 14i. (b) Rotated points (-1, 3), (-2, -1), (3, -2): \mu_{20} = 14, \mu_{02} = 14, \mu_{11} = -3 + 2 - 6 = -7, so c_{20} = -14i. Rule: e^{2i \cdot 90°} = e^{i\pi} = -1, -1 \cdot 14i = -14i. ✓ (c) Mirrored points (3, -1), (-1, -2), (-2, 3): \mu_{11} = -7, c_{20} = -14i = \overline{14i}. The same as the rotated set: c_{20} cannot tell them apart. (d) Rotated: c_{30} \to e^{3i \cdot 90°}(75 + 15i) = -i(75 + 15i) = 15 - 75i. Mirrored: \overline{75 + 15i} = 75 - 15i. Different, so order 3 separates them. c_{30}c_{12}^3 turns by 3\alpha - 3\alpha = 0: the rotated set has the same product as the original (183180 - 493920i); the mirrored set has its conjugate (183180 + 493920i): same \phi_5, opposite \phi_7.

B4. Disc 0.159 < square 0.167 < ellipse 2:1 0.199 < ellipse 4:1 0.338. \phi_1 is the mean squared distance from the centroid relative to the area: the more the mass is spread out away from the centre, the larger. The disc and the square have \phi_2 = 0 (\lambda_1 = \lambda_2); the ellipses have \phi_2 = 0.014 and 0.089.

B5. -\log_{10} 0.2 = 0.70; 2.52; 6.00; for the negative value +\log_{10}(2 \times 10^{-10}) = -9.70. Raw values span 9 orders of magnitude: in a Euclidean distance the largest feature would dominate and the small ones would not count. The log puts them on comparable scales; the sign keeps the information that \phi was negative.

B6. 6 and 9 are the same digit rotated by 180° (in many fonts and hands): identical invariants. Because \phi_1 \dots \phi_6 are also mirror invariant, mirror pairs collapse too; in a seven-segment display 2 and 5 are exact mirror images. \phi_7 separates mirror pairs (2 from 5), but not 6 from 9, which is a pure rotation.

B7. (a) Centroid, area and major axis (the long side) work. After rotating the long side onto x, \mu_{30} = 0 (the rectangle is symmetric), so the 180° ambiguity remains, and also the mirror ambiguity; for a rectangle these do not matter (the rotated copy is identical), but for a nearly rectangular shape the choice will flip with noise. (b) For a square \lambda_1 = \lambda_2: no major axis. A three-armed star has c_{20} = c_{21} = c_{12} = 0 by symmetry (see the quiz), but c_{30} \ne 0; it turns by 3\alpha, so \tfrac13\arg c_{30} fixes the angle up to the star’s own symmetry (multiples of 120°).

B8. (x - \bar x)^3 = x^3 - 3\bar x x^2 + 3\bar x^2 x - \bar x^3. Summing with I: \mu_{30} = m_{30} - 3\bar x m_{20} + 3\bar x^2 m_{10} - \bar x^3 m_{00}. With \bar x m_{00} = m_{10}: 3\bar x^2 m_{10} - \bar x^2 m_{10} = 2\bar x^2 m_{10}.

B9. Substituting x = su, y = sv: \mu'_{pq} = \iint (su)^p (sv)^q f(u, v)\,s^2\,du\,dv = s^{p+q+2}\mu_{pq} (central coordinates scale the same way). \eta'_{pq} = s^{p+q+2}\mu_{pq}/(s^2\mu_{00})^{1 + (p+q)/2} = \eta_{pq}. A contrast change multiplies every \mu_{pq} by a, so \eta_{pq} by a^{-(p+q)/2}. Dividing by \mu_{00}\,\sigma^{p+q} with \sigma^2 = (\mu_{20} + \mu_{02})/\mu_{00} removes both: under scaling the numerator gets s^{p+q+2} and the denominator s^2 \cdot s^{p+q}; under contrast both get a.

B10. (x + iy)^p (x - iy)^q = \rho^{p+q} e^{i(p-q)\theta}. The rotated image f' has at (\rho, \theta + \alpha) the value f had at (\rho, \theta), so c'_{pq} = \iint \rho^{p+q} e^{i(p-q)(\theta + \alpha)} f = e^{i(p-q)\alpha} c_{pq} (the area element \rho\,d\rho\,d\theta is unchanged). For real f: \overline{c_{pq}} = \iint \rho^{p+q} e^{-i(p-q)\theta} f = c_{qp}.

B11. Reflection maps x + iy to x - iy, so (x + iy)^p (x - iy)^q \to (x - iy)^p (x + iy)^q = \overline{(x + iy)^p (x - iy)^q} and c_{pq} \to \overline{c_{pq}}. Magnitudes and real parts of products are unchanged (\phi_1 \dots \phi_6); \operatorname{Im}(\overline{c_{30}}\,\overline{c_{12}}^3) = -\operatorname{Im}(c_{30}c_{12}^3). Dependence: \phi_5^2 + \phi_7^2 = |c_{30}c_{12}^3|^2 = |c_{30}|^2 |c_{12}|^6 = \phi_3 \phi_4^3 (with |c_{12}| = |c_{21}|).

B12. With \eta = \mu/\mu_{00}^2 for order 2 and S = \mu/\mu_{00}: \phi_2 = \big((S_{11} - S_{22})^2 + 4S_{12}^2\big)/\mu_{00}^2 = \big((\operatorname{tr} S)^2 - 4\det S\big)/\mu_{00}^2 = (\lambda_1 - \lambda_2)^2/\mu_{00}^2.

Explore: complex moments as arrows

A shape on the shape canvas, rotated by \alpha about its centroid (slider, or play: 30° per second). Six small complex planes show c_{20}, c_{11}, c_{30}, c_{21}, c_{12} (of the normalised moments, y up) and the product c_{30}c_{12}^3; the orange trail is the path of the arrow tip. Each dial is scaled to the size its moment had when the shape was loaded (relative to c_{11}), so a moment that is zero shows as a dot. mirror ↕ reflects the shape top–bottom. You can also drag the shape’s corners.

P1. Preset rotate slowly (press play, then pause). Set \alpha to 0°, 45° and 90° and read the arguments of c_{20}, c_{30}, c_{21}, c_{12}. Check them against (p - q)\alpha.

P2. Preset mirror: the shape is reflected once. What happens to every arrow? What happens to the product, and which Hu invariant is its imaginary part?

P3. Preset 3-fold symmetry. Which arrows are (almost) zero? Rotate by 120°: why do all arrows come back to where they started? Compare with B7(b).

P4. Preset a rectangle. Which moments vanish, and why? Rotate by 90°: what does c_{20} do?

P1. At 0°: \arg c_{20} = 67°, \arg c_{30} = 2°, \arg c_{21} = -41°, \arg c_{12} = 41°. At 45°: 157° (+90°), 137° (+135°), 3° (\approx +45°), -3° (\approx -45°). At 90°: -113° (i.e. 247°, +180°), -88° (i.e. 272°, +270°), 48° (+90° up to a degree), -48°. c_{11} stays at 0° and the product at about 126°–127°: the small deviations are digitisation.

P2. Every arrow is reflected in the real axis: c_{20} goes from 67° to -67°, c_{30} from 2° to -2°, c_{21} and c_{12} swap their arguments (\pm 41°), c_{11} stays. The product goes from 126° to -126°: its real part (\phi_5) stays, its imaginary part (\phi_7) changes sign.

P3. c_{20}, c_{21} and c_{12} are almost 0 (0.0017 and 0.0006 of c_{11} to the matching power; not exactly 0 because of the pixels), and so is the product. Only c_{11} and c_{30} remain. After 120°, c_{30} has turned by 3 \cdot 120° = 360°: it is back, as it must be, because the shape itself is unchanged.

P4. All order-3 moments vanish: the rectangle is point symmetric (a 180° rotation leaves it unchanged, which flips every odd-order moment, so they must be 0). c_{20} turns by 2 \cdot 90° = 180°: from 46° to -134°; it changes sign.

Explore: which numbers survive?

The shape canvas again, now with a reference shape (orange, dashed) and the current shape (blue). Each preset sets a reference and then transforms the current shape; set reference freezes the current shape as the new reference. Both are digitised on the same pixel grid (the pixel-size slider re-digitises both). The table gives raw m, central \mu, normalised \eta and Hu’s \phi_1 \dots \phi_7 for both, with the relative change (green below 1 %, red from 10 %). The bar plot shows -\operatorname{sign}(\phi)\log_{10}|\phi|. The inset draws both shapes after pose normalisation. grey edges uses the fraction of each pixel covered by the shape instead of a 0/1 mask.

E1. Preset translate. Which rows change, and by how much? Why are the changes of \mu, \eta and \phi exactly 0 here?

E2. Preset scale. Which rows change? Read the change of \eta_{20} and \phi_1. Why are they not exactly 0?

E3. Preset rotate (50°). Which rows change a lot, which little? Read the changes of \phi_1, \phi_2, \phi_4 and \phi_7. Then tick grey edges: what happens to the changes? Finally set the pixel size to 2 (grey edges off). Explain.

E4. Preset mirror. Which invariant changes, and how? Look at the bar plot.

E5. Preset small shape: an L of 30 % size, rotated by 45°. Read the changes of \phi_1, \phi_2 and \phi_5. Then set the pixel size to 0.5 and 0.25, with and without grey edges. What do you conclude about invariants of small objects?

E6. Preset square. What does the inset say, and why? Which invariants are 0? Rotate the square in 15° steps with set reference in between: does pose normalisation work?

E1. Only the raw moments change: m_{10} from 1.735 \times 10^4 to 2.234 \times 10^4 (22 %), m_{20} from 5.18 \times 10^5 to 8.35 \times 10^5. The shift is a whole number of pixels (8, 6), so the mask is exactly the same set of pixels, moved: every central moment is identical.

E2. m and \mu change (\mu_{20} from 1.12 \times 10^4 to 8.62 \times 10^4, about (1.25/0.75)^4 = 7.7 times); \eta and \phi stay: \eta_{20} 0.0905 → 0.0907, \phi_1 changes by 0.11 %. Not exactly 0, because the two sizes are digitised differently: the small L’s edges do not fall on pixel boundaries.

E3. \mu and \eta change completely (\eta_{20}: 0.0906 → 0.197, \eta_{30} changes sign). \phi_1: 0.019 %, \phi_2: 0.45 %, \phi_3: 0.34 %, \phi_4: 2.8 %, \phi_5: 0.73 %, \phi_6: 1.1 %, \phi_7: 4.5 %. With grey edges every change is below 0.5 % (largest 0.44 % for \phi_6). At pixel size 2 (binary) the changes grow to 0.9 %–17 %. Invariance holds for the continuous shape; the rotated L has staircase edges, and the higher invariants (small numbers made of high powers of the boundary) are the most sensitive. Partial pixels (grey edges) and finer pixels reduce the error.

E4. Only \phi_7 changes: -1.72 \times 10^{-6} becomes +1.72 \times 10^{-6} (the table shows ≥ 100 %); the bar jumps from -5.77 to +5.77. \phi_1 \dots \phi_6 are unchanged (to 13 digits: a mirror maps the pixel grid onto itself).

E5. At pixel size 1 (binary): \phi_1 3.3 %, \phi_2 13 %, \phi_5 170 % (it changes sign). At 0.5: 2.2 %, 0.15 %, 99 %; at 0.25: 4.4 %, 16 %, 20 %. With grey edges: 4.1 %, 14 %, 31 % (pixel size 1), 0.48 %, 2.5 %, 13 % (0.5), 0.046 %, 1.2 %, 8.8 % (0.25). The arms of the small L are only 3.6 canvas units wide, so even at pixel size 0.25 rounding the width to whole pixels changes it by a few per cent. Invariants of small objects are unreliable, high-order ones the most; partial pixels help a lot.

E6. “ambiguous pose”: \lambda_1 = \lambda_2 (47.92 both), so the major axis is undefined, and \mu_{30} = 0. All invariants except \phi_1 are 0 (to rounding): the square is symmetric under 90° rotation, so c_{20}, c_{30}, c_{21} vanish. Pose normalisation does not work: the inset angle is arbitrary, decided by rounding. A square needs the phase of c_{40} (4-fold symmetry).

Code

Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in moments/src/moment_invariants/exercise.rs. The raw moments of the previous unit are given (raw_moments).

C1. central_from_raw(m): all central moments from the raw ones, without a second pass over the image (the binomial theorem):

x̄ = m10 / m00,  ȳ = m01 / m00
for every p, q with p + q ≤ N:
    μ[p][q] = sum over k ≤ p, l ≤ q of  C(p, k) · C(q, l) · (−x̄)^(p−k) · (−ȳ)^(q−l) · m[k][l]

The checks compare it with the direct sum and with the formula for \mu_{30} of B8.

C2. normalised(mu): \eta_{pq} = \mu_{pq} / \mu_{00}^{1 + (p+q)/2}. The check is B1: the 4 \times 2 and the 12 \times 6 rectangle both have \eta_{20} = 1/6.

C3. complex_moment(img, p, q): the normalised complex moment, with y pointing up:

for every pixel (x, y):
    z = (x − x̄) + i·(ȳ − y)                  # y up
    c = c + z^p · conj(z)^q · I(x, y)
c = c / μ00^(1 + (p+q)/2)

The checks are B3 (the three points: c_{20} = 14i, c_{11} = 28, c_{30} = 75 + 15i before dividing by 3^{1 + (p+q)/2}), and that a 90° turn of the image multiplies c_{pq} by i^{\,p-q}.

C4. hu(img): Hu’s seven invariants from c_{11}, c_{20}, c_{30}, c_{21}, c_{12} (the table on the slide “The math: complex moments and Hu’s invariants”). The checks: the product c_{30}c_{12}^3 of B3(d), a mirror image (only \phi_7 changes, its sign), a 90° turn and a copy twice as large.

Then cargo run -p moments -- moment-invariants. It draws an L of 60 pixels, prints its invariants on the log scale of the slide “In practice”, and then the relative change of \phi_1 \dots \phi_7 when the L is moved, scaled, rotated by 50° or mirrored, drawn as a 0/1 mask or with grey edges (the fraction of each pixel covered). The shapes are in out/moment-invariants/shapes.png.

C5. Which changes are exactly 0, and why? Which are not, although the invariants are invariant? What do grey edges do?

C6. Compare the small L (18 pixels, rotated) with E5 of the demo. Which invariant is the least reliable here, and why?

The model solution is moments/src/moment_invariants/solution.rs in the download.

C5. Moving by whole pixels and mirroring give exactly the same set of pixels (moved or reflected), so the changes are 0.00 %, except \phi_7 under the mirror: it changes sign (3.54 \times 10^{-6} becomes -3.54 \times 10^{-6}, shown as 200 %). Scaling (to 45 pixels) and rotating (50°) draw a new staircase outline, which is not an exact copy: as 0/1 masks \phi_1 changes by 0.58 % and 0.15 %, but \phi_4 by 14 % and 0.6 %, and \phi_5, the smallest invariant (10^{-6.76}), by 405 % and 7.9 %. With grey edges the scaled L stays within 0.4 % except \phi_5 (5.2 %), \phi_6 (1.7 %) and \phi_7 (0.6 %), and the rotated one within 0.1 %. The invariance holds for the continuous shape; the pixels break it, high orders most.

C6. At 18 pixels the arms are only 5 pixels wide: \phi_1 changes by 2.4 %, \phi_2 and \phi_4 by about 16 %, \phi_5 by 172 % and \phi_6 by 81 %, as in E5. \phi_5 is a product of four third-order moments that nearly cancel (10^{-6.76} for this L): a small change of each moment is a large change of the product. Small objects need low orders, grey edges, or both.

Where this leads

Invariants are built from the monomial moments, and the next unit shows why monomials are a poor basis: they look alike, their moments are redundant, and getting the image back from them is ill-conditioned. Legendre polynomials fix that on a square; Zernike polynomials do it on a disc, where rotation invariance comes for free.