Mircea Paul Muresan
Mircea Paul Muresan

Reputation: 639

Efficiently summing elements in a row of a matrix using Integral Image

I would like to make sums for each element of the matrix in an interval of +30 -30 of the curent position. To be more precise suppose I have an element a[i][j] and I like to make the sum of all elements a[i][j - 30] + a[i][j - 29] + a[i][j - 28] + ..... + a[i][ j + 28] + a[i][j+29] + a[i][j + 30; I have also computed the integral image of the matrix such that I can easily and efficiently make the sum by the formula A + D - C - D; Here you can see a post how it works http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#integral My question is how can I make the sum efficiently using the already computed integral image. Or is there another efficient way?

Thank you for your time!

P.S. I know that I could compute the sum for the first 30 elements and at each step add and subtract 1 element - add one from the front and subtract one from the bottom. But I wonder if I could do it faster

Upvotes: 1

Views: 502

Answers (1)

Miki
Miki

Reputation: 41775

By using integral images, you are able to get the sum of the values in a given rectangle, like (from Wikipedia):

enter image description here

You just need to set the proper values for A,B,C,D.

Mat1f I; // your integral image

// for each i,j (check boundaries!)
int radius = 30;
float A = I[i-1][j-radius-1];
float B = I[i-1][j+radius];
float C = I[i][j-radius-1];
float D = I[i][j + radius];

float sum = D - B - C + A;

Upvotes: 1

Related Questions