openCV中垫的总和

Sum of mat in openCV

我正在尝试计算矩阵列表的平均值(逐个元素)。首先,我逐个元素求和,这是我使用的代码

 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;
     }
 }

这是我得到的输出的第一行:

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

正如你在上面看到的,总和有问题。不知道它是什么。知道发生了什么吗?

要将几帧累积成一帧'sum',你需要一个深度更大的帧,否则你会溢出(或饱和)它。

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);