在逐帧读取和写入视频时,我在最后一帧出现断言失败错误。错误仅显示在最后一帧,不知道为什么。在这里看到了这个答案,这建议给waitkey,我的代码已经有了waitkey。
我的简单代码如下
int main()
{
CvCapture *capture=cvCaptureFromFile("C:\\vid\\op.mp4");
if(capture==NULL)
{
printf("can't open video");
}
Mat frame, first_frame,current_frame;
char buffer[100];
int frame_count=1,p=1;
while(1)
{
/*Getting the current frame from the video*/
frame=cvQueryFrame(capture);
cv::cvtColor(frame,current_frame,1); //saving current frame
sprintf(buffer,"C:\\frames\\image%u.jpg",p);
imwrite(buffer,current_frame);
p++;
waitKey(1);
}
return 0;
}
有人请帮忙
解决方案:读取每个文件后,我添加了一张支票-
if(frame.empty()){
fprinf("cannot access frame");
return -1;
}
您需要在每次查询后检查框架是否为空
喜欢
frame=cvQueryFrame(capture);
if (frame.empty()) break;
之所以会出现这样的错误,是因为您试图在最后一帧之后将空的Mat转换为灰度,因此如果帧为空,则退出循环。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句