I build opencv with ffmpeg support and this is a snippet of my code to read a video and iterate through the frames.
if (argc != 3) {
std::cout<<"Usage: exe input_video_filename skip_frames \n"<<std::endl;
return 0;
}
char* video_fn = argv[1];
int skip_frames = atoi(argv[2]);
VideoCapture cap;
if(!cap.open(video_fn))
{
std::cout<<"error reading video"<<video_fn<<std::endl;
return -1;
}
Mat frame,gray;
int i = 0;
double score = 0;
vector<double> scores;
while(true)
{
if(!**cap.retrieve(frame)**)
{
std::cout<<"error retrieve frame"<<std::endl;
break;
}
if(i%skip_frames)
{
cvtColor(frame,gray,CV_BGR2GRAY);
double sc = compute_cpbdm(gray);
std::cout<<sc<<std::endl;
scores.push_back(sc);
}
}
The program succeed to read the video at line cap.open(video_fn) but fail to retrieve frame from VideoCapture at the line if(!cap.retrieve(frame)). I tried with mp4 and avi but it always fails.Does anyone encountered similar problem? How to solve it? Many thanks!
you’re using
capture.retrieve(frame), then there should be acapture.grab()before that.alternatively, you can do:
capture.read(frame), which does both at the same time,or just
capture >> frame;(same thing as read)