Maystro
Maystro

Reputation: 2955

Sum of mat in openCV

I'm trying to calculate the mean (element by element) of a list of matrix. First, I'm doing the sum element by element and here is the code I'm using

 Mat imageResult = videoData[round(timestampInstImages[indexImg] * 100)];
 for (double frame = (timestampInstImages[indexImg] + timeBetweenFields); frame < (timestampInstImages[indexImg] + 1); frame += timeBetweenFields)
 {
     double roundedTimestamp = round(frame * 100);
     if (!videoData[roundedTimestamp].empty())
     {
          cout << "imageResult " << imageResult.at<int>(10,10) << endl;
          cout << "videoData[roundedTimestamp] " << videoData[roundedTimestamp].at<int>(10,10) <<endl;
          imageResult += videoData[roundedTimestamp];
          cout << "Result : " << imageResult.at<int>(10,10) << endl;
     }
 }

Here are the first lines of the output I got:

imageResult 912924469
videoData[roundedTimestamp] 929701431
Result : 1842625900 //(912924469 + 929701431) It looks good
imageResult 1842625900
videoData[roundedTimestamp] 963386421
Result : -1493214815 // Not sure how the sum of 963386421 and 1842625900 returns this value???
imageResult -1493214815
videoData[roundedTimestamp] 963518006
Result : -536905769
imageResult -536905769

As you can see above, there is something wrong in the sum. Not sure what it is. Any idea what is happening?

Upvotes: 0

Views: 1641

Answers (1)

berak
berak

Reputation: 39796

to accumulate several frames into a 'sum' frame, you need one with a larger depth, else you will overflow (or saturate) it.

Mat acc(height,width,CV_32FC3,Scalar::all(0));

cv::accumulate(frame,acc);
cv::accumulate(frame,acc);
cv::accumulate(frame,acc);

acc /= 3;
Mat mean;
acc.convertTo(mean, CV_8UC3);

Upvotes: 2

Related Questions