OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Thu, 02 Aug 2012 13:54:41 -0500overflow avoidance on multi-frame accumulationhttp://answers.opencv.org/question/1082/overflow-avoidance-on-multi-frame-accumulation/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?Thu, 02 Aug 2012 10:29:50 -0500http://answers.opencv.org/question/1082/overflow-avoidance-on-multi-frame-accumulation/Comment by sammy for <p>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. </p>
<p>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:</p>
<pre><code>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;
}
</code></pre>
<p>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?</p>
http://answers.opencv.org/question/1082/overflow-avoidance-on-multi-frame-accumulation/?comment=1092#post-id-1092Try 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. Thu, 02 Aug 2012 12:22:24 -0500http://answers.opencv.org/question/1082/overflow-avoidance-on-multi-frame-accumulation/?comment=1092#post-id-1092Answer by awknaust for <p>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. </p>
<p>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:</p>
<pre><code>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;
}
</code></pre>
<p>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?</p>
http://answers.opencv.org/question/1082/overflow-avoidance-on-multi-frame-accumulation/?answer=1095#post-id-1095It looks like you are doing exactly what cv::accumulate does as mentioned in your [last question](http://answers.opencv.org/question/262/extracting-a-vector-of-pixel-values-across/?answer=302#post-id-302)
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.Thu, 02 Aug 2012 13:54:41 -0500http://answers.opencv.org/question/1082/overflow-avoidance-on-multi-frame-accumulation/?answer=1095#post-id-1095