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);
有帮助吗?

解决方案

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

其他提示

just don't overcomplicate it, avoid the inner loops:

Mat Mean = Mat::zeros(width, height,CV_32F);
for(i=0; i<N; i++) {
    Mean += imread(fileName.c_str(),0);
}
Mean /= N;

and yes, if you wnat to access single pixels, you have to use:

image.at(r,c);

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top