Exercise: moments as projections onto monomials

Before this unit: Day 14: Labelling.

See also: Day 33: Random forests.

Goal

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

  • compute a small table of raw and central moments by hand, for a mask and for a grey image,
  • read the low orders as mean, covariance, skewness and kurtosis of the image seen as a distribution,
  • predict how moments change under mirroring and between a square and a disc with the same covariance,
  • explain the choices made when computing moments: scaled coordinates, pixel as a point or as a square,
  • (math) derive moments of simple shapes, relate moments to the Fourier transform and to Green’s theorem.

Background

Idea What it means
Moment of order p + q m_{pq} = \sum_x \sum_y x^p y^q I(x, y) (I = mask or grey values)
Central moment \mu_{pq} = \sum (x - \bar x)^p (y - \bar y)^q I, with \bar x = m_{10}/m_{00}, \bar y = m_{01}/m_{00}
From raw moments \mu_{20} = m_{20} - m_{10}^2/m_{00}, \mu_{11} = m_{11} - m_{10} m_{01}/m_{00}
As a distribution order 1 mean, 2 covariance S = \mu/m_{00}, 3 skewness \gamma_x = \mu_{30}/(m_{00}\sigma_x^3), 4 kurtosis \kappa_x = \mu_{40}/(m_{00}\sigma_x^4), \sigma_x^2 = \mu_{20}/m_{00}
Count (N+1)(N+2)/2 moments with p + q \le N
Pixel as a square adds 1/12 per pixel to \mu_{20} and \mu_{02}

Coordinates: x = column, y = row (down), pixel centres at integers.

By hand

B1. A filled rectangle [0, 4] \times [0, 2] (continuous, I = 1 inside). Use \iint x^p y^q\,dx\,dy = \frac{4^{p+1}}{p+1}\cdot\frac{2^{q+1}}{q+1}. (a) Compute m_{00}, m_{10}, m_{01}, m_{20}, m_{11}, m_{02}. (b) The centroid, and \mu_{20}, \mu_{11}, \mu_{02} from the raw moments. (c) Check \mu_{20}/m_{00} against the variance of a uniform distribution on an interval of length 4.

B2. A 4 \times 4 grey image (rows from the top, y = 0..3; columns x = 0..3): I = \begin{bmatrix} 0 & 1 & 2 & 1 \\ 0 & 2 & 3 & 1 \\ 0 & 1 & 2 & 0 \\ 0 & 0 & 1 & 0 \end{bmatrix} (a) Compute all m_{pq} with p + q \le 2 and the centroid. (b) Compute \mu_{20}, \mu_{11}, \mu_{02}. (c) Compute \mu_{30} and \mu_{03} (directly, from the deviations x - \bar x, y - \bar y). Which way is the image skewed along y? Does that match what you see?

B3. One pixel at the origin. Compute \mu_{20} if the pixel is a point at its centre, and if it is the unit square [-\tfrac12, \tfrac12]^2 with I = 1. How much does the square model add to \mu_{20} of an object with 548 pixels, and to \mu_{20}/m_{00}?

B4. A pear lies on its side: a large round bulb on the left, a smaller neck on the right, symmetric about the horizontal line through its centroid. (a) Predict the signs of \mu_{30}, \mu_{03}, \mu_{12} and \mu_{21} (or say which are zero). (b) The pear is mirrored left–right. Which of the four change? (c) Rotated by 180° instead? Does order 3 distinguish the mirror image from the rotated copy here? Why?

B5. A square of side a = 30 and a disc of radius r. (a) Choose r so that both have the same \mu_{20}/m_{00} (the variance of the x-marginal). Use a^2/12 and r^2/4. (b) Are their equivalent ellipses the same? Their \mu_{11}? (c) The kurtosis of the square’s marginal is 1.8, of the disc’s 2.0. Explain in words why the disc’s is larger.

B6. How many moments are there up to order 4, 10 and 20? Compare with the number of pixels of a 64 \times 64 image.

B7. A 256 \times 256 image, coordinates in pixels. How large is the weight x^{10} at x = 255? A double has about 16 significant digits. Explain what goes wrong when \mu_{10,0} is computed from raw moments in pixel units, and why mapping x to [-1, 1] helps.

Math questions

B8. (math) Show that for the rectangle [0, a] \times [0, b], m_{pq} = \frac{a^{p+1} b^{q+1}}{(p+1)(q+1)}, and that \mu_{20} = a^3 b/12.

B9. (math) Let g(x) = \int f(x, y)\,dy be the marginal along x. Show that \int (x - \bar x)^p g(x)\,dx = \mu_{p0}. Then show that a reflection x \to -x multiplies \mu_{pq} by (-1)^p.

B10. (math) Expand e^{-2\pi i(ux + vy)} in a Taylor series to show F(u, v) = \sum_{p,q} \frac{(-2\pi i)^{p+q}}{p!\,q!} m_{pq} u^p v^q. Which derivative of F at the origin gives m_{20}, and m_{11}?

B11. (math) The triangle with vertices (0, 0), (4, 0), (0, 3). Use the edge formulas m_{00} = \tfrac12 \sum_i c_i and m_{10} = \tfrac16 \sum_i (x_i + x_{i+1}) c_i, c_i = x_i y_{i+1} - x_{i+1} y_i, to compute its area and \bar x. Check \bar x against the centroid of a triangle (the mean of its vertices).

B12. (math) For the uniform distribution on [-\tfrac a2, \tfrac a2] show \sigma^2 = a^2/12 and kurtosis 9/5.

B1. (a) m_{00} = 8, m_{10} = 4^2 \cdot 2/2 = 16, m_{01} = 4 \cdot 2^2/2 = 8, m_{20} = 4^3 \cdot 2/3 = 128/3 = 42.67, m_{11} = (4^2/2)(2^2/2) = 16, m_{02} = 4 \cdot 2^3/3 = 32/3 = 10.67. (b) Centroid (16/8, 8/8) = (2, 1). \mu_{20} = 128/3 - 16^2/8 = 128/3 - 32 = 32/3 = 10.67; \mu_{11} = 16 - 16 \cdot 8/8 = 0 (the rectangle is axis-aligned); \mu_{02} = 32/3 - 8 = 8/3 = 2.67. (c) \mu_{20}/m_{00} = (32/3)/8 = 4/3 = 1.333 = 4^2/12: the variance of a uniform distribution on an interval of length 4.

B2. (a) m_{00} = 14, m_{10} = 26, m_{01} = 15, m_{20} = 54, m_{11} = 27, m_{02} = 27. Centroid \bar x = 26/14 = 1.857, \bar y = 15/14 = 1.071. (b) \mu_{20} = 54 - 26^2/14 = 5.714; \mu_{11} = 27 - 26 \cdot 15/14 = -0.857; \mu_{02} = 27 - 15^2/14 = 10.929. (c) \mu_{30} = 0.490, \mu_{03} = 4.653. Standardised: \gamma_x = 0.134, \gamma_y = 0.482. Along y the image is skewed towards larger y: most of the mass is in rows 0–2, with a thin tail (the single 1) in the bottom row.

B3. As a point: \mu_{20} = 0. As a square: \int_{-1/2}^{1/2}\int_{-1/2}^{1/2} x^2\,dx\,dy = 1/12 = 0.0833. For 548 pixels: +548/12 = 45.7 in \mu_{20}, +1/12 = 0.083 in \mu_{20}/m_{00} (the same for any object).

B4. (a) The mass is on the left with a tail to the right: \mu_{30} > 0. The pear is symmetric top–bottom, so every moment odd in y is zero: \mu_{03} = \mu_{21} = 0. \mu_{12} = \sum (x - \bar x)(y - \bar y)^2: the left (bulb) is wide in y and has x - \bar x < 0, so \mu_{12} < 0. (b) Mirroring left–right flips the sign of moments odd in x: \mu_{30} and \mu_{12} change sign; \mu_{03}, \mu_{21} stay 0. (c) A 180° rotation flips all odd orders: \mu_{30} and \mu_{12} change sign, \mu_{03}, \mu_{21} stay 0, exactly as the mirror. They agree because the pear is itself symmetric top–bottom: its mirror image is its rotated copy. For a shape without that symmetry they would differ.

B5. (a) a^2/12 = r^2/4 gives r = a/\sqrt3 = 17.32. (b) Both have S = 75\,\mathbf 1 (the identity times 75) and \mu_{11} = 0: the same circle as equivalent ellipse. (c) The square’s marginal is flat: every column has the same height. The disc’s marginal is highest in the middle and falls to 0 at the sides. For the same variance, a distribution with more mass near the centre must have some mass far out: the fourth power weights these far values heavily, so the kurtosis is larger.

B6. 15, 66 and 231. A 64 \times 64 image has 4096 values: 231 moments are 5.6 % of that.

B7. 255^{10} \approx 1.16 \times 10^{24}. Raw moments of order 10 in pixel units are sums of numbers of this size; with 16 significant digits the absolute rounding error of each term is about 10^{8}. The central moment is a combination of large raw moments with alternating signs that nearly cancel, so the result is dominated by rounding error. In [-1, 1] every weight is at most 1 and the terms are of similar size.

B8. m_{pq} = \int_0^a x^p\,dx \int_0^b y^q\,dy = \frac{a^{p+1}}{p+1}\frac{b^{q+1}}{q+1}. Then m_{00} = ab, m_{10} = a^2 b/2, m_{20} = a^3 b/3, and \mu_{20} = m_{20} - m_{10}^2/m_{00} = a^3 b/3 - a^3 b/4 = a^3 b/12.

B9. \int (x - \bar x)^p g(x)\,dx = \int (x - \bar x)^p \int f(x, y)\,dy\,dx = \iint (x - \bar x)^p (y - \bar y)^0 f = \mu_{p0}. For f'(x, y) = f(-x, y) the centroid moves to -\bar x; substituting x \to -x in \mu'_{pq} = \iint (x + \bar x)^p (y - \bar y)^q f(-x, y) gives \iint (-(x - \bar x))^p (y - \bar y)^q f(x, y) = (-1)^p \mu_{pq}.

B10. e^{-2\pi i(ux + vy)} = \sum_p \frac{(-2\pi i u x)^p}{p!} \sum_q \frac{(-2\pi i v y)^q}{q!}; integrating term by term against f gives the series. Differentiating, \frac{\partial^{p+q} F}{\partial u^p \partial v^q}(0, 0) = (-2\pi i)^{p+q} m_{pq}. So \partial^2 F/\partial u^2 (0,0) = -4\pi^2 m_{20} and \partial^2 F/\partial u\,\partial v\,(0,0) = -4\pi^2 m_{11}.

B11. Vertices in order (0,0), (4,0), (0,3) (counter-clockwise with y up): c_0 = 0 \cdot 0 - 4 \cdot 0 = 0, c_1 = 4 \cdot 3 - 0 \cdot 0 = 12, c_2 = 0 \cdot 0 - 0 \cdot 3 = 0. m_{00} = 12/2 = 6. m_{10} = \tfrac16 (4 + 0) \cdot 12 = 8. \bar x = 8/6 = 4/3 = the mean of the vertices’ x, (0 + 4 + 0)/3.

B12. \sigma^2 = \frac1a \int_{-a/2}^{a/2} t^2\,dt = \frac{a^2}{12}; \frac1a \int t^4\,dt = \frac{a^4}{80}; kurtosis = \frac{a^4/80}{a^4/144} = \frac{144}{80} = \frac95 = 1.8.

Explore: moments of any order

The labelling day’s shape canvas, now with moments of every order up to 4. The table gives the central moments \mu_{pq} (or, unticked, the raw moments m_{pq}) divided by m_{00}: the moments of the image as a distribution, in pixel units. Red cells are positive, blue negative; the colour strength compares a cell with the largest of its order. The bars along the bottom and right edge of the canvas are the marginals (column and row sums) with their mean, \pm 1 sd and an arrow for the skewness. Below the table: mean, sd, skewness and kurtosis of both marginals, and \mu_{20}/m_{00} with the pixel as a point and as a square. grey image replaces the mask by a blurred copy that gets brighter to the right; ellipse draws the equivalent ellipse. The buttons rotate, scale, mirror and move the shape; the slider changes the pixel size.

E1. Preset a pear. Read m_{00}, \mu_{20}/m_{00}, \mu_{30}/m_{00} and the skewness along x. Which cells of order 3 are zero, and why? Compare with B4(a).

E2. Press mirror ↔︎ (or the preset its mirror image). Which entries of the table change? Which stay the same? Then set the pixel size to 2 and to 0.5. How do \mu_{20}/m_{00} and the skewness change, and why the difference?

E3. Presets square: kurtosis and disc with the same S. Compare \mu_{20}/m_{00}, the ellipses and the kurtosis of both. Which entries of the table differ most?

E4. Preset L: the ellipse is not enough. Read \mu_{11}/m_{00} and the two skewness values. Rotate the L by 180° (twelve presses of ⟲ 15°): what happens to the order-2 and order-3 entries?

E5. Preset grey vs binary: tick and untick grey image. How do m_{00}, the centroid and the skewness along x change? Explain the sign of the skewness.

E6. Preset pixel: point or square shows a single pixel (pixel size 8). Read \mu_{20}/m_{00} with pixel = square on and off. Then go back to the pear and do the same. Compare with B3.

E7. Draw a shape of your own (draw new shape after deleting the old one) that has \mu_{30} < 0 and \mu_{03} > 0. Check with the table.

E1. m_{00} = 548, \mu_{20}/m_{00} = 63.50, \mu_{30}/m_{00} = 115, skewness +0.226. \mu_{03} and \mu_{21} are 0 (the pear is symmetric top–bottom, so everything odd in y vanishes); \mu_{12}/m_{00} = -49.1 is negative, as predicted in B4.

E2. Only the moments odd in x change sign: \mu_{30}/m_{00} = -115, \mu_{12}/m_{00} = +49.1, skewness -0.226; the centroid moves to \bar x = 36.77. Everything even in x (m_{00}, \mu_{20}, \mu_{02}, \mu_{22}, \mu_{40}, …) is unchanged. With pixel size 2, \mu_{20}/m_{00} = 16.27; with 0.5, 253.26: the table is in units of the current pixels, and halving the pixel doubles every length, so second moments grow by 4 (63.50 × 4 = 254). The skewness is dimensionless and hardly changes: 0.238 (pixel size 2), 0.226, 0.224 (pixel size 0.5); the small differences are digitisation.

E3. Square (side 30): \mu_{20}/m_{00} = 74.92, kurtosis 1.797 in both directions. Disc (radius 17.3): \mu_{20}/m_{00} = 74.47, kurtosis 1.990. The ellipses are the same circle; all order-3 moments are 0 for both. The order-4 cells differ: \mu_{40}/m_{00} = 1.009 \times 10^4 (square) against 1.104 \times 10^4 (disc), and \mu_{22}/m_{00} = 5613 against 3751. The square has mass in its corners, where |x| and |y| are both large (\mu_{22}); the disc has its outermost mass on the axes (\mu_{40}). The digitised values are close to the continuous 75, 1.8 and 2.0.

E4. \mu_{11}/m_{00} = 44.44 (positive: with y down, the L’s mass runs from top left to bottom right), skewness +0.712 along x and -0.567 along y. After 180° every order-3 entry changes sign (\mu_{30}/m_{00}: 474 → −504, \mu_{03}/m_{00}: −741 → 865), while the order-2 entries stay the same up to digitisation (76.22 → 78.31, 44.44 → 47.76: the rotated L falls differently on the pixel grid). The ellipse cannot see the rotation, order 3 can.

E5. Binary: m_{00} = 676, centroid (29.50, 29.50), skewness 0 (the ellipse is point symmetric). Grey: m_{00} = 422.5 (the total intensity: blurred and dimmed on the left), centroid (32.18, 29.07), skewness along x -0.276. The mass now piles up on the bright right side, with a long dim tail to the left: negative skewness.

E6. One pixel: 0 (point) and 0.083 (square), i.e. 1/12. The pear: 63.498 and 63.582, again +0.083. The correction is the same per pixel, so it matters only for very small objects (or when comparing with the continuous shape).

E7. For example the right triangle with corners (50, 10), (50, 50), (10, 10) (right angle at the top right): its mass is concentrated at the right and at the top, with tails to the left (towards smaller x: \mu_{30} < 0) and downwards (towards larger y, since y points down: \mu_{03} > 0). Digitised: \mu_{30}/m_{00} = -491, \mu_{03}/m_{00} = +491. Many shapes work; the signs in the table must match.

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/moments_basis/exercise.rs.

C1. raw_moments(img, order): the table of all m_{pq} with p + q \le order, in pixel units (x = column, y = row):

m[p][q] = 0 for all p + q ≤ N
for every pixel (x, y):
    for every p, q with p + q ≤ N:
        m[p][q] = m[p][q] + x^p · y^q · I(x, y)

The check is your answer to B2(a).

C2. central_moments(img, order): first the centroid \bar x = m_{10}/m_{00}, \bar y = m_{01}/m_{00}, then the same loop with x - \bar x and y - \bar y in place of x and y. The checks are B2(b) and (c), and that moving the image leaves every \mu_{pq} unchanged.

C3. skewness_kurtosis(mu): the image as a distribution. From the central moments up to order 4:

sd_x = sqrt(μ20 / m00)
skewness_x = μ30 / (m00 · sd_x³)
kurtosis_x = μ40 / (m00 · sd_x⁴)          # and the same along y with μ02, μ03, μ04

The checks: the skewness of B2(c), a mirror image (the skewness along x changes sign), and the kurtosis of the square (1.797) and the disc (about 2.0) of B5.

C4. moments_separable(img, order, scaled): all moments at once, the separable way of the slide “Computing moments”. With scaled, the pixel centres are first mapped to [-1, 1]:

x → (2x + 1) / width − 1,   y → (2y + 1) / height − 1        # only if scaled
for every column x and every q ≤ N:
    c[x][q] = sum over the rows y of y^q · I(x, y)
for every p, q with p + q ≤ N:
    m[p][q] = sum over the columns x of x^p · c[x][q]

The checks compare it with C1, and a 4 \times 4 image of ones in [-1, 1] (m_{20} = 5).

Then cargo run -p moments -- moments-basis and open out/moments-basis/shapes.png: a pear, its mirror image, a square and a disc with the same covariance, an L, and the pear in grey (brighter to the right). The program prints a table of their moments, then times C1 and C4 on the micrograph.

C5. Compare the rows of the pear and its mirror image, then of the square and the disc. Which numbers differ? Then compare the pear with the grey pear: why does its skewness along x get smaller?

C6. All moments up to order 10 of the 256 \times 256 micrograph: count roughly how many powers C1 computes and how many C4 needs, and compare with the two times printed. Then compare m_{10,0} in pixel units and in [-1, 1] with B7.

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

C5. The pear (660 pixels, bulb on the left) has skewness +0.321 along x; its mirror image has -0.321. Everything else is the same except the centroid (\bar x = 27.52 against 35.48): m_{00}, \mu_{20}/m_{00} = 85.62, \mu_{02}/m_{00} = 36.31 and both kurtosis values. The skewness along y is 0, as the pear is symmetric top–bottom. The square (side 30) and the disc (radius 17.3) have almost the same \mu_{20}/m_{00} (74.92 and 74.47), so the same ellipse, and skewness 0; only the kurtosis differs: 1.797 against 1.990. In the grey pear the bright right side carries more of the mass: the centroid moves right (27.52 → 29.09) and the mass is less lopsided, so the skewness drops to 0.154.

C6. C1 computes the 66 products x^p y^q for each of the 65 536 pixels: about 4 million. C4 computes 11 powers per pixel (0.7 million), then 66 per column, which is almost nothing. The program prints about 30 ms against 5 ms (the numbers vary from computer to computer); the two tables agree to 14 digits. m_{10,0} is 5.1 \times 10^{27} in pixel units and 4.5 \times 10^{3} in [-1, 1]: in pixel units every term is huge, and a central moment computed from such raw moments loses its digits to cancellation (B7).

Where this leads

Moments change when the shape moves, grows or turns. The next unit builds combinations that do not change: central moments for translation, normalised moments for scale, and Hu’s invariants for rotation. The unit after that asks how to get the image back from its moments, and why monomials are a poor basis for that.