Loading videos in MATLAB

Before we start discussing how to create our own videos, it is important to first see how MATLAB handles videos. In fact, video processing is one of the areas in which MATLAB has been evolving a lot over these past years. As opposed to image processing, where imread was introduced in early versions of the software and could be used for loading most popular image formats, the respective function for video loading has been changed a lot. The main reason behind the changes was the differences in video compression formats, which did not allow for a single efficient function that could handle opening every possible one. In this section, we will present the different functions that can be used in MATLAB for video importing. This way, readers with previous versions of MATLAB will be able to use the function they feel most comfortable with.

Loading videos with aviread

The first function used for reading videos in MATLAB was aviread. This function still exists in Version 2012b, but it is scheduled to be removed in future versions. Furthermore, it has a limited functionality with respect to the video types that it can read, since it is only designed to open .avi files. A typical usage of aviread is shown as follows, using one of the videos included in MATLAB (singleball.avi):

>> A = aviread('singleball.avi'),

This command gives the following result:

Loading videos with aviread

As you can see, the resulting variable, A, is of type struct. Its size is equal to the number of frames comprising the video. Luckily, the number of frames was not big, so we had no problem importing all of them in our workspace. Imagine if we tried to import a 25p video with one hour duration. Then, we would have needed to store 90000 frames! This would lead to a certain memory problem, especially if our video was high resolution.

To avoid this problem, it is common to first inspect the videos we are about to process and then decide on a strategy for importing them. To do this, we can use aviinfo, a function that complements aviread and aims at drawing information from a video file. To use it, we just type it in the command line with the video filename as input:

>> aviinfo('singleball.avi'),
Loading videos with aviread

Now, let's say that we want to import just the first 10 frames of our video. The only thing needed is to identify the frames we want to load in aviread:

>> A = aviread('singleball.avi', 1:10);

It can be easily deduced that adding a step of 2 in the frames vector, that is, using 1:2:10 instead of 1:10, would lead to skipping even numbered frames, leading to a speed-up of our image if the playback rate is kept steady.

Since the video imported is stored in a struct, we must find a way to manipulate it using what we know, that is, multi-dimensional matrices. If we type the name of our struct in the command line, we get:

>> A

The output of the preceding command is as follows:

A = 
1x10struct array with fields:
cdata
colormap

This means that the struct has two fields; cdata and colormap. The colormap field is useful only in the case of videos with indexed images as frames (no color information is stored in the pixel data). If the color information is directly stored in the pixel data of the frames (called truecolor images in this case), as in all the cases we have seen so far in this book, colormap will be an empty matrix. The cdata field holds all pixel information.

If we want to access the 5th frame of our imported video, we will have to use the following command:

>> frame5 = A(5).cdata;

If, however, we want to read all 10 frames to a four-dimensional matrix, we will have to use a for loop:

>> for i = 1:10, vid(:,:,:,i) = A(i).cdata; end
>> size(vid)

The output of the preceding code is as follows:

ans =
   360   480     3    10

The second line of code was used to see the size of our result, which is normal, as it shows that our generated matrix has 360 rows, 480 columns, 3 colors, and 10 frames.

Tip

A big disadvantage of aviread for Unix users is that it can only handle uncompressed .avi files. This is why external toolboxes for video processing were extensively used in the past. The most important one is VideoIO, which is still well-maintained and can be found at http://sourceforge.net/projects/videoio/

Loading videos with mmreader

The next attempt for a video reading function in MATLAB was mmreader. This function (or class) was part of the introduction of object oriented programming methods in MATLAB and it supported more video formats than aviread. On the downside, the speed of video importing, using mmreader was reduced. This function will be abandoned in future releases of MATLAB, so it would be wise not to use it extensively in your work.

As already mentioned, mmreader is an object oriented function, meaning that its output is a multimedia reader object that can be used to import video data from a file. The function that will then import the video from the constructed object is called read. The process that must be used to achieve the same results shown in the previous paragraph for aviread, is as follows:

>> vObj = mmreader('singleball.avi'),
>> videoA = read(vObj);        % read in all frames from video object
>> videoB = read(vObj,[1 10]); % read in only the 10 first frames 

This process leads to the following result in our workspace:

Loading videos with mmreader

As you can see in the figure, the object constructed has an mmreader type, while the two imported videos are four-dimensional 8-bit integer matrices (rows x columns x colors x frames). VideoB should be identical to the vid matrix generated in the previous example (you can check it using the size function). Once again, MATLAB produces a warning about mmreader, having to do with its removal in future versions.

In case we need to inspect our video file before we load it in MATLAB, we can take advantage of the fact that we have to create a video object first and use the get function to inspect it. This can be performed as follows:

>> get(vObj)
Loading videos with mmreader

Loading videos with VideoReader

The VideoReader function (or class) has almost identical usage to mmreader. The only visible differences are that it generates a VideoReader object and it usually performs a little faster. To achieve the same results as in previous paragraphs, we have to type in the following:

>> vObj = VideoReader('singleball.avi'),
>> videoA = read(vObj);        % read in all frames from video object
>> videoB = read(vObj,[1 10]); % read in only the 10 first frames
>> size(videoB)

The output of the preceding code will be as follows:

Loading videos with VideoReader

As you can see from the results of the previous commands, the results are identical to previous methods. The second video also has the same number of frames as in previous tries. Of course, this time we had no complaints from MATLAB about our selection of function, since VideoReader is the most recently introduced function for video file reading. Using get on the result of VideoReader will, as you can easily prove yourself, produce the same result as the one produced for the mmreader result. If we want to use some of the fields produced by get, for example the number of frames, we can do it by typing:

>> numOfFrames = get(vObj,'NumberOfFrames')

The preceding command will give the following result:

numOfFrames =
    45

Choosing which function to use for video reading

The choice of the best video reading function for your needs is usually made taking into account three basic parameters; the version of MATLAB you have access to, the format of the video file you want to process, and the desired speed of processing.

The first parameter cannot be covered extensively here, since the book is based on MATLAB version 2012b. However, you should take into account that versions of MATLAB prior to 2007b included only aviread. Versions from 2007b to 2010b supported both aviread and mmreader, and finally since 2010b, all three functions can be used. However, as already mentioned, you should cautiously use the two oldest functions because they have been scheduled to be replaced in future versions.

Now, let's see what are the formats that each function supports. As its name implies, aviread only reads .avi video files, which can only be uncompressed in Unix systems. The two other functions have pretty much the same functionality when it comes to video formats. This can be proven using the getFileFormats method that is available in both mmreader and VideoReader. To see them, we can type in the following commands:

>> mmreader.getFileFormats()
>> VideoReader.getFileFormats()

Both these calls have the same result, which in Windows looks like as follows:

Video File Formats:
    .asf - ASF File
    .asx - ASX File
    .avi - AVI File
    .m4v - MPEG-4 Video
    .mj2 - Motion JPEG2000
    .mov - QuickTime movie
    .mp4 - MPEG-4
    .mpg - MPEG-1
    .wmv - Windows Media Video

Regarding the speed of processing, the improvements are neither so spectacular nor so definitive that they will dictate the use of either one of the two new functions. The faster of the three is aviread, but as it is nearly obsolete and it does not support a variety of formats it should be avoided.

Taking all these facts into consideration, your choice of the appropriate function should probably be the most recent function supported by your version of MATLAB. Therefore, from now on we will be using VideoReader for all the video importing tasks we will demonstrate.

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

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