v9.1.1
Menu

Compute Integral Image

easyPROImage ProcessingComputer Vision 1

Compute the integral image (summed-area table) of a 2D grayscale image.

The integral image II at position (i,j)(i, j) is defined as the sum of all pixel values at or above row ii and at or to the left of column jj in the original image SS:

I(i,j)=∑i′≤i∑j′≤jS(i′,j′)I(i, j) = \sum_{i' \leq i} \sum_{j' \leq j} S(i', j')

The integral image can be computed efficiently using the recurrence:

I(i,j)=S(i,j)+I(i−1,j)+I(i,j−1)−I(i−1,j−1)I(i, j) = S(i, j) + I(i-1, j) + I(i, j-1) - I(i-1, j-1)

with I(i,j)=0I(i, j) = 0 when i<0i < 0 or j<0j < 0.

Integral images are used extensively in computer vision for fast computation of rectangular region sums, enabling algorithms like Viola-Jones face detection and SURF feature detection.

Example

Input
image = [[1, 2, 3],
         [4, 5, 6],
         [7, 8, 9]]
Output
[[1, 3, 6], [5, 12, 21], [12, 27, 45]]
Reasoning
  • Input: image is a 3×33 \times 3 grid SS of pixel intensities, with S(0,0)=1S(0,0)=1 top-left and S(2,2)=9S(2,2)=9 bottom-right.
  • Output: a grid of the same size where I(i,j)I(i,j) is the total of the rectangle running from the top-left corner down to row ii, column jj.
  • The top row is just running totals: 1, 1+2=3, 3+3=61,\ 1+2=3,\ 3+3=6. The left column likewise: 1, 5, 121,\ 5,\ 12.
  • Interior cells combine neighbours: I(1,1)=5+3+5−1=12I(1,1) = 5 + 3 + 5 - 1 = 12, which matches 1+2+4+51+2+4+5. The −1-1 is there because I(0,0)I(0,0) sits inside both I(0,1)I(0,1) and I(1,0)I(1,0), so it was counted twice.
  • The same rule gives I(1,2)=6+6+12−3=21I(1,2) = 6 + 6 + 12 - 3 = 21 and I(2,1)=8+12+12−5=27I(2,1) = 8 + 12 + 12 - 5 = 27, and I(2,2)=9+21+27−12=45I(2,2) = 9 + 21 + 27 - 12 = 45, the sum of every pixel.
  • Notice: any rectangle's sum now needs only four corner values. For example, the bottom-right 2×22 \times 2 block is 45−6−12+1=28=5+6+8+945 - 6 - 12 + 1 = 28 = 5+6+8+9.

Constraints

  • Input: 2D list of integers (grayscale image)
  • Return: 2D list of integers (integral image, same dimensions)
  • Use pure Python (no numpy)
  • Image dimensions: 1 <= rows, cols <= 100

More from Computer Vision 1

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.