2016-04-25 113 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)

相关问题