openCV 错误:断言失败(scn == 3 || scn == 4) [英] openCV Error: Assertion failed (scn == 3 || scn == 4)

查看:47
本文介绍了openCV 错误:断言失败(scn == 3 || scn == 4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在最后一帧出现断言失败错误,同时逐帧读取和写入视频.错误只显示在最后一帧,不知道为什么.看到这个答案here,建议给waitkey,我的代码已经有wait key

I am having Assertion failed error at the last frame , while reading and writing a video frame by frame. The errors only shows at the last frame, don't know why. saw this answer here, whichs suggests to give waitkey, my code already have wait key on it.

我的简单代码如下

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

请大家帮忙

解决方案:我在读取每个文件后添加了一个检查-

Solution: I added a check just after reading every file as-

if(frame.empty()){
    fprinf("cannot access frame");
    return -1;
}

推荐答案

每次查询后,您需要检查您的框架是否为空

You need to check your frame is empty or not after each query

喜欢

   frame=cvQueryFrame(capture);
     if (frame.empty()) break;

您收到这样的错误是因为您试图在最后一帧之后将空 Mat 转换为灰度,因此如果帧为空,则退出循环.

You are getting such an error because you are trying to convert an empty Mat to grayscale after last frame, so exit the loop if frame is empty.

这篇关于openCV 错误:断言失败(scn == 3 || scn == 4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆