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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21871540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:47:25  来源:igfitidea点击:

openCV Error: Assertion failed (scn == 3 || scn == 4)

copencvvideo-capture

提问by MMH

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.

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

my simple code is as follows

我的简单代码如下

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

Anybody please help

任何人请帮忙

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

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

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

回答by Haris

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

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

Like

喜欢

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

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.

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