为什么函数 cv::subtract() returns 错误 "size of input arguments do not match"?

Why does the function cv::subtract() returns the error "size of input arguments do not match"?

我想减去从网络摄像头拍摄的两张连续图像。 如您所见,我在 while 循环中执行此操作。在 while 循环的最后一行中,我设置了 frame2 = frame,因此我可以从下一次迭代中减去它们。但是函数cv::subtractreturns在终端出现上面的错误。

我做错了什么?

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

}

每次执行 while 循环时,都会创建 frame2 并进行默认初始化。当你打电话时

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

您正在尝试从其中包含图像的 Mat 中减去默认构建的 Mat。这两个 Mat 大小不一样,所以你会得到错误。

如果希望在每次执行 while 循环后保留它们的值,则需要将 frameframe2 的声明移到 while 循环之外。您还需要将 frame2 初始化为相同的大小或将第二张图像捕获到其中,以便您可以在第一次使用 subtract

您需要像在 counter 中那样在 while 循环范围之外声明 frame2。现在,每次循环迭代都会得到一个新的空 frame2

您不妨将所有 Mat 移到 while 循环之外,这样内存就不必在每次迭代结束时取消分配并在下一次重新分配,尽管这不是错误,并且在这种情况下您可能不会看到性能下降。

此外,@rhcpfan 是正确的,因为您需要注意浅拷贝和深拷贝。使用 cv::swap(frame, fram2).