2016-04-25 114 views
1

我想從攝像頭中減去兩個連續的圖像。 正如你所看到的,我正在做一個while循環。在while循環的最後一行,我設置了frame2 = frame,所以我可以從下一次迭代中減去它們。但是函數cv :: subtract在終端中返回上述錯誤。爲什麼函數cv :: subtract()返回錯誤「輸入參數的大小不匹配」?

我做錯了什麼?

#include <iostream> 
#include "core.hpp" 
#include "highgui.hpp" 
#include "imgcodecs.hpp" 
#include "cv.h" 

using namespace std; 
using namespace cv; 


int main(int argc, char* argv[]) 
{ 

    VideoCapture cap(0); ///open the video camera no. 0 (laptop's default camera) 

    ///make a writer object: 
    cv::VideoWriter writer; 

    if (!cap.isOpened()) /// if not success, exit program 
    { 
     cout << "ERROR INITIALIZING VIDEO CAPTURE" << endl; 
     return -1; 
    } 

    char* windowName = "Webcam Feed(diff image)"; 
    namedWindow(windowName,WINDOW_NORMAL); ///create a window to display our webcam feed 


    ///we need to define 4 arguments for initializing the writer object: 
    //filename string: 
    string filename = "C:\\Users\\PEYMAN\\Desktop\\ForegroundExtraction\\openCV_tutorial\\2.writing from video to file\\Payman.avi"; 

    //fourcc integer: 
    int fcc = CV_FOURCC('D','I','V','3'); 

    //frame per sec integer: 
    int fps = 10; 

    //frame size: 
    cv::Size framesize(cap.get(CV_CAP_PROP_FRAME_WIDTH),cap.get(CV_CAP_PROP_FRAME_HEIGHT)); 


    ///initialize the writet object: 
    writer = VideoWriter(filename,fcc,fps,framesize); 

    if(!writer.isOpened()){ 

     cout << "Error opening the file" << endl; 
     getchar(); 
     return -1; 
    } 

    int counter = 0; 
    while (1) { 

     Mat frame,frame2,diff_frame; 


     ///read a new frame from camera feed and save it to the variable frame: 
     bool bSuccess = cap.read(frame); 



     if (!bSuccess) ///test if frame successfully read 
     { 
      cout << "ERROR READING FRAME FROM CAMERA FEED" << endl; 
      break; 
     } 

     /// now the last read frame is stored in the variable frame and here it is written to the file: 
     writer.write(frame); 

     if (counter > 0){ 

      cv::subtract(frame2,frame,diff_frame); 
      imshow(windowName, diff_frame); ///show the frame in "MyVideo" window 

     } 

     ///wait for 10ms for a key to be pressed 
     switch(waitKey(1)){ 

     ///the writing from webcam feed will go on until the user presses "esc": 
     case 27: 
      ///'esc' has been pressed (ASCII value for 'esc' is 27) 
      ///exit program. 
      return 0; 

     } 

    frame2 = frame; 
    counter++; 
    } 

    return 0; 

} 
+0

嘗試在複製幀時使用frame2 = frame.clone()。你可以使用'frame2 = frame2 - frame'或'frame2 - = frame'而不是'cv :: subtract(不需要) – rhcpfan

+1

@rhcpfan更好,使用'cv:swap(frame,frame2);' –

回答

2

每當您執行while循環時frame2被創建並且默認初始化。當你調用

cv::subtract(frame2,frame,diff_frame); 

您試圖減去默認從Mat,在它的圖像構建Mat。這兩個Mat將不會是相同的大小,所以你得到的錯誤。

如果您希望在每次執行while循環後保留其值,您需要將frameframe2的聲明移至while循環的外部。您還需要將frame2初始化爲相同尺寸,或者將第二張圖像捕捉到第二張圖像中,以便第一次使用subtract

2

您需要聲明frame2超出while循環的範圍,就像您使用counter所做的那樣。現在,你會得到一個新的,空的frame2與循環的每個迭代。

你還不如把所有的Mat S中while循環外,這樣的內存沒有在每次迭代和結束時被取消分配重新分配了下,雖然這不是一個錯誤,在這種情況下你可能不會看到性能損失。

此外,@rhcpfan是正確的,因爲你需要注意淺與深的副本。使用cv::swap(frame, fram2)

相關問題