Get the mean of sequence of frames

Fadwa picture Fadwa · Nov 24, 2013 · Viewed 7.9k times · Source

I want to calculate the mean of the sequence of frames by adding them and then divide by the total number of frames. The problem is that i can't access the pixels in the image. I used this code.

for(i = 1; i <= N; i++){
 image = imread(fileName.c_str(),0);
 Mat Mean = Mat::zeros(width, height,image.type());
        for(w = 0; w < image.rows ; w++)
           for(h = 0; h < image.cols ; h++)
               Mean.row(w).col(h) += (image.at<unsigned float>(w,h) / N);
         }

I'm always having an Assertion Failed error. I also tried:

(float)image.at<uchar>(w,h)

image.row(w).col(h)[0]

image.row(w).col(h).val[0]

but in vain.

Here is the working code...but i can't display the final result because it's float.

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;
    imshow("mean",Mean);
    waitKey(0);

Answer

zenpoy picture zenpoy · Nov 24, 2013

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