Maybe try using a different approach: cv::accumulate
:
cv::Mat avgImg;
avgImg.create(width, height,CV_32FC3);
for(i = 1; i <= N; i++){
image = imread(fileName.c_str(),0);
cv::accumulate(image, avgImg);
}
avgImg = avgImg / N;
Notice that if you need to show the result image you will have to convert it to CV_8U
or to normalize it, for example:
Mat Mean = Mat::zeros(width, height,CV_32F);
for(i = 1; i <= framesToLearn ; i++){
image = imread(fileName.c_str(),0);
accumulate(image, Mean);
}
Mean = Mean /framesToLearn;
Mean.convertTo(Mean,CV_8U);
imshow("mean",Mean);
waitKey(0);