2013-01-07 51 views
1

工作,我嘗試建立10幀的平均值,所以我嘗試:CV ::附加不OpenCV的

..... 
cv::Mat frame,outf,resultframe1, resultframe2; 
VideoCapture cap(1); 
cap>> frame; 
resultframe1 = Mat::zeros(frame.rows,frame.cols,CV_32F); 
resultframe2 = Mat::zeros(frame.rows,frame.cols,CV_32F); 
while(waitKey(0) != 27}{ 
cap>> frame; 
if (waitKey(1) = 'm'){ 
for ( int j = 0 ; j <= 10 ; j++){ 
cv::add(frame,resultframe1,resultframe2);// here crashes the program ????? 
    .... 
} 

}

我怎麼能解決這個任何想法。 在此先感謝

+0

Ehmm ..你知道你的畫框的大小嗎?如果是的話,你可以嘗試用初始化幀矩陣的代碼嗎? (Mat frame = Mat :: zeros(frame.rows,frame.cols,CV_32F))。您的矩陣深度可能與CV_32F不同。我不確定,但那是我想到的唯一原因。 – emreakyilmaz

+0

你是否已經通過一個調試器來確保'frame','resultframe1'和'resultframe2'全部有效且不爲空? – WildCrustacean

+0

if條件下單個'='符號而不是雙'=='。 1個額外的for循環迭代。它將運行11次而不是10次。 – sgarizvi

回答

2

當OpenCV C++接口中有運算符可用時,不需要顯式調用add函數。以下是如何平均指定幀數的方法。

void main() 
{ 
    cv::VideoCapture cap(-1); 

    if(!cap.isOpened()) 
    { 
     cout<<"Capture Not Opened"<<endl; return; 
    } 

    //Number of frames to take average of 
    const int count = 10; 

    const int width = cap.get(CV_CAP_PROP_FRAME_WIDTH); 
    const int height = cap.get(CV_CAP_PROP_FRAME_HEIGHT); 

    cv::Mat frame, frame32f; 

    cv::Mat resultframe = cv::Mat::zeros(height,width,CV_32FC3); 

    for(int i=0; i<count; i++) 
    { 
     cap>>frame; 

     if(frame.empty()) 
     { 
      cout<<"Capture Finished"<<endl; break; 
     } 

     //Convert the input frame to float, without any scaling 
     frame.convertTo(frame32f,CV_32FC3); 

     //Add the captured image to the result. 
     resultframe += frame32f; 
    } 

    //Average the frame values. 
    resultframe *= (1.0/count); 

    /* 
    * Result frame is of float data type 
    * Scale the values from 0.0 to 1.0 to visualize the image. 
    */ 
    resultframe /= 255.0f; 

    cv::imshow("Average",resultframe); 
    cv::waitKey(); 

} 

創建矩陣時,像CV_32FC3而不是僅CV_32F始終指定完整的類型。

+0

非常感謝,它的工作原理 – Engine