Ask Your Question
0

overflow avoidance on multi-frame accumulation

asked 2012-08-02 10:29:50 -0600

rich gravatar image

I asked a question a few weeks ago about calculating image statistics (mean, variance, skewness, etc.) across multiple frames. The answers I received worked nicely, but I have a follow-on question.

I am starting with an array of matrices ( Mat *frameArray ) of type CV_U16C1 or CV_U8C1. When accumulating multiple frames, it is necessary to perform the accumulation and division in floating point math to avoid overflow. My current implementation is as the following:

Mat sumImage, tempDouble; 
frameArray[0].convertTo(sumImage,CV_64FC1,0); // initialize accumlation to zeros 
for(int i=0; i < numFrames; i++) { 
    frameArray[i].convertTo(tempDouble,CV_64FC1); 
    sumImage += tempDouble;
}

This works, but requires converting each frame to double precision, which seems slow and rather inefficient. In fact the "convertTo" function accounts for more than half of the time required for this loop. Can anyone recommend a more efficient approach? Is there a way to recast the pointer to the matrix array?

edit retag flag offensive close merge delete

Comments

Try converting to CV_32SC1 instead of CV_64FC1, it should be more than enough, unless you average more than 2^24 frames, which I doubt... Integer math is sometimes much faster, and conversions are especially fast.

sammy gravatar imagesammy ( 2012-08-02 12:22:24 -0600 )edit

1 answer

Sort by » oldest newest most voted
2

answered 2012-08-02 13:54:41 -0600

awknaust gravatar image

It looks like you are doing exactly what cv::accumulate does as mentioned in your last question

If you look at the documentation, the Output Array has to be a CV_32F or CV_64F, but the inputs can be 8bit or 16 bit.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-08-02 10:29:50 -0600

Seen: 1,186 times

Last updated: Aug 02 '12