0

I have a vector of type cvMat into which I have been storing frames taken from my computer's webcam. After storing 100 frames, I would like to play the frames back. If record is my vector of cvMats, I thought this might be done as so:

cvNamedWindow("play-back",CV_WINDOW_AUTOSIZE);
cvMoveWindow("play-back",100,100);
for (vector<Mat>::iterator iter = record.begin(); iter != record.end();++iter) {
   imshow("play-back",*iter);
}

When executed, the program seems to work well enough for storing the cvMats and getting input from the webcam, but when I attempt to get playback, the program seems to execute that portion of code very quickly -- so quickly, in fact, that I don't have time to appreciate the results. How might I be able to improve this code so that the playback is not so rushed?

user1936768
  • 659
  • 1
  • 9
  • 14

1 Answers1

3

You need to give control to OpenCV to actually render each image in the window and hold it for a brief period of time before switching to the next. You should add a call to cvWaitKey with a delay of, say 41ms (approximately 24 fps). Then you can check cvWaitKey's return value so that the user can stop the playback. Something like this:

cvNamedWindow("play-back",CV_WINDOW_AUTOSIZE);
cvMoveWindow("play-back",100,100);
for (vector<Mat>::iterator iter = record.begin(); iter != record.end();++iter) {
   imshow("play-back",*iter);
   if( cvWaitKey(41) == 27 ) // ESC
       break;
}
Community
  • 1
  • 1
Pablo
  • 8,644
  • 2
  • 39
  • 29
  • Taking into account what you had to say, and reading what was presented in the link you provided, I updated my code to the following: cvNamedWindow("play-back",CV_WINDOW_AUTOSIZE); cvMoveWindow("play-back",100,100); for (vector::iterator iter = record.begin(); iter != record.end();++iter) { imshow("play-back",*iter); cvWaitKey(41); } The result of this is that frames are recorded as usual, but the playback frame begins at the most recent frame obtained from the webcam. So, in other words, it doesn't seem like the iterator is beginning at the start of the sequence of cvMats?? – user1936768 Dec 29 '12 at 18:03
  • Although, mysteriously, when I vary the number of frames stored in the vector, the amount of time that the playback window freezes for also changes as though it were waiting longer for more frames to pass through it -- but these frames still aren't being shown (except for what appears to be the most recent frame from the webcam). – user1936768 Dec 29 '12 at 18:12
  • Ah! I believe I know what the issue is! When loading images, I am placing frames into the vector, I am making only shallow copies. So naturally, they would all be identical to the most recent assignment to the frame. What I really need to do, if my thinking is correct, is to create a deeper copy -- java, as I recall, has a .clone() method for that type of thing (not so?). Is there a similar thing in C++? – user1936768 Dec 29 '12 at 18:24
  • Yes, I've figured it out. Thank you. – user1936768 Dec 29 '12 at 18:30