Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

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

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.

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...