简体   繁体   English

发现相机的帧差异时出错

[英]Error while finding differences in frames from camera

int main(int argc, char* argv[])
{
    VideoCapture cap(0);
    Mat current_frame;
    Mat previous_frame;
    Mat result; 
    Mat frame;

    //cap.open(-1);
    if (!cap.isOpened()) {
        //cerr << "can not open camera or video file" << endl;
        return -1;
    }

    while(1)
    {
        cap >> current_frame;
        if (current_frame.empty())
            break;

        if (! previous_frame.empty())  {
            // subtract frames
            subtract(current_frame, previous_frame, result);
        }


        imshow("Window", result);
        waitKey(10);

        frame.copyTo(previous_frame); 
    }
}

When i run this program to subtract current frame from the previous frame and then show the resultant frame , it show me this error while start executing 当我运行该程序从上一帧中减去当前帧然后显示结果帧时,它在开始执行时向我显示此错误

Unhandled exception at 0x755d812f in WK01.exe: Microsoft C++ exception: cv::Exception at memory location 0x001fe848.. WK01.exe中0x755d812f的未处理异常:Microsoft C ++异常:内存位置0x001fe848处的cv :: Exception。

And i want to apply the same thing on recorded video 我想在录制的视频上应用相同的内容

I think the problem is with previos_frame . 我认为问题出在previos_frame You assign value to previous_frame only at the and of the loop. 您只能在循环的和处将值分配给previous_frame I think it might be empty at the start of the while loop, so the 我认为在while循环开始时它可能为空,因此

if (! previous_frame.empty())  {
        // subtract frames
        subtract(current_frame, previous_frame, result);
    }

block will not executed. 块将不会执行。

previous_frame also must be the same size as current_frame when subtracting. 减去时, previous_frame的大小也必须与current_frame相同。

This code(the subtract method) should determine the size of result , what You'd like to show at the following line. 此代码(减法)应确定result的大小,即您希望在下一行显示的内容。

in the 1st frame, result is empty ! 在第一帧中,结果为空!

imshow("Window", result); // this will crash

also, you're copying the empty frame Mat to previous_frame , that should be current_frame instead, no ? 另外,您要将空frame Mat复制到previous_frame ,应该是current_frame ,不是吗?

try like: 尝试像:

   if (! previous_frame.empty())  {
       // subtract frames
       subtract(current_frame, previous_frame, result);
       imshow("Window", result); 
   }
   waitKey(10);
   current_frame.copyTo(previous_frame); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM