Grabbing the image skips current (or next) frame.
Hey there, I've recently realised that all of my output videos are twice as fast as the original, so i made the program print out the current frame and noticed that it was increasing by 2 each time (2,4,6,8...). When I removed the "cap>>currentImage;" it printed 1,2,3,4,...
I just don't understand why this is happening. Is there a mistake in the way I'm printing the frame? Any help would be appreciated.
int main (int argc, char *argv[]) {
/// CHANGE INPUT FILE HERE
Mat currentImage;
VideoCapture cap("testcut.avi");//"herman.avi"
if (!cap.isOpened()) {
cout << "Failed to open the input video" << endl;
exit(5);}
for(;;){
cap>>currentImage;
if (!cap.grab())
{
cout << "\n End of video, looping" << endl;
cap.set(CV_CAP_PROP_POS_AVI_RATIO, 0);
}
waitKey(80);
cout<<"frame number= "<<cap.get(CV_CAP_PROP_POS_FRAMES)<<endl;
}
return 0;
}
I don't understand your problem " my output videos are twice as fast as the original". When you read a video file using cap>> currentimage; there is no time constraint. Only time constraint is fps. first frame is at time 0 and next at 1/fps. Now if you want to play video imshow(currentimage0); sleep(1/fps) and imshow(currentimage1)
Yeah, sure sure. I had a video writer with CV_CAP_PROP_FPS or something set as the fps, but I took it out as I was trying to strip the code to see where it skipped the frame.
I've never played a video using imshow(currentimage0) though, I always use imshow(currentimage). Do you think it would make a difference if I didn't use cap>>currentimage every loop, and instead used it once before the loop and then called currentimage0 to display?
Edit: Do you think if I remove the cap>>currentImage and make a Mat currentImage= cap after the if loop, it will stop grabbing twice? That seems positive.