Read Video Files - MATLAB & Simulink.pdf. Read Video Files - MATLAB & Simulink.pdf. Open. Extract. Open with. Si
Read Video Files Read All Frames in Video File This example shows how to read and store data from all frames in a video file, display one frame, and then play all frames at the video's frame rate. Construct a VideoReader object associated with the sample file, xylophone.mp4. vidObj = VideoReader('xylophone.mp4');
Determine the height and width of the frames. vidHeight = vidObj.Height; vidWidth = vidObj.Width;
Create a MATLAB® movie structure array, s. s = struct('cdata',zeros(vidHeight,vidWidth,3,'uint8'),... 'colormap',[]);
Read one frame at a time using readFrame until the end of the file is reached. Append data from each video frame to the structure array. k = 1; while hasFrame(vidObj) s(k).cdata = readFrame(vidObj); k = k+1; end
Get information about the movie structure array, s. whos s
Name Size Bytes Class Attributes s 1x141 32503552 struct s is a 1by141 structure array, containing data from the 141 frames in the video file. Display the fifth frame stored in s. image(s(5).cdata)
Resize the current figure and axes based on the video's width and height. Then, play the movie once at the video's frame rate using the movie function. set(gcf,'position',[150 150 vidObj.Width vidObj.Height]); set(gca,'units','pixels'); set(gca,'position',[0 0 vidObj.Width vidObj.Height]); movie(s,1,vidObj.FrameRate);
Close the figure. close
Read All Frames Beginning at Specified Time Read part of a video file starting 0.5 second from the beginning of the file. Construct a VideoReader object associated with the sample file, 'xylophone.mp4'.
vidObj = VideoReader('xylophone.mp4');
Specify that reading should begin 0.5 second from the beginning of the file by setting theCurrentTime property. vidObj.CurrentTime = 0.5;
Create an axes to display the video. Then, read video frames until the end of the file is reached. currAxes = axes; while hasFrame(vidObj) vidFrame = readFrame(vidObj); image(vidFrame, 'Parent', currAxes); currAxes.Visible = 'off'; pause(1/vidObj.FrameRate); end
Read Video Frames Within Specified Time Interval Read part of a video file from 0.6 to 0.9 second. Construct a VideoReader object associated with the sample file, 'xylophone.mp4'. vidObj = VideoReader('xylophone.mp4');
Specify that reading should begin 0.6 second from the beginning of the file by setting theCurrentTime property. vidObj.CurrentTime = 0.6;
Read one frame at a time until the CurrentTime reaches 0.9 second. Append data from each video frame to the structure array, s. k = 1; while vidObj.CurrentTime