pub fn integral_image<P, T>(image: &Image<P>) -> Image<ChannelMap<P, T>> where
    P: Pixel<Subpixel = u8> + WithChannel<T>,
    T: From<u8> + Primitive + AddAssign
Expand description

Computes the 2d running sum of an image. Channels are summed independently.

An integral image I has width and height one greater than its source image F, and is defined by I(x, y) = sum of F(x’, y’) for x’ < x, y’ < y, i.e. each pixel in the integral image contains the sum of the pixel intensities of all input pixels that are strictly above it and strictly to its left. In particular, the left column and top row of an integral image are all 0, and the value of the bottom right pixel of an integral image is equal to the sum of all pixels in the source image.

Integral images have the helpful property of allowing us to compute the sum of pixel intensities in a rectangular region of an image in constant time. Specifically, given a rectangle [l, r] * [t, b] in F, the sum of the pixels in this rectangle is I(r + 1, b + 1) - I(r + 1, t) - I(l, b + 1) + I(l, t).

Examples

use imageproc::integral_image::{integral_image, sum_image_pixels};

let image = gray_image!(
    1, 2, 3;
    4, 5, 6);

let integral = gray_image!(type: u32,
    0,  0,  0,  0;
    0,  1,  3,  6;
    0,  5, 12, 21);

assert_pixels_eq!(integral_image::<_, u32>(&image), integral);

// Compute the sum of all pixels in the right two columns
assert_eq!(sum_image_pixels(&integral, 1, 0, 2, 1)[0], 2 + 3 + 5 + 6);

// Compute the sum of all pixels in the top row
assert_eq!(sum_image_pixels(&integral, 0, 0, 2, 0)[0], 1 + 2 + 3);