Quiz: integral images

Short questions to test your understanding. Try each on paper first, then open the answer. S(x, y) is the sum of all pixels in columns 1..x and rows 1..y, with a zero row and column at index 0.


Q1. The running totals of a row are 0, 2, 7, 7, 10, 18. What are the pixel values? What is the sum of pixels 2 to 4?

Neighbouring differences give the pixels; a range sum is one difference.

Pixels: 2, 5, 0, 3, 8. Pixels 2–4: S(4) - S(1) = 10 - 2 = 8 (= 5 + 0 + 3).


Q2. Why do we pad the table with a zero row and column?

What would S(x_0 - 1, \cdot) be for a rectangle that starts in column 1?

So that the four-corner formula works for every rectangle, including those touching the top or left border: the corner just outside the rectangle is then a real table entry with value 0. Without padding, every border case would need its own formula.


Q3. For the rectangle with corners A (above-left, outside), B (above-right), C (below-left), D (its bottom-right pixel), why is S(A) added, not subtracted?

Which pixels are in both S(B) and S(C)?

S(D) contains the rectangle plus the strip above it, the strip to its left, and the block above-left. Subtracting S(B) removes the strip above and the block; subtracting S(C) removes the strip to the left and the block again. The block has been removed twice, so S(A), which is exactly that block, is added back once.


Q4. How many additions does the sum over a 64 \times 64 window take directly, and with the integral image? Does the answer with the table change for a 256 \times 256 window?

Adding n numbers takes n - 1 additions.

Directly 64^2 - 1 = 4095 additions. With the table 3 (four lookups), and still 3 for 256 \times 256 (directly 65\,535). The cost does not depend on the size of the rectangle.


Q5. You need the sum over a single 3 \times 3 window of a 2000 \times 2000 image, once. Should you build the integral image?

What does building the table cost?

No. Building it takes about 3 \times 4 \cdot 10^6 additions; the direct sum takes 8. The table pays off only when many sums are needed (a window at every pixel), when the windows are large, or when windows of many sizes are needed.


Q6. Fill in: the running total is to a row of pixels what the ______ is to a function, and taking differences of neighbouring entries corresponds to ______.

The antiderivative (integral from the start up to x); taking differences corresponds to differentiation, which gives the function back. The range sum S(b) - S(a-1) is the discrete \int_a^b f = F(b) - F(a).


Q7. In what order must the table entries be filled with the recursion S(x,y) = I(x,y) + S(x{-}1,y) + S(x,y{-}1) - S(x{-}1,y{-}1)? Could you fill it column by column instead?

Which entries must already be known?

Each entry needs its left, upper and upper-left neighbours, so any order that visits those first works: row by row (left to right) is the usual raster order, but column by column (top to bottom) works just as well. A random order does not.


Q8. (math) How many table lookups does a box sum in a 4-dimensional table need (e.g. a 3D video, x, y, z, t)? What sign does the corner with all four coordinates at the lower end get?

2^4 = 16 lookups. A corner with u coordinates at the upper end gets the sign (-1)^{4 - u}; the all-lower corner has u = 0, sign (-1)^4 = +1.