Exercise: integral images

Goal

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

  • compute running totals (prefix sums) and any range sum as a difference of two entries,
  • build the integral image of a small image, with its zero row and column,
  • compute the sum over any rectangle with four lookups and check it by direct addition,
  • count what the table saves, and when it does not pay off,
  • (math) prove the corner formula and generalise it to 3D.

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

Background

Idea What it means
Running total S(i) = f(1) + \dots + f(i), S(0) = 0; built with one addition per entry
Range sum f(a) + \dots + f(b) = S(b) - S(a-1): one subtraction
Integral image S(x, y) = sum of the pixels in columns 1..x and rows 1..y; S(0, y) = S(x, 0) = 0
Recursion S(x, y) = I(x, y) + S(x-1, y) + S(x, y-1) - S(x-1, y-1), filled row by row
Rectangle, columns x_0..x_1, rows y_0..y_1 S(x_1, y_1) - S(x_1, y_0{-}1) - S(x_0{-}1, y_1) + S(x_0{-}1, y_0{-}1)
Cost building: about 3 additions per pixel; each rectangle: 3 additions, whatever its size

Pixels are numbered from 1; x is the column (across), y the row (down). Throughout, the 5 × 5 image is

I = \begin{bmatrix} 2 & 0 & 1 & 3 & 1 \\ 1 & 4 & 2 & 0 & 2 \\ 3 & 1 & 5 & 2 & 0 \\ 0 & 2 & 1 & 4 & 3 \\ 1 & 3 & 0 & 2 & 5 \end{bmatrix}

(row y = 1 on top). The demo’s preset exercise image is the same image.

By hand

B1. The row f = (3, 1, 4, 1, 5, 9, 2, 6). (a) Write down the running totals S(0), \dots, S(8). (b) Compute the sums of pixels 3–7, 2–5 and 1–8 from the table. How many additions would each need directly? (c) What is S(6) - S(5)? What does a difference of two neighbouring entries give in general?

B2. (a) Build the 6 \times 6 integral image of I (with the zero row and column), row by row with the recursion. Check: the last entry must be the sum of all 25 pixels. (b) Compute the sum over the inner 3 \times 3 block (columns 2–4, rows 2–4) with four lookups. Name the four entries and their signs. Check by adding the nine pixels. (c) The same for columns 3–5, rows 2–5. (d) Get the single pixel I(3, 3) back from four table entries.

B3. A rectangle that touches the border: columns 1–3, rows 1–2. Which of the four lookups fall in the zero row or column? Do you need a special case?

B4. Counting. A 100 \times 100 image; we want the sum of the k \times k window at every position where the window fits inside the image. (a) k = 3: how many windows are there? How many additions directly, and how many with the table (building it, plus 3 per window)? (b) The same for k = 31. (c) When is the table not worth it?

B5. You change one pixel of the image, I(2, 2), from 4 to 6. Which entries of the table change, and by how much?

B1. (a) S = 0, 3, 4, 8, 9, 14, 23, 25, 31. (b) Pixels 3–7: S(7) - S(2) = 25 - 4 = 21 (directly 4 + 1 + 5 + 9 + 2: 4 additions). Pixels 2–5: S(5) - S(1) = 14 - 3 = 11 (3 additions). Pixels 1–8: S(8) - S(0) = 31 - 0 = 31 (7 additions). With the table: always 1 subtraction. (c) S(6) - S(5) = 23 - 14 = 9 = f(6). The difference of neighbouring entries gives back the pixel: differencing undoes summing.

B2. (a)

0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 2 2 3 6 7
2 0 3 7 10 13 16
3 0 6 11 19 24 27
4 0 6 13 22 31 37
5 0 7 17 26 37 48

(row y down the side, column x across the top). Example: S(3, 3) = I(3,3) + S(2,3) + S(3,2) - S(2,2) = 5 + 11 + 10 - 7 = 19. The last entry 48 is the sum of all pixels. (b) S(4,4) - S(4,1) - S(1,4) + S(1,1) = 31 - 6 - 6 + 2 = 21. Directly: 4 + 2 + 0 + 1 + 5 + 2 + 2 + 1 + 4 = 21. (c) S(5,5) - S(5,1) - S(2,5) + S(2,1) = 48 - 7 - 17 + 2 = 26. (d) I(3,3) = S(3,3) - S(3,2) - S(2,3) + S(2,2) = 19 - 10 - 11 + 7 = 5.

B3. S(3, 2) - S(3, 0) - S(0, 2) + S(0, 0) = 10 - 0 - 0 + 0 = 10 (= 2 + 0 + 1 + 1 + 4 + 2). Three lookups fall in the zero row or column. No special case: the zeros do the work, which is the point of padding.

B4. (a) 98^2 = 9604 windows. Directly 9604 \times 8 = 76\,832 additions. With the table: building 3 \times 10\,000 = 30\,000, plus 3 \times 9604 = 28\,812: 58\,812 in total. A modest saving. (b) 70^2 = 4900 windows; directly 4900 \times 960 = 4\,704\,000; with the table 30\,000 + 14\,700 = 44\,700, about 100 times fewer. (c) For small windows and few of them: building the table costs about 3 additions per pixel, which a single 2 \times 2 window (3 additions) never pays back. It pays off for large windows, many windows, or windows of many different sizes.

B5. S(x, y) changes by +2 for every entry with x \ge 2 and y \ge 2: the 4 \times 4 block of entries from (2,2) to (5,5), 16 entries. Every other entry stays the same. (A single changed pixel can change a quarter of the table: the table is cheap to read, but not cheap to update.)

Math questions

B6. (math) Prove the corner formula of the Background from the definition S(x, y) = \sum_{x' \le x}\sum_{y' \le y} I(x', y'): count how many times each pixel of the image is included in the four terms. Then show that the recursion is the corner formula for a single pixel.

B7. (math) A volume (or a video, with time as the third axis) has a 3D table S(x, y, z) = sum over x' \le x, y' \le y, z' \le z. (a) How many lookups does the sum over a box need, and with which signs? (b) Write the recursion that builds S in one pass.

B8. (math) Show that the integral image can be built with two passes of 1D running totals: first along every row, R(x, y) = \sum_{x' \le x} I(x', y), then along every column of R. How many additions per pixel does that take?

B6. Let the rectangle be columns x_0..x_1, rows y_0..y_1. Each pixel (x', y') is in S(x, y) if x' \le x and y' \le y. Split the image into regions by whether x' < x_0, x_0 \le x' \le x_1 or x' > x_1, and the same for y'. A pixel with x' > x_1 or y' > y_1 is in none of the four terms. For the others, count the terms +S(x_1, y_1) - S(x_1, y_0{-}1) - S(x_0{-}1, y_1) + S(x_0{-}1, y_0{-}1) that contain it:

  • inside the rectangle: 1 - 0 - 0 + 0 = 1;
  • left of it (x' < x_0, y_0 \le y' \le y_1): 1 - 0 - 1 + 0 = 0;
  • above it (x_0 \le x' \le x_1, y' < y_0): 1 - 1 - 0 + 0 = 0;
  • above-left (x' < x_0, y' < y_0): 1 - 1 - 1 + 1 = 0.

So the sum contains exactly the pixels of the rectangle, once each. With x_0 = x_1 = x, y_0 = y_1 = y: I(x, y) = S(x, y) - S(x, y-1) - S(x-1, y) + S(x-1, y-1), which rearranged is the recursion.

B7. (a) 8 lookups, one per corner of the box. With lower corner (x_0{-}1, y_0{-}1, z_0{-}1) and upper (x_1, y_1, z_1): a corner with u upper coordinates has sign (-1)^{3 - u}: + for the all-upper corner, - for the three corners with two upper coordinates, + for the three with one, - for the all-lower corner. (b) S(x,y,z) = I(x,y,z) + S(x{-}1,y,z) + S(x,y{-}1,z) + S(x,y,z{-}1) - S(x{-}1,y{-}1,z) - S(x{-}1,y,z{-}1) - S(x,y{-}1,z{-}1) + S(x{-}1,y{-}1,z{-}1): the single-voxel box solved for its all-upper corner.

B8. \sum_{y' \le y} R(x, y') = \sum_{y' \le y}\sum_{x' \le x} I(x', y') = S(x, y). Each pass is one addition per pixel, so 2 additions per pixel, one fewer than the recursion (which uses 3). The two-pass form is also easier to run in parallel: all rows at once, then all columns.

Explore

The demo has two modes. In 1D (first), the top plot shows a row of pixel values (drag the bars up or down), the bottom plot their running total as a staircase. The sliders a and b choose a range: its bars are shaded, and the red and green dots mark S(a-1) and S(b). smooth replaces the bars by a curve f and the staircase by its antiderivative F.

In 2D (below), the left grid is the image and the right grid its table, with the zero row and column in grey. step fills one more table entry: the orange cell is the one being filled, the green + cells are added, the red − cell subtracted. fill all completes the table. Drag across the image to choose a rectangle: its four corner entries are marked with their signs, the readout writes out the sum, and a counter adds up the additions “direct” and “table” over all rectangles you have tried. edit pixels lets you change pixel values (upper half of a cell: +1, lower half: −1).

E1. In 1D, preset exercise row: check B1(b) by moving a and b. Then make pixel 6 equal to 0 (drag its bar down). Which entries of the running total change? Which range sums change?

E2. In 1D, preset one spike. Describe the staircase. Where is it steep, and where flat? Tick smooth: where does F rise fastest?

E3. In 2D, preset exercise image: press step until the cell S(3, 3) is filled. Check the four numbers in the readout against your B2(a).

E4. Fill the table, then drag the rectangles of B2(b) and B2(c). Compare with your answers. After both, what does the counter say for “direct” and “table”? Now drag the whole image: how many additions directly, and with the table?

E5. Preset one pixel, four lookups and preset rectangle at the border: compare with B2(d) and B3.

E6. Preset 1 × 8 row = 1D. Which two of the four lookups are always 0? Why is the 2D formula then the same as the 1D one?

E7. Switch to edit pixels and add 2 to the pixel at x = 2, y = 2 (exercise image, filled table). Which table entries change? Compare with B5.

E1. With f(6) = 0, the running totals S(6), S(7), S(8) all drop by 9 (to 14, 16, 22); S(0) to S(5) stay. Every range that contains pixel 6 changes by -9 (e.g. 3–7 becomes 12); ranges that end before 6 or start after 6 do not.

E2. The staircase rises by 1 at every pixel (flat-ish, slope 1) and jumps by 9 at the spike (position 7). It is steep where the pixels are bright. With smooth, F rises fastest at the peak of f: the slope of F is f.

E3. Filling cell 13 (row 3, column 3): S(3,3) = I(3,3) + S(2,3) + S(3,2) - S(2,2) = 5 + 11 + 10 - 7 = 19.

E4. Inner 3 \times 3: 31 - 6 - 6 + 2 = 21. Columns 3–5, rows 2–5: 48 - 7 - 17 + 2 = 26. Counter after these two: direct 8 + 11 = 19, table 3 + 3 = 6. The whole image: S(5,5) - 0 - 0 + 0 = 48, directly 24 additions, with the table 3 (the counter adds them to the running totals).

E5. One pixel: 19 - 10 - 11 + 7 = 5, as in B2(d): 0 additions directly, 3 with the table, so for one pixel the table loses. Border: 10 - 0 - 0 + 0 = 10, as in B3.

E6. The two lookups in row 0 (the zero row): S(x_1, 0) and S(x_0 - 1, 0). What is left, S(x_1, 1) - S(x_0 - 1, 1), is exactly the 1D rule S(b) - S(a-1). For pixels 4–8: 31 - 8 = 23.

E7. All 16 entries with x \ge 2 and y \ge 2 grow by 2 (e.g. S(5,5) from 48 to 50); row 1, column 1 and the zero row and column stay the same.

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 integral/src/integral_image/exercise.rs.

The code counts pixels from 0, the exercises from 1: pixel f(i) of B1 is f[i − 1], and pixel “column 3, row 3” is (x, y) = (2, 2). The table keeps the numbering of the exercises: its entry S(x, y) is table[(x, y)], with the zero row and column at index 0.

C1. running_total(f) and range_sum(s, a, b): the running total of a row, and the sum of a range from it.

S(0) = 0
for i = 1 .. N:
    S(i) = S(i − 1) + f(i)
sum of pixels a .. b = S(b) − S(a − 1)

The checks are your answers to B1.

C2. integral_image(img): the (W+1) \times (H+1) table, filled row by row with the recursion.

S = table of zeros, one row and one column larger than the image
for y = 1 .. H:
    for x = 1 .. W:
        S(x, y) = I(x, y) + S(x − 1, y) + S(x, y − 1) − S(x − 1, y − 1)

The check is your table of B2(a).

C3. rect_sum(s, x0, y0, x1, y1): the sum over columns x0..=x1 and rows y0..=y1 with four lookups, S(x_1, y_1) - S(x_0{-}1, y_1) - S(x_1, y_0{-}1) + S(x_0{-}1, y_0{-}1) in the numbering of the exercises. The checks are B2(b)–(d) and B3, and every rectangle of a small crop against adding up its pixels.

C4. integral_two_pass(img): the same table in two passes, as in B8.

for every row y:     R(x, y) = R(x − 1, y) + I(x, y)        # running totals along the row
for every column x:  S(x, y) = S(x, y − 1) + R(x, y)        # running totals of R down the column

C5. window_sums_direct(img, k) and window_sums_table(img, k): the sum of every k \times k window that fits in the image, once by adding its k^2 pixels, once with four lookups in the table.

Then cargo run -p integral -- integral-image and open the pictures in out/integral-image/.

C6. Open table.png, the integral image of the micrograph. Describe it. Why is it a smooth ramp, although the micrograph is full of detail? Where is the detail hidden?

C7. The program times the window sums for k = 3, 11, 31, 101. How do the two times change with k? Predict the ratio of the direct times for k = 31 and k = 101 from the number of windows and the additions per window (B4), and compare.

The model solution is integral/src/integral_image/solution.rs in the download.

C6. Black at the top left, rising smoothly to white at the bottom right, where the entry is the sum of the whole image (about 43 271 for grey values between 0 and 1). Every entry adds up a large block of pixels, so a single pixel changes it very little: the table looks smooth. The detail is in the differences of neighbouring entries: the program gets every pixel back exactly from four entries (largest difference 0).

C7. Directly, the time grows with k^2: on our computer 2.8, 28, 187 and 935 ms. With the table it stays at about 2 to 4 ms for every k (building the table plus four lookups per window), and it even gets a little faster for large k, because fewer windows fit. Prediction: (24\,336 \cdot 101^2) / (51\,076 \cdot 31^2) \approx 5.1; measured 935 / 187 = 5.0. For k = 3 the table brings nothing (2.8 against 4.4 ms): building it costs about as much as the 9 additions per window it saves, as in B4(c). Times depend on the computer; the ratios do not.

Where this leads

The table answers “what is the sum over this rectangle?” in constant time. The next unit asks it at every pixel, for windows of any size: box filters, the local mean and variance, and what happens when the numbers in the table get too big.