从相机中查找帧的差异时出错

Error while finding differences in frames from camera

本文关键字:出错 相机 查找      更新时间:2023-10-16
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); 
    }
}

当我运行这个程序从前一帧中减去当前帧,然后显示结果帧时,它在开始执行

时显示这个错误

WK01.exe中0x755d812f的未处理异常:Microsoft c++ exception: cv:: exception at memory location 0x001fe848.

我想对录制的视频应用同样的东西

在第一帧中,结果为空!

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

同样,你正在将空的frame Mat复制到previous_frame,那应该是current_frame,不是吗?

试一试:

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

我认为问题是与previos_frame。只在循环结束时给previous_frame赋值。我认为在while循环开始时它可能是空的,所以

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

块不会被执行。

previous_frame也必须与current_frame大小相同。

这段代码(减去方法)应该确定result的大小,即您希望在下一行显示的内容。