gusucode.com > Matlab脸部识别程序源码 > code/code/rect_sum.m

    function [pixsum] = rect_sum(ii,xstart,ystart,xend,yend)
    [xdim ydim] = size(ii);
    %case 1, want a rectangle that has start coordinates at the top left of
    %the image, in which case we simply return ii(xend,yend).
    if xstart == 1 && ystart == 1
        pixsum = ii(xend,yend);
    %case 2, need to do a bit of math
    %elseif xstart == 1 & ystart ~=1
        
    elseif xstart ~= 1 & ystart ==1
        pixsum = ii(xdim,ydim) - ii(xstart-1,yend);
%         In this example, we want B.
%          ______________________
%         |                      |
%         |          A           |
%         |______________________|<-ii(xstart-1,yend)
%         |                      |
%         |           B          |
%         |______________________|

%          ______________________
%         |                      |
%         |                      |
%         |           C          |
%         |                      |
%         |                      |
%         |______________________|<-ii(xdim,ydim)
%       Thus sum of pixels in B is ii(xdim,ydim) - ii(xstart-1,yend)
    else 
%         xstart
%         ystart
%         xend
%         yend
%         ii(xend,yend) 
%         ii(xend,ystart) 
        pixsum = ii(xend,yend) - ii(xend,ystart-1);
%       We're interested in the sum of the pixels in B.
%       We take ii(xend,yend)
%          ______________________
%         |         |            |
%         |    A    |     B      |
%         |_________|____________|
%         |                      |
%         |                      |
%         |______________________|
%         Pixels in A : ii(xend,ystart-1)
%          ______________________
%         |                      |
%         |          C           |
%         |______________________|<-ii(xend,yend)
%         |                      |
%         |                      |
%         |______________________|
%       Thus sum of pixels in B is ii(xend,yend) - ii(xend,ystart-1)
    end
end