Compute Integral Image
Compute the integral image (summed-area table) of a 2D grayscale image.
The integral image at position is defined as the sum of all pixel values at or above row and at or to the left of column in the original image :
The integral image can be computed efficiently using the recurrence:
with when or .
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
image = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]][[1, 3, 6], [5, 12, 21], [12, 27, 45]]
- Input:
imageis a grid of pixel intensities, with top-left and bottom-right. - Output: a grid of the same size where is the total of the rectangle running from the top-left corner down to row , column .
- The top row is just running totals: . The left column likewise: .
- Interior cells combine neighbours: , which matches . The is there because sits inside both and , so it was counted twice.
- The same rule gives and , and , the sum of every pixel.
- Notice: any rectangle's sum now needs only four corner values. For example, the bottom-right block is .
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