Exercise: Zernike moments on the unit disc

Goal

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

  • list the Zernike polynomials of low order, sketch them, and name the ones used in optics,
  • check the orthogonality of the radial polynomials with the weight \rho,
  • predict what a rotation or a mirror does to Z_{nm}, and why |Z_{nm}| is invariant,
  • name what the magnitudes lose (6 and 9) and the discretisation and arithmetic errors of Zernike moments,
  • (math) prove the rotation rule and express low-order Zernike moments in geometric moments.

Background

Idea What it means
Zernike polynomials V_{nm} = R_{nm}(\rho)e^{im\theta}, n \ge 0, \lvert m\rvert \le n, n - \lvert m\rvert even
Small radial polynomials R_{00} = 1, R_{11} = \rho, R_{20} = 2\rho^2 - 1, R_{22} = \rho^2, R_{31} = 3\rho^3 - 2\rho, R_{33} = \rho^3, R_{40} = 6\rho^4 - 6\rho^2 + 1
Orthogonality \iint \overline{V_{nm}}V_{n'm'}\,dA = \frac{\pi}{n+1}\delta_{nn'}\delta_{mm'}; radial: \int_0^1 R_{nm}R_{n'm}\rho\,d\rho = \frac{1}{2(n+1)}\delta_{nn'}
Zernike moments Z_{nm} = \frac{n+1}{\pi}\iint I\,\overline{V_{nm}}\,dA; real image: Z_{n,-m} = \overline{Z_{nm}}
Rotation by \alpha Z_{nm} \to Z_{nm}e^{-im\alpha}; \lvert Z_{nm}\rvert invariant
Mirror top–bottom: Z_{nm} \to \overline{Z_{nm}}; left–right: Z_{nm} \to (-1)^m\overline{Z_{nm}}

By hand

B1. Orthogonality of the radial polynomials. (a) Compute \int_0^1 R_{20}R_{00}\,\rho\,d\rho and \int_0^1 R_{31}R_{11}\,\rho\,d\rho. (b) Compute \int_0^1 R_{20}^2\,\rho\,d\rho and compare with \frac{1}{2(n+1)}. (c) Where is the zero of R_{20}? Sketch R_{20}, R_{40} and R_{22} on [0, 1].

B2. An image has Z_{22} = 0.3 + 0.4i. (a) What are |Z_{22}| and its phase? (b) The image is rotated by 30° counter-clockwise. Compute the new Z_{22} (magnitude, phase, and as a + bi). (c) The original image is mirrored top–bottom instead. What is Z_{22} now?

B3. Counting. (a) List all (n, m) with n \le 4. How many Zernike polynomials is that, and how many with m \ge 0? (b) How many distinct moments (with m \ge 0) are there up to n = 20? How many real numbers is that (note Z_{n0} is real)?

B4. The disc inscribed in a 64 \times 64 image. (a) What fraction of the pixels lies inside it? (The demo counts 3228 pixel centres.) (b) The image is a crop of a larger micrograph with bright background everywhere. What does a Zernike reconstruction do in the corners? (c) What are the alternatives, and their drawbacks?

B5. Optics. The wavefront error over a round lens pupil is written as W(\rho, \theta) = \sum a_{nm}V_{nm}. (a) Which term is defocus, and what shape does it describe? (b) Which terms describe astigmatism? How many times does the pattern repeat around the pupil? (c) Why is V_{11} (tilt) usually not counted as an aberration of the image?

B6. 6 and 9, b and d. (a) Why do a 6 and a 9 have the same |Z_{nm}| for every n, m? (b) How do the phases of Z_{11} and Z_{22} differ between them? (c) Do b and d have the same magnitudes? Which feature could separate them?

B7. The factorial formula for R_{40,0} has terms up to about 5 \times 10^{13} in size, while |R_{40,0}| \le 1. A double has about 16 significant digits. (a) Estimate the absolute error of R_{40,0} computed with that formula. (b) The demo library compares it with a recurrence: the largest difference on [0, 1] is 1.9 \times 10^{-10} at n = 20, 7.7 \times 10^{-7} at n = 30, 0.015 at n = 40 and 73 at n = 50. Which orders are safe with the factorial formula?

Math questions

B8. (math) Using \int_0^{2\pi}e^{i(m'-m)\theta}d\theta = 2\pi\delta_{mm'} and the radial orthogonality, show \iint \overline{V_{nm}}V_{n'm'}\,\rho\,d\rho\,d\theta = \frac{\pi}{n+1}\delta_{nn'}\delta_{mm'}, and that \hat I = \sum Z_{nm}V_{nm} with the given Z_{nm} is the orthogonal projection.

B9. (math) Show that for a real image Z_{n,-m} = \overline{Z_{nm}}, and that a rotation by \alpha multiplies Z_{nm} by e^{-im\alpha}.

B10. (math) In disc coordinates (x = \rho\cos\theta, y = \rho\sin\theta), express Z_{00}, Z_{11} and Z_{20} in geometric moments m_{pq}. What do Z_{11} and Z_{20} measure?

B11. (math) Show that a left–right mirror x \to -x maps Z_{nm} to (-1)^m\overline{Z_{nm}}.

B1. (a) \int_0^1 (2\rho^2 - 1)\rho\,d\rho = \tfrac12 - \tfrac12 = 0. \int_0^1 (3\rho^3 - 2\rho)\rho\cdot\rho\,d\rho = \int_0^1 (3\rho^5 - 2\rho^3)\,d\rho = \tfrac12 - \tfrac12 = 0. (b) \int_0^1 (4\rho^5 - 4\rho^3 + \rho)\,d\rho = \tfrac23 - 1 + \tfrac12 = \tfrac16 = \frac{1}{2 \cdot 3}. ✓ (c) 2\rho^2 - 1 = 0 at \rho = 1/\sqrt2 = 0.707: R_{20} goes from -1 at the centre to +1 at the rim. R_{40} starts at 1, dips to -0.5 at \rho^2 = 1/2 and returns to 1 at the rim (two zeros, at \rho = 0.46 and 0.89). R_{22} = \rho^2 rises from 0 to 1 without a zero.

B2. (a) |Z_{22}| = 0.5, phase 53.1°. (b) The phase changes by -2 \cdot 30° = -60°: magnitude 0.5, phase -6.9°, Z_{22} = 0.496 - 0.060i. (c) Top–bottom mirror conjugates: 0.3 - 0.4i (phase -53.1°), magnitude still 0.5.

B3. (a) (0,0); (1,\pm1); (2,0), (2,\pm2); (3,\pm1), (3,\pm3); (4,0), (4,\pm2), (4,\pm4): 15 polynomials, 9 with m \ge 0. (b) (20 + 2)^2/4 = 121 moments with m \ge 0. Real numbers: the 11 moments with m = 0 are real, the 110 others complex: 11 + 220 = 231 = (N+1)(N+2)/2, the same count as the Legendre moments up to order 20.

B4. (a) 3228/4096 = 78.8\,\% (the continuous value is \pi/4 = 78.5\,\%). (b) The basis is only defined on the disc: the reconstruction is 0 (or undefined) in the corners, a large error there. (c) A circumscribed disc covers the whole square, but a quarter of its area (1 - 2/\pi = 36\,\%) lies outside the image: those parts are treated as 0, which looks like a sharp edge and causes ringing. Or crop/mask the object so that it lies inside the disc on a dark background, which is the usual choice for shape description.

B5. (a) V_{20} = 2\rho^2 - 1: a paraboloid, the wavefront curved like a lens that is too strong or too weak; the constant -1 only shifts it so that its mean over the pupil is 0. (b) V_{2,\pm2} = \rho^2\cos 2\theta and \rho^2\sin 2\theta: a saddle, the focus differs between two perpendicular directions; the pattern repeats twice around the pupil. (c) A tilt of the wavefront moves the whole image sideways without blurring it; it is a shift, not a loss of sharpness.

B6. (a) A 9 is a 6 rotated by 180°, and rotation changes only phases. (b) Z_{11} turns by -180° (sign change), Z_{22} by -360° \equiv 0°: unchanged. In general odd m change sign, even m stay. (c) b and d are mirror images: mirroring conjugates the moments (up to the sign (-1)^m), so the magnitudes are the same. A phase difference, for example the phase of Z_{31} relative to that of Z_{11} (a combination that is rotation invariant but changes under mirroring, like Hu’s \phi_7), separates them.

B7. (a) Relative rounding errors of 10^{-16} on terms of size 5 \times 10^{13}: absolute errors around 5 \times 10^{-3}, consistent with the measured 0.015. (b) Up to about n = 30 (error below 10^{-6}); at n = 40 the error is 1.5 % of the range of R; at n = 50 the result is meaningless.

B8. \iint \overline{V_{nm}}V_{n'm'} = \int_0^1 R_{nm}R_{n'm'}\rho\,d\rho\int_0^{2\pi}e^{i(m'-m)\theta}d\theta. The angular integral is 2\pi\delta_{mm'}; for m = m' the radial integral is \frac{1}{2(n+1)}\delta_{nn'}; product \frac{\pi}{n+1}\delta\delta. With an orthogonal basis the projection is \sum \frac{\langle I, V_{nm}\rangle}{\langle V_{nm}, V_{nm}\rangle}V_{nm}, and \frac{\langle I, V_{nm}\rangle}{\pi/(n+1)} = \frac{n+1}{\pi}\iint I\,\overline{V_{nm}} = Z_{nm}.

B9. \overline{V_{nm}} = R_{nm}e^{-im\theta} = V_{n,-m} (R_{nm} depends only on |m|). For real I: Z_{n,-m} = \frac{n+1}{\pi}\iint I\,\overline{V_{n,-m}} = \frac{n+1}{\pi}\iint I\,V_{nm} = \overline{Z_{nm}}. Rotation: I'(\rho, \theta) = I(\rho, \theta - \alpha); substitute \theta = \theta' + \alpha: Z'_{nm} = \frac{n+1}{\pi}\iint I(\rho, \theta')R_{nm}e^{-im(\theta' + \alpha)} = e^{-im\alpha}Z_{nm}.

B10. Z_{00} = \frac1\pi\iint I = \frac{m_{00}}{\pi}. Z_{11} = \frac2\pi\iint I\rho e^{-i\theta} = \frac2\pi\iint I(x - iy) = \frac2\pi(m_{10} - im_{01}): the centroid’s offset from the disc centre (0 for a centred object). Z_{20} = \frac3\pi\iint I(2\rho^2 - 1) = \frac3\pi\big(2(m_{20} + m_{02}) - m_{00}\big): the spread \mu_{20} + \mu_{02} (for a centred object) compared with the disc, i.e. \phi_1 in disguise.

B11. x \to -x maps \theta \to \pi - \theta. Z'_{nm} = \frac{n+1}{\pi}\iint I(\rho, \pi - \theta)R_{nm}e^{-im\theta}; with \theta = \pi - \theta': e^{-im(\pi - \theta')} = (-1)^m e^{im\theta'}, so Z'_{nm} = (-1)^m\frac{n+1}{\pi}\iint I R_{nm}e^{im\theta'} = (-1)^m\overline{Z_{nm}}.

Explore: the Zernike polynomials

basis: the real Zernike polynomials for n \le 8 as a pyramid (rows n, columns m; R_{nm}\cos m\theta for m \ge 0, R_{n|m|}\sin |m|\theta for m < 0), red positive, blue negative. Click one for the large view, its radial profile and its name in optics; |V| shows the magnitude |R_{nm}(\rho)|. rotation: a 64 \times 64 test image (a blob, a 6, an F) and a copy rotated by \alpha (optionally mirrored), both drawn from their outlines with partial pixels; the bars compare |Z_{nm}| (n \le 8, m \ge 0) of both, and the dial shows the phase of one Z_{nm} before (orange) and after (blue).

Z1. Preset defocus: read the zero of R_{20} from the profile and compare with B1(c). Click (4, 0) and (6, 0): how many zeros? Where do the rings crowd?

Z2. Switch to |V|. Why do all tiles of the same n and |m| look the same?

Z3. Preset rotate: magnitudes stay (blob, 60°). Read the largest relative difference of |Z_{nm}| and the phase change of Z_{22}. Choose Z_{11} and Z_{33} too. Then try \alpha = 15° and 45°. Explain the differences.

Z4. Preset 6 vs 9. Compare the two images and the bars. Choose Z_{11}, Z_{22}, Z_{31} and read the phase changes. Compare with B6.

Z5. Preset same |Z|, different shape: an F and its mirror image. Are the bars the same? What happens to the phase of Z_{31}? Compare with the rule for a left–right mirror. Untick mirror and set \alpha = 90°: what is the phase change of Z_{31} now?

Z1. The profile crosses 0 at \rho \approx 0.71 (1/\sqrt2). R_{40} has 2 zeros, R_{60} has 3; the outer zeros are closer together: the rings crowd towards the rim, where the area (weight \rho) is largest.

Z2. |V_{nm}| = |R_{nm}(\rho)|\cdot|e^{im\theta}| = |R_{nm}(\rho)|: the angular factor has magnitude 1, so only rings remain, the same for +m and -m. (In the cos/sin view, \cos m\theta and \sin m\theta have different magnitudes; their combination e^{im\theta} does not.)

Z3. Largest difference 1.5 % (for moments above 2 % of |Z_{00}|); Z_{22} phase change -120° = -2 \cdot 60°, Z_{11} -60°, Z_{33} -180°. At 15°: 1.9 %, phase change -30°; at 45°: 3.0 %, -90°. The magnitudes would be exactly equal for the continuous shape; the differences come from drawing the rotated outline on square pixels, which is not a rotation of the pixels; how large the effect is depends on how the edges fall on the grid at each angle.

Z4. The rotated 6 is a 9; the bars are identical (difference 0.0 %: a 180° rotation maps the pixel grid onto itself exactly). Z_{11}: -180°, Z_{22}: 0°, Z_{31}: -180°: odd m change sign, even m stay, as in B6. The phases, not the magnitudes, tell 6 from 9.

Z5. The bars are identical (0.0 %). The phase of Z_{31} goes from 41° to 139°: (-1)^1\overline{Z} has phase 180° - 41° = 139°. ✓ With a rotation of 90° instead (no mirror): 41° \to -49°, a change of -90° = -1 \cdot 90°.

Explore: the disc and the noise

The reconstruction demo of the previous unit; look at the Zernike panel (dashed: the inscribed disc) and the green curve.

E1. Preset Zernike outside the disc (micrograph, N = 20). Compare the four errors. Why is the Zernike error so large, and why does the Zernike curve hardly fall with N?

E2. Choose nucleus and N = 2, 8 and 20. Compare Zernike with Legendre at each. Why can Zernike be better here?

E3. Preset noise in high orders (nucleus, \sigma = 0.3). Find the best N for Zernike and its error, and the error at N = 40. Compare with Legendre and Fourier. Where in the Zernike panel does the noise show most at N = 40?

E1. Monomials 0.142, Legendre 0.160, Zernike 0.552, Fourier 0.139. The Zernike reconstruction is 0 outside the disc, and the micrograph is bright there: those 21 % of the pixels carry a large error whatever N is, so the curve stays near 0.55. Inside the disc alone the Zernike error is 0.158, like the others.

E2. N = 2: Zernike 0.571, Legendre 0.685; N = 8: 0.324 and 0.361; N = 20: 0.185 and 0.226. The nucleus is a roundish object in the middle of a dark square: a basis that lives on the disc spends no effort on the empty corners, and its low orders are round blobs, close to the shape.

E3. Zernike is best at N = 18 (0.257); at N = 40 it is 0.421, worse than Legendre (0.340) and Fourier (0.318). The noise shows as speckle concentrated towards the rim of the disc: the high-order radial polynomials oscillate fastest there and fit the noise of the outer pixels.

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/zernike/exercise.rs. The disc is the one inscribed in a square image (radius = half the side); polar(x, y, size) gives \rho and \theta of a pixel centre, with y up, and factorial is given.

C1. radial(n, m, rho): R_{nm}(\rho) by the factorial formula:

s = 0
for k = 0 .. (n − |m|)/2:
    s = s + (−1)^k · (n−k)! / ( k! · ((n+|m|)/2 − k)! · ((n−|m|)/2 − k)! ) · ρ^(n−2k)

The checks are the small radial polynomials of the background table, R_{nm}(1) = 1, and the orthogonality integrals of B1.

C2. zernike_moment(img, n, m): the moment as a sum over the pixels inside the disc, each of area \Delta A = (2/\text{size})^2:

Z = 0
for every pixel with ρ ≤ 1:
    Z = Z + I(x, y) · R_nm(ρ) · (cos mθ − i sin mθ) · ΔA
Z = (n + 1)/π · Z

The checks: a constant image (Z_{00} \approx 1), a 90° turn (Z_{nm} is multiplied by e^{-im \cdot 90°}, B2), the two mirrors (B11), and Z_{n,-m} = \overline{Z_{nm}} (B9).

C3. reconstruct(img, order): all moments with m \ge 0 up to order N, then at every pixel inside the disc

Î = sum over n ≤ N, m ≥ 0 of  w · R_nm(ρ) · (Re Z_nm · cos mθ − Im Z_nm · sin mθ)
                               # w = 1 for m = 0, 2 for m > 0 (Z_n,−m is the conjugate)

and 0 outside. The check: an image made of V_{00}, V_{20} and V_{22} comes back from the moments up to order 2.

C4. radial0_recurrence(n, rho): R_{n0} without the huge alternating terms, from R_{n0}(\rho) = P_{n/2}(2\rho^2 - 1) and the Legendre recurrence of the previous unit. The check compares it with C1 up to n = 20.

Then cargo run -p moments -- zernike. It turns a letter F (64 \times 64, drawn with partial pixels) by 30°, 90° and 180° and mirrors it, and prints the largest change of |Z_{nm}| (n \le 8) and three phases (out/zernike/rotations.png). It rebuilds the F from its moments up to order 4, 10, 20 and 30 (out/zernike/reconstructions.png), and compares C1 with C4 for large n.

C5. Check the phases of Z_{11}, Z_{22} and Z_{31} after the turns against -m\alpha. Why is the change of the magnitudes 0.00 % for 90° and 180° but not for 30°? Compare the 180° turn with the mirror image: which numbers tell them apart?

C6. How does the reconstruction change with N? What happens in the corners?

C7. Compare the differences between C1 and C4 with B7. Up to which order would you trust the factorial formula?

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

C5. Original: Z_{11} at -134.4°, Z_{22} at -152.2°, Z_{31} at 34.7°. Turned 30°: -165.3°, 148.1° (= -211.9°) and 3.9°, changes of -30.9°, -59.7° and -30.8°, close to -30°, -60°, -30°. Turned 90°: changes of exactly -90°, -180°, -90°; turned 180°: -180°, 0° (-360°), -180°. A turn by 90° or 180° maps the pixel grid onto itself, so the turned F has exactly the same pixels: the magnitudes do not change at all. At 30° the F is drawn anew on the grid; its magnitudes change by at most 1.4 % of |Z_{00}| (digitisation). The 180° turn and the left–right mirror have the same magnitudes, but opposite phases: Z_{11} at 45.6° against -45.6°, Z_{22} at -152.2° against 152.2°, Z_{31} at -145.3° against 145.3°. The phases tell them apart, the magnitudes cannot (B6).

C6. At N = 4 the F is a blurred blob (relative error inside the disc 0.649); at N = 10 a recognisable, wobbly F (0.390); at N = 20 and 30 sharper, with ripples (0.269, 0.200). The ripples run in rings around the centre and are strongest towards the rim. The corners outside the disc stay 0: the basis does not reach them.

C7. The program prints 1.1 \times 10^{-13} (n = 10), 5.1 \times 10^{-10} (n = 20), 2.1 \times 10^{-6} (n = 30), 0.020 (n = 40) and 100 (n = 50), the same sizes as in B7. Up to about n = 30 the formula is fine; from n = 40 the rounding errors of the huge alternating terms are visible, and at 50 the values are meaningless.

Where this leads

Magnitudes of Zernike moments (or Hu’s invariants) turn a shape into a short vector of numbers. With a training set of labelled shapes, such vectors can be fed to a classifier: nearest class mean, nearest neighbour, or the random forests of day 33, which owns training and testing. Moments describe the mass distribution and are blind to topology and thin branches: the skeleton day gives the complementary features. In 3D, the same construction gives the 3 \times 3 inertia tensor of a labelled volume and 3D Zernike moments with spherical harmonics as the angular part (3D images, day 51). The same “keep the first coefficients” idea drives the DCT of the compression day (day 8).