www.gusucode.com > images 案例代码 matlab源码程序 > images/ComputeIntegralImageExample.m

    %% Use Integral Image to Compute Region Sums
% 
%%
% Create a simple sample image. For this example, sum the 2-by-2
% rectangular region starting at row 1, column 3 (value 1) and extending to
% row 2, column 4 (value 14). In this trivial example, it's easy to
% calculate the sum of the pixels in the region: 1 + 7 + 8 + 14 = 30.

% Copyright 2015 The MathWorks, Inc.

I = magic(5)
%%   
% Create an integral image of the sample image. The value of each pixel in
% the integral image is the sum of the pixel above it and the pixel to its
% left. Note how |integralImage| adds a padding row to the top and left
% sides of the image.
J = integralImage(I)
%%
% Sum the rectangular region in the integral image. In this calculation,
% you extend the rectangular region you sum. The coordinates of the four
% corners are: (startRow,startCol),(startRow,endCol+1),(endRow,startCol),
% and (endRow+1,endCol+1)
regionSum = (J(1,3) + J(3,5)) - (J(1,5) + J(3,3))