Time for action – reading and playing back a video

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):

  1. First, we import the video in a matrix, using VideoReader followed by read:
    >> vObj = VideoReader('singleball.avi'),
    >> video = read(vObj);  % read in all frames from video object
  2. Now, it is time to use the number of frames to create a video structure like the one that is generated by 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
  3. Since we have stored our video in the necessary format, we can now create the figure in which we have to display it and then call movie:
    >> hf = figure;
    >> movie(vid)

    The last frame of the played-back video will look like as follows:

    Time for action – reading and playing back a video
  4. Even though we managed to play back our video, there are a couple of things that are not optimal. One of them is the frame rate. The default frame rate used by movie, when we have not specified something different, is 12 fps. The other thing is that we have left the choice about the size of the window for the playback. Let's try to fix these things by specifying both the attributes, while at the same time ask MATLAB to playback the video five times. We'll start again from where we left off at step 2:
    >> hf = figure;
    >> set(hf,'position',[200 200 vObj.WidthvObj.Height]);
    >> movie(hf,vid,5,vObj.FrameRate)
  5. The result is, as expected, a loop of five consecutive playbacks of our video, at its proper frame rate.

What just happened?

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset