It's time for our first hands-on example in video processing. Let's use the following steps to import and play-back the video we used before (singleball.avi
):
VideoReader
followed by read
:>> vObj = VideoReader('singleball.avi'), >> video = read(vObj); % read in all frames from video object
aviread
. Remember, for truecolor frames, it consists of a cdata
field with all the pixel values and an empty colormap
field:>> numOfFrames = get(vObj, 'NumberOfFrames'), >> for i = 1:numOfFrames, vid(i).cdata = video(:,:,:,i); % Frames are stored in cdata vid(i).colormap = []; % Colormap is empty end
movie
:>> hf = figure; >> movie(vid)
The last frame of the played-back video will look like as follows:
>> hf = figure; >> set(hf,'position',[200 200 vObj.WidthvObj.Height]); >> movie(hf,vid,5,vObj.FrameRate)
You just got a first glimpse at some of the things MATLAB is capable of when it comes to video. First we loaded an .avi
video into our workspace as a four-dimensional matrix and then we converted it to a video structure so that it can be played back properly using movie
. Finally, we fine-tuned our code to make sure that the position and dimensions of the playback window will be appropriate for our video and then we asked for a five-times repeat of the video at its original frame rate (specified by
vObj.FrameRate
). The call to
movie
was made with all possible inputs, which are in order of appearance; the handle to the figure we want our video to be played back in, the name of the video struct variable, the number of times we want the video to be played back, and the frame rate of the playback.