[英]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);
}
}
当我运行该程序从上一帧中减去当前帧然后显示结果帧时,它在开始执行时向我显示此错误
WK01.exe中0x755d812f的未处理异常:Microsoft C ++异常:内存位置0x001fe848处的cv :: Exception。
我想在录制的视频上应用相同的内容
我认为问题出在previos_frame
。 您只能在循环的和处将值分配给previous_frame
。 我认为在while循环开始时它可能为空,因此
if (! previous_frame.empty()) {
// subtract frames
subtract(current_frame, previous_frame, result);
}
块将不会执行。
减去时, previous_frame
的大小也必须与current_frame
相同。
此代码(减法)应确定result
的大小,即您希望在下一行显示的内容。
在第一帧中,结果为空!
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.