How can I programmatically display specific frames in a video using Video Viewer?
14 views (last 30 days)
Show older comments
MathWorks Support Team
on 24 Jan 2024
Answered: MathWorks Support Team
on 25 Jan 2024
I am using MATLAB's Video Viewer app to play videos, and I want to display some selected frames. Specifically, I have a vector containing specific frame indices that I wish to display in sequence, with a pause between each. Currently, the app only provides a manual option to jump to frames using an icon. Is there a method to achieve this programmatically?
Accepted Answer
MathWorks Support Team
on 24 Jan 2024
Currently, the Video Viewer app does not provide a built-in programmatic interface for controlling playback or displaying specific frames. However, you can use MATLAB functions directly to achieve a similar outcome.
As a workaround, you can use the "VideoReader" object to access frames and then display them using the "imshow" function. Below is an example of how to display a sequence of frames from a video with a pause of 1 second between each frame:
% Create a VideoReader object to read the video file
v = VideoReader('xylophone_video.mp4');
% Vector of frame numbers you want to display
frameIndices = [1, 25, 50, 75, 100];
% Loop through the frame indices
for i = 1:length(frameIndices)
% Set the current time of the video reader object
frameIndex = frameIndices(i);
if frameIndex >= 1 && frameIndex <= v.NumFrames
% Get the timestamp of video frame to read
v.CurrentTime = (frameIndex-1) / v.FrameRate;
% Read the frame at the specified time
frame = readFrame(v);
% Display the frame
imshow(frame);
title(['Frame index: ' num2str(frameIndex)]);
% You can apply a fixed pause duration
pause(1); % Pauses for 1 second
else
disp(['Frame number ' num2str(frameIndex) ' is out of range.']);
end
end
For more details about "VideoReader", "readFrame", and "writeVideo", please refer to the following links:
VideoReader:
readFrame:
writeVideo:
0 Comments
More Answers (0)
See Also
Categories
Find more on Audio and Video Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!