# Calculating sum using integral image

I am attempting to calculate sums over rectangular areas in images using the Viola & Jones trick with the integral image.

Im using c++ and the cv::Mat class.

Here is what I am doing:

.
.
.

integral(Img,M);

CvRect region;

region.x=0;
region.y=0;
region.width=Img.cols;
region.height=Img.rows; //calculating sum on entire image

double regionSum;

int tl= IntegralImage.at<double>((region.y),(region.x));
int tr= IntegralImage.at<double>((region.y),(region.x+region.width+1));
int bl= IntegralImage.at<double>((region.y+region.height+1),(region.x));
int br= IntegralImage.at<double>((region.y+region.height+1),(region.x+region.width+1));

regionSum = br-bl-tr+tl;

cout<<"tl: "<<tl<<" tr: "<<tr<<" bl: "<<bl<<" br: "<<br<<" sum: "<<regionSum<<endl;

For some images those parameters output zero, for others there is a result but that doesn't really make sense, so obviously I am doing something wrong-

edit retag close merge delete

Sort by ยป oldest newest most voted

your integral img has int type(by default), you're accessing it as double.

so either specify the right type:

integral(img,isum,CV_64F); // omitting the type param makes an int image

or access it as int:

isum.at<int>(row,col);
more

THANKS! This help me a lot!

( 2014-07-23 02:50:01 -0500 )edit

Official site

GitHub

Wiki

Documentation