Play a Video Frame-by-Frame Using VideoReader?
53 views (last 30 days)
Show older comments
Hello -
I have been working with a GUI in which I have a video, whose data is read through VideoReader, which I would like to be played frame-by-frame using a "for"-loop. The reason for playing it frame-by-frame is that I also have a data file which I would like to plot alongside the video in real time. Right now I am simply performing a test to see if a "for"-loop can be used to play a video frame-by-frame, however, I do not seem to have had much success. I have included the test code below. I am not that familiar with VideoReader or GUIs, hence the difficulty. Any ideas on what I might be doing wrong?
% --- Executes on button press in PlayVideo.
function PlayVideo_Callback(hObject, eventdata, handles)
% hObject handle to PlayVideo (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
videofile = handles.videofile;
nFrames = handles.nFrames;
MOV = VideoReader(videofile);
axes(handles.VideoBox);
for i = 1:nFrames
img = read(MOV,1);
image(img);
end
guidata(hObject, handles);
0 Comments
Answers (1)
Image Analyst
on 17 Jun 2015
After the call to image(), put these lines:
drawnow; % Force immediate repainting of screen.
pause(0.25); % Pause a fraction of a second to slow down the video.
2 Comments
Image Analyst
on 17 Jun 2015
The pause is not necessary. It's just to slow it down. It's the drawnow that is necessary, though a long enough pause gives the same effect. Essentially the image() function sends a "message" to the graphics card (video adapter) to refresh the screen with the current image. But the operating system in a multitasking system can decide on what order to process messages. When you have a tight, intensive loop, it might decide that that loop takes precedence over other things like screen refreshes, disk I/O, playing music, etc. What was happening was that it was screaming through the loop and the screen refresh messages were piling up, essentially getting ignored until it thought it had enough time to act upon them, which was only after your loop had finished, then it did them all so fast you didn't see any of them except the final one. Putting a drawnow in there will force it to not continue until the screen has been refreshed/repainted/updated. So now you will see them all. However depending on how fast the loop is and how many frame there are, you might not see much. For example, it might display all 10 frames in a quarter of a second which is too fast for you to see. The pause() makes each image be visible for the specified amount of time before the loop continues on to the next image.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!