2013-02-14 42 views
1

我試圖得到使用,OpenCV的deviationof多張圖片的標準多張圖片的標準方差,在這裏我做了什麼:入門使用opencv

的#include 的#include 的#include

using namespace std; 
    using namespace cv; 

int main(){ 
cv::Mat frame,frame32f; 
char filename[40]; 
cv::Mat mean; 
const int count =134; 
const int width = 1920; 
const int height = 1080; 
cv::Mat resultframe = cv::Mat::zeros(height,width,CV_32FC3); 
cv::Mat deviationframe = cv::Mat ::zeros(height,width,CV_32FC3); 
cv::Mat temp = cv::Mat ::zeros(height,width,CV_32FC3); 
for(int i = 1 ; i<= count; i++){ 
//int i = 3; 
sprintf(filename,"d:\\BMdvideos\\images\\image%d.tiff",i); 
frame = imread(filename,CV_LOAD_IMAGE_COLOR); 
frame.convertTo(frame32f,CV_32FC3); 
resultframe +=frame32f; 
frame.release(); 
} 
resultframe *= (1.0/count); 
for(int j =1; j<count; j++){ 
    sprintf(filename,"d:\\BMdvideos\\images\\image%d.tiff",j); 
    frame = imread(filename,CV_LOAD_IMAGE_COLOR); 
    frame.convertTo(frame32f,CV_32FC3); 
    temp =(frame32f - resultframe); 
    deviationframe+= temp.mul(temp); 

    //temp.release(); 
} 
imshow("devi",deviationframe); // works 
deviationframe *= 1.0/(count -1); 
imshow("devi2",deviationframe); // works 
cv::sqrt(deviationframe,deviationframe); 
resultframe *= 1.0/255.0; 
imshow("devi3",deviationframe);// works 
deviationframe *= 1.0/255.0; 

imshow("mean ",resultframe); 
imshow("deviation frame ",deviationframe);// BLACK FRAME !!!!!!!!!!!!!!!!!!! 
    waitKey(0); 
return 0; 

}

當我看到我得到的resultframe「mean value」是正確的,但std偏差是錯誤的。任何想法我在做什麼錯了,在此先感謝幫助

回答

1

您不積累平方差圖像的結果來計算標準偏差。當前代碼的結果是隻有最後一個圖像被平方併除以圖像總數。所有以前的計算都沒有影響。

另外,除以255僅爲可視化的圖像,而不是實際計算。在計算標準偏差之前,您將除以255,這會使您的結果不正確。

修改代碼如下:

. 
. 
. 
resultframe *= (1.0/count); 
cv::Mat deviationResult = cv::Mat::zeros(height,width,CV_32FC3); 

for(int j =1; j< count; j++) 
{ 
    sprintf(filename,"d:\\BMdvideos\\images\\image%d.tiff",j); 
    frame = imread(filename,CV_LOAD_IMAGE_COLOR); 
    frame.convertTo(frame32f,CV_32FC3); 
    deviationframe =(frame32f - resultframe); 
    deviationResult += deviationframe.mul(deviationframe); 
} 
resultframe *= (1.0/255.0); 
deviationResult = deviationResult /(count -1); 
cv::sqrt(deviationResult ,deviationResult); 
deviationResult *= (1.0/255.0); 
imshow("mean ",resultframe); 
imshow("deviation frame ",deviationResult); 
waitKey(0); 
return 0; 
+0

@ sgar91謝謝您的回答是有意義,但它並沒有改變我得到的結果 – Engine 2013-02-14 12:37:06

+1

@Engine哦!我知道了。你沒有積累**差異平方圖像**。當它到達'deviationframe =(frame32f - resultframe);'時,當前的代碼只覆蓋以前的值。看到我更新的答案。 – sgarizvi 2013-02-14 13:04:09

+0

再次感謝您的幫助,你就是這個原因之一,但是現在當我將偏差幀調整爲255時,我得到了黑框我編輯了我的問題 – Engine 2013-02-14 13:08:49

0

這裏@ sgar91的幫助後的最終代碼我希望它可以幫助別人

#include <opencv2\core\core.hpp> 
#include <opencv2\highgui\highgui.hpp> 
#include <opencv2\opencv.hpp> 
#include <stdlib.h> 

using namespace std; 
using namespace cv; 

int main(){ 
cv::Mat frame,frame32f; 
char filename[40]; 
cv::Mat mean; 
const int count =134; 
const int width = 1920; 
const int height = 1080; 
cv::Mat resultframe = cv::Mat::zeros(height,width,CV_32FC3); 
cv::Mat deviationframe = cv::Mat ::zeros(height,width,CV_32FC3); 
cv::Mat temp = cv::Mat ::zeros(height,width,CV_32FC3); 
for(int i = 1 ; i<= count; i++){ 
    sprintf(filename,"d:\\BMdvideos\\images\\image%d.tiff",i); 
    frame = imread(filename,CV_LOAD_IMAGE_COLOR); 
    frame.convertTo(frame32f,CV_32FC3); 
    resultframe +=frame32f; 
    frame.release(); 
} 
resultframe *= (1.0/count); 
for(int j =1; j<count; j++){ 
    sprintf(filename,"d:\\BMdvideos\\images\\image%d.tiff",j); 
    frame = imread(filename,CV_LOAD_IMAGE_COLOR); 
    frame.convertTo(frame32f,CV_32FC3); 
    temp =(frame32f - resultframe); 
    deviationframe+= temp.mul(temp); 

    //temp.release(); 
} 
deviationframe *= 1.0/(count -1); 

deviationframe= deviationframe/255; 
cv::sqrt(deviationframe,deviationframe); 
resultframe *= 1.0/255.0; 
imshow("mean ",resultframe); 
imshow("deviation frame ",deviationframe); 
waitKey(0); 
return 0; 
}