openCV错误:断言失败(scn == 3 || scn == 4)

MMH*_*MMH 15 c opencv video-capture

我在最后一帧时有Assertion失败错误,同时逐帧读取和写入视频.错误只显示在最后一帧,不知道为什么.在这里看到这个答案,这建议给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;
}  
Run Code Online (Sandbox Code Playgroud)

有人请帮忙

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

if(frame.empty()){
    fprinf("cannot access frame");
    return -1;
}
Run Code Online (Sandbox Code Playgroud)

Har*_*ris 18

每次查询后都需要检查框架是否为空

喜欢

   frame=cvQueryFrame(capture);
     if (frame.empty()) break;
Run Code Online (Sandbox Code Playgroud)

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

  • 如果您将图像加载为灰度,则可能会出现此错误,而不是像BGR2XXXX那样尝试使用cvtColor (18认同)