Exercise: brightness constancy and the aperture problem

Goal

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

  • read a flow field from arrows and from the colour wheel,
  • estimate 1D motion from the slope and the change in time, for single pixels and for a window,
  • read speeds from a kymograph and convert them to physical units,
  • draw constraint lines in velocity space and find the motion where they cross, or explain why it cannot be found (the aperture problem),
  • (math) derive the optical flow constraint and the normal flow.

Do the pen-and-paper part first, then the demos.

Background

Idea What it means
Flow (u, v) where the content of a pixel of frame 1 is in frame 2, in pixels per frame (x right, y down)
Colour wheel hue = direction (red = right, counter-clockwise on screen), saturation = speed relative to a stated maximum, white = still
Brightness constancy a moving point keeps its grey value
Optical flow constraint I_x u + I_y v + I_t = 0: one equation per pixel
1D estimate u = -I_t / I_x; over a window u = -\sum I_x I_t / \sum I_x^2
Constraint line all (u, v) that satisfy one pixel’s equation; its point closest to the origin is the normal flow
Aperture problem along an edge all lines coincide: only the motion across the edge is known

In the by-hand questions, I_x is the central difference \tfrac12[f(x+1) - f(x-1)] of frame 1 and I_t = frame 2 − frame 1.

By hand

B1. The bump of the lecture: frame 1 = (0, 0, 1, 4, 1, 0, 0), frame 2 = (0, 0, 0, 1, 4, 1, 0) at x = 0, \dots, 6. (a) Compute I_x and I_t at x = 1, \dots, 5. (b) Compute u = -I_t/I_x at each of these pixels. Which pixels are reliable, and why does the peak give no answer? (c) Combine the five pixels by least squares. Compare with the true motion.

B2. Too fast, and bleaching. Same frame 1 as in B1. (a) Frame 2 is now the bump moved by 2 pixels: (0, 0, 0, 0, 1, 4, 1). Repeat the window estimate of B1(c). What goes wrong? (b) Frame 2 is the bump of B1 moved by 1 pixel but 10 % dimmer (bleaching): 0.9 \cdot (0, 0, 0, 1, 4, 1, 0). Repeat the window estimate. Is the error small or large, and where does it come from?

B3. Constraint lines. Pixel A has (I_x, I_y, I_t) = (1, 0, -2), pixel B has (0, 1, -1). (a) Write each pixel’s constraint and draw both lines in velocity space (u across, v up the page as a number axis). (b) Read off the motion. Give the normal flow of each pixel on its own. (c) Pixel C has (2, 0, -4). Does it add information? Which structure in the image could give A and C but no B?

B4. Two other pixels. (I_x, I_y, I_t) = (2, 1, -5) and (1, -2, 0). Solve for (u, v). Are the two gradients parallel? Check with the determinant.

B5. Kymograph. In a kymograph of vesicle transport, one line has a slope of 3 pixels per frame (moving right), another −1 pixel per frame. The frame interval is 2 s and a pixel is 0.1 µm. Give both speeds in µm/s and the directions.

B6. Reading flow fields. Sketch the colour-wheel picture of (a) a uniform translation to the right; (b) a rotation counter-clockwise about the centre; (c) an expansion from the centre; (d) a field that is zero on the left half and moves up on the right half. Which one looks like the colour-wheel legend itself?

B7. For each case, does brightness constancy hold? (a) A GFP-labelled cell imaged every 10 s for an hour with strong illumination. (b) Brightfield images of beating cardiomyocytes taken at 100 frames per second. (c) A rounded cell rolling over a surface under oblique illumination. (d) A nucleus moving up out of the focal plane.

B1. (a) I_x = (0.5, 2, 0, -2, -0.5); I_t = (0, -1, -3, 3, 1). (b) u = (0, 0.5, \text{undefined}, 1.5, 2). None is exactly right. At x = 1 and x = 5 the slopes are small, so the estimates rest on tiny numbers; at x = 3 (the peak) I_x = 0: the slope says nothing about the motion. The linearisation is also inaccurate on a bump this narrow. (c) \sum I_x I_t = 0 - 2 + 0 - 6 - 0.5 = -8.5, \sum I_x^2 = 0.25 + 4 + 0 + 4 + 0.25 = 8.5, u = 8.5/8.5 = 1.0: exactly the true motion. The errors of the two flanks cancel.

B2. (a) I_t = (0, -1, -4, 0, 4), \sum I_x I_t = 0.5 \cdot 0 + 2 \cdot (-1) + 0 \cdot (-4) + (-2) \cdot 0 + (-0.5) \cdot 4 = -4, so u = 4/8.5 = 0.47 instead of 2. The motion is as large as the bump itself: frame 2’s bump no longer overlaps the slopes of frame 1’s bump, and the linear approximation I_t \approx -I_x u fails. The cure (next unit): warping and coarse-to-fine pyramids. (b) I_t = (0, -1, -3.1, 2.6, 0.9), \sum I_x I_t = -2 - 5.2 - 0.45 = -7.65, u = 7.65/8.5 = 0.90: a 10 % error. The dimming is a change of grey value that the method can only explain as motion: on the rising flank dimming looks like motion to the right, on the falling flank like motion to the left, and they do not cancel exactly.

B3. (a) A: u - 2 = 0, the vertical line u = 2. B: v - 1 = 0, the horizontal line v = 1. (b) They cross at (u, v) = (2, 1). Normal flow of A: (2, 0); of B: (0, 1). Each pixel alone sees only the component along its gradient. (c) C: 2u - 4 = 0, i.e. u = 2: the same line as A (a stronger edge in the same direction), no new information. A and C without B: a vertical edge (gradient along x); its motion along y is invisible.

B4. 2u + v = 5 and u - 2v = 0: u = 2v, so 5v = 5, v = 1, u = 2. The determinant 2 \cdot (-2) - 1 \cdot 1 = -5 \ne 0: the gradients are perpendicular, the lines cross at a right angle.

B5. 3 \text{ px/frame} \times 0.1\ \mu\text{m/px} / 2\ \text{s} = 0.15\ \mum/s to the right; 1 \times 0.1/2 = 0.05\ \mum/s to the left.

B6. (a) One uniform colour, red (right), everywhere. (b) The colours run round the centre, white at the centre (speed 0) and more saturated outwards: at the right of the centre the motion is up (hue 90°), at the top it is left (180°). (c) Expansion: at every point the motion points away from the centre, so the colour at angle θ is the hue θ: exactly the legend, white in the middle. (d) Left half white, right half the “up” hue (90° counter-clockwise from red: yellow-green in the usual wheel), a sharp border in the middle.

B7. (a) No: an hour of strong illumination bleaches GFP; the image dims over time. (b) Mostly yes: frames are very close in time and brightfield does not bleach (but the contraction can move structures in depth). (c) No: the shading of a rolling 3D object changes as it turns. (d) No: the nucleus blurs and dims as it leaves the focal plane.

Math questions

B8. (math) Derive I_x u + I_y v + I_t = 0 from I(x + u\,\delta t, y + v\,\delta t, t + \delta t) = I(x, y, t) by a first-order Taylor expansion. Write down the largest neglected term for a 1D profile and use it to explain B2(a).

B9. (math) Show that the point of the line I_x u + I_y v = -I_t closest to the origin is \mathbf u_\perp = -I_t \nabla I / |\nabla I|^2 and that its distance from the origin is |I_t| / |\nabla I|. Evaluate for (I_x, I_y, I_t) = (3, 4, -10).

B10. (math) Show that u = -\sum I_x I_t / \sum I_x^2 minimises E(u) = \sum (I_x u + I_t)^2 over a window, and that E has a unique minimum iff at least one I_x \ne 0.

B8. I(x + u\delta t, y + v\delta t, t + \delta t) = I + (I_x u + I_y v + I_t)\delta t + O(\delta t^2). Setting this equal to I and dividing by \delta t gives the constraint as \delta t \to 0; with \delta t = one frame it is an approximation. In 1D the largest neglected term is \tfrac12 I_{xx} u^2: it is small only while |u| is small compared with |I_x / I_{xx}|, the distance over which the slope changes. The bump of B2 changes slope within one pixel, so a motion of 2 pixels is far outside that range.

B9. Minimise |\mathbf u|^2 subject to \nabla I \cdot \mathbf u = -I_t. With a Lagrange multiplier, \mathbf u = \mu \nabla I; substituting gives \mu |\nabla I|^2 = -I_t, so \mathbf u_\perp = -I_t \nabla I / |\nabla I|^2, of length |I_t| / |\nabla I|. (Geometrically: the line has normal \nabla I, so the closest point lies along \nabla I.) For (3, 4, -10): |\nabla I|^2 = 25, \mathbf u_\perp = 10 \cdot (3, 4)/25 = (1.2, 1.6), distance 10/5 = 2.

B10. E'(u) = 2\sum I_x (I_x u + I_t) = 0 gives u \sum I_x^2 = -\sum I_x I_t. E''(u) = 2\sum I_x^2 \ge 0, and it is > 0 (a strictly convex parabola with a unique minimum) iff some I_x \ne 0. If all I_x = 0 (a flat window), E does not depend on u: every motion fits equally well.

Explore

The demos of this unit: 1D motion and kymographs, reading flow fields, and the aperture.

Explore: speed from slope and change

A 1D profile moves over 40 frames. Top: frame 1 and frame 2 (click a pixel to read its I_x and I_t). Middle: the per-pixel estimate -I_t/I_x (dots; missing where |I_x| is tiny), the window least-squares estimate (red line) and the truth (dashed). Right: the kymograph with a ruler; drag its two handles along a line to read the slope. Frames apart compares frame 1 with a later frame (the estimate is divided by the number of frames).

K1. Preset the by-hand bump. Check your answers to B1 with the readout (click x = 1, \dots, 5). Set frames apart to 2: compare with B2(a).

K2. Preset a ramp: exact everywhere. Why is the per-pixel estimate exact here, and why would it stay exact for any speed?

K3. Preset a bump: pixels disagree, the window is right. Click x = 33 and x = 39. What do the single pixels say, and the window? Now shrink the window to 0: what happens at the peak?

K4. Preset too fast: a bump of width σ = 4 moves 6 pixels per frame. Read the window estimate at the centre (x = 36). Lower the speed step by step: up to about which speed is the estimate within 10 %?

K5. Preset bleaching breaks constancy (speed 1, 10 % per frame). Read the window estimate at x = 30, 36 and 42. Explain the pattern. What does the kymograph look like?

K6. Preset transport: four vesicles. Use the ruler to measure each speed. Which pair crosses, and at which frame roughly?

K1. The readout gives I_x = 0.5, 2, 0, -2, -0.5 and I_t = 0, -1, -3, 3, 1, per-pixel u = 0, 0.5, undefined, 1.5, 2, and the window (half-width 2) estimate at x = 3 is 1.00. With frames 2 apart, the window estimate at x = 3 is 0.47/2 = 0.24 px/frame instead of 1: the motion of 2 pixels between the two frames compared is too large, as in B2(a).

K2. A ramp has the same slope everywhere and no curvature, so the first-order Taylor expansion is exact: I_t = -I_x u holds for any u. Every pixel gives exactly 1.5 (the preset speed), with no window needed.

K3. x = 33 gives 1.07, x = 39 gives 0.93: the linearisation errors on the two flanks have opposite signs. The window (half-width 6) gives 1.00 everywhere near the bump. With window 0 the estimate at the peak (x = 36) is missing: I_x \approx 0 there.

K4. At 6 px/frame the window estimate at x = 36 is 2.96 (and 4.05 at x = 30): far off and inconsistent. At 2 px/frame it is 1.88 (6 % low); at 3 px/frame 2.56 (15 % low). So up to about 2 px per frame, half the width σ of the bump: the motion must be small compared with the scale of the structure.

K5. 1.52 at x = 30, 0.90 at x = 36, 0.28 at x = 42. On the left (rising) flank the dimming makes frame 2 darker, which the constraint reads as the profile moving right: too fast. On the right flank dimming looks like motion to the left: too slow. In the kymograph the line fades towards the bottom, but its slope is unchanged: the slope is still the right speed.

K6. About 1.5, 0.5, 2.5 and −1 px/frame (the readout lists the true values). The vesicle at x = 70 (speed 2.5) and the one starting at x = 110 (speed −1) approach each other at 3.5 px per frame: they meet after about 40/3.5 \approx 11 frames, near x = 99.

Explore: reading flow fields

One synthetic field, shown as arrows, as the colour wheel and as the speed. Strength scales the field (negative values reverse it). Which field is this? hides the arrows and the speed map.

F1. Compare your sketches of B6 with translation, rotation and expansion (zoom). Which features of the colour picture tell rotation and expansion apart?

F2. Look at saddle and shear. Describe each in words from the arrows, then from the colours alone.

F3. Press which field is this? five times and name each field from its colours before you press show answer.

F1. Both have white at the centre and colours that run once round it. In the expansion the colour at each point is the hue of the direction pointing away from the centre (red on the right, the legend itself). In the rotation the colours are turned a quarter turn: red (moving right) is at the top for a clockwise and at the bottom for a counter-clockwise rotation.

F2. Saddle: stretching along x and compressing along y; the colours run round the centre in the opposite sense to the expansion (red right, cyan left, but “up” colours below and “down” colours above). Shear: horizontal motion that changes with y; two hues only (red in one half, cyan in the other), white along the middle row, saturation growing with the distance from it.

F3. The first five presses show: a contraction (expansion reversed), a saddle, a translation, a vortex and a shear (reversed); then come a closing wound, cells moving apart, an expansion and a rotation. Clues: a white line down the middle with two colours either side is a closing wound (both sheets move towards the gap) or cells moving apart (two blobs, moving away from the centre); a coloured ring with white inside and outside is a vortex.

Explore: the aperture

A pattern moves behind a hole. Left: the moving pattern and, in yellow, the motion each pixel sees (its normal flow). Right: velocity space with one constraint line per pixel (blue), the true velocity (green; drag it) and the least-squares point (red).

A1. Preset bar through a hole. Read the true and the least-squares velocity. Drag the green dot along the blue line. Does anything change in the hole? Tick remove the hole.

A2. Preset the barber pole. In which direction does the grating seem to move in the slit? What is its true motion? What do the constraint lines look like?

A3. Preset a corner: the lines cross. Where do the two families of lines meet? Shrink the hole until it no longer contains the corner: what happens?

A4. Preset flat patch: no lines, then noise: lines scatter. How far is the least-squares point from the truth with noise 6? Make the hole larger: does it get better?

A5. Pattern micrograph. Move the hole size from 4 to 40. At what size do the lines start to cross clearly?

A1. True (1.50, -1.00), least squares (0.70, 0.41): the normal flow, the component of the motion across the bar. Dragging the green dot along the line changes nothing in the hole (only the motion along the bar changes); with the hole removed, the bar’s ends show the true motion.

A2. The stripes seem to move along the slit (the eye follows the long axis), whatever the true motion: the preset moves them to the right at (1.5, 0). All lines are parallel (one gradient direction), so the true motion cannot be recovered; the least-squares point is the normal flow.

A3. At the true velocity (1, 1) (least squares (1.04, 1.04)): one family of lines from the vertical edge (u = 1), one from the horizontal edge (v = 1). With a hole that contains only one edge, only one family is left: the aperture problem returns.

A4. Flat: no lines at all; the readout says “no gradients” and the least-squares point means nothing. With noise 6 the lines scatter, and the least-squares point is (0.84, 0.78) at radius 24 instead of (1.04, 1.04) without noise. A larger hole (radius 40) does not cure this: (0.80, 0.74). More lines average away the random scatter, but noise in the gradients themselves pulls the least-squares answer towards zero (a systematic bias). The cure is less noisy gradients: smooth before differentiating (the σ of the next unit).

A5. At the preset velocity (1, 1): with radius 4 the hole sees one membrane, the lines are nearly parallel (the readout calls them crossing, but the smaller eigenvalue is only 0.12 of the larger) and the estimate is (1.72, 1.35). From radius 10 on the lines come from structures in several directions and the estimate settles at about (1.4, 1.35); at radius 40, (1.37, 1.34). It stays too large: the micrograph has fine texture, so a motion of 1.4 pixels already strains the linearisation (B2(a), K4). Smoothing and warping (next unit) deal with that.

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 opticalflow/src/flow_constraint/exercise.rs.

C1. derivatives_1d(f1, f2): the slope and the change in time of a 1D profile, as in the by-hand questions. The check is B1(a).

Ix(x) = (f1(x+1) − f1(x−1)) / 2        # 0 at the two ends
It(x) = f2(x) − f1(x)

C2. pixel_speed(ix, it): u = -I_t/I_x, or no answer where I_x is (almost) 0. The check is B1(b).

C3. window_speed(ix, it, centre, half): least squares over the window centre − half ..= centre + half. The checks are B1(c) and B2.

u = − Σ Ix·It / Σ Ix²        # no answer if Σ Ix² is (almost) 0

C4. normal_flow(ix, iy, it): the point of the constraint line closest to the origin, -I_t\,\nabla I/|\nabla I|^2. The checks are B9 and B3(b).

C5. intersect(a, b): where the constraint lines of two pixels cross, or no answer if their gradients are parallel. The checks are B3, B3(c) and B4.

det = Ix1 · Iy2 − Iy1 · Ix2                 # 0: parallel lines, the aperture problem
u = (−It1 · Iy2 + It2 · Iy1) / det
v = (−Ix1 · It2 + It1 · Ix2) / det

Then cargo run -p opticalflow -- flow-constraint and open the pictures in out/flow-constraint/. The program moves a bump of width σ = 4 at several speeds and estimates the speed with a window of half-width 6 at its centre; dims the bump by 10 % while it moves by 1; draws a kymograph of four vesicles (kymograph.png, one row per frame, time downwards) and estimates their speeds; and moves the micrograph (blurred with σ = 1.5) by (1, 0.5) px and colours the normal flow of every pixel (normal-flow.png, colour wheel as in legend.png, grey = no gradient).

C6. Up to which speed is the window estimate of the bump within 10 %? Which vesicle’s speed is estimated badly from one pair of frames, and why? Read its speed from the slope in kymograph.png instead.

C7. The bleaching bump: read the estimates at x = 30, 36 and 42 (left flank, centre, right flank). Explain the pattern.

C8. In normal-flow.png the whole image moves by the same (1, 0.5), yet the colours change from place to place. Why? The program prints the mean component of the normal flow along the true direction: why is it about half the true speed? (math)

The model solution is opticalflow/src/flow_constraint/solution.rs in the download.

C6. True speed 0.5, 1, 2, 3, 4, 6: estimates 0.51, 1.00, 1.88, 2.56, 2.96, 2.96. Within 10 % up to 2 px per frame (−6 %); at 3 px it is 15 % low, and beyond it no longer grows: about half the width σ of the bump, as in K4. The vesicles (bumps of width 2.5) give 1.44, 0.52, 2.04 and −1.00 for the true 1.5, 0.5, 2.5 and −1: the one moving 2.5 px per frame moves as far as its own width, too far for the linearisation. In the kymograph its line is the flattest of the four: about 2.5 px further right with every row, 100 px over the 40 frames.

C7. 1.35 on the left flank, 0.90 at the centre, 0.45 on the right flank, for a true speed of 1. On the rising (left) flank, getting darker looks like the bump moving right, so the estimate is too fast; on the falling flank it looks like a motion to the left: too slow. At the centre the two halves of the window partly cancel, and 10 % dimming gives 10 % too slow, as in B2(b).

C8. Each pixel only sees the motion along its own gradient (the aperture problem): the normal flow is the true motion projected onto the gradient direction, so its direction follows the local edges, within 90° of the true direction (right and a little down): mostly magenta, red and orange. It is pale where the edge runs along the motion. The mean component along the true direction is 0.56 against the true speed 1.12. (math) With the angle φ between gradient and motion, the normal flow is |\mathbf u|\cos\varphi along the gradient, and its component along \mathbf u is |\mathbf u|\cos^2\varphi; averaged over gradient directions spread evenly, \cos^2 averages to ½.

Where this leads

One constraint per pixel is not enough. The next unit, Lucas–Kanade, combines the lines of a small window by least squares, and finds that its matrix is the structure tensor of the edges day. The unit after that, Horn–Schunck, makes the whole field smooth instead.