【问题标题】:openCV Error: Assertion failed (scn == 3 || scn == 4)openCV 错误:断言失败 (scn == 3 || scn == 4)
【发布时间】:2014-03-19 06:07:42
【问题描述】:

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

我的简单代码如下

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

【问题讨论】:

    标签: c opencv video-capture


    【解决方案1】:

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

    喜欢

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

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

    【讨论】:

    • 如果您将图像加载为灰度,也可能会出现此错误,而不是尝试使用 BGR2XXXX 之类的 cvtColor
    • @Haris 我有一个类似的问题,我在这里的一个问题中问过:stackoverflow.com/questions/45534183/… 如果您能看一下,我将不胜感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多