2013-11-24 23 views
4

我想通過加上它們來計算幀序列的均值,然後除以幀的總數。問題是我無法訪問圖像中的像素。我用這個代碼。獲取幀序列的均值

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

我總是有一個Assertion失敗的錯誤。 我也試過了:

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

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

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

但徒然。

這是工作代碼...但我無法顯示最終結果,因爲它是浮動的。

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

回答

4

也許嘗試用不同的方法: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; 

請注意,如果你需要顯示的結果圖像,你必須把它轉換爲CV_8U或正常化它,例如:

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

+0

結果是正確的,但我不能通過imshow()顯示他們......它只是一個白色圖像。 – Misaki

+0

你應該轉換爲'uint8'而不是'float32' – zenpoy

+0

我將CV_32FC3更改爲CV_32S,並且因爲不同的數據類型而導致運行時錯誤。 – Misaki

0

只是不過分複雜化了,避免了內部循環:

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

是的,如果你wnat訪問單個像素,使用:

圖像。在(R,C);

+0

我想你的代碼,我有如下所示運行時錯誤... https://www.dropbox.com/s/yx0nckxmik5fgvm/Untitled.jpg – Misaki

+0

現在它工作得很好,但summatio在Mean中n不會超過255的值!!! – Misaki

+0

可能,因爲N ===這裏很小 – berak