Chapter 8. Acquiring and Processing Videos

Now that you are acquainted with the processes of importing videos in MATLAB and creating new ones from still images, it is time we explore another aspect of MATLAB video processing, which is video acquisition. In this chapter, we will learn how you can use MATLAB to acquire videos, or sequences of images, which are saved on your computer rather than the storage of the camera. Furthermore, we will learn about the storage space problems faced everyday by video processing professionals and discuss compression issues and tricks, so that we mitigate this problem. Finally, some difficulties of real-time video processing in MATLAB will be presented and explained. Various tips on speeding up such processes will also be given. While learning all these, we will investigate hands-on examples that will help you comprehend various implementation techniques.

In this chapter, we will cover:

  • How we can record videos in MATLAB, using the Image Acquisition Tool
  • What video compression is and why it is important
  • How we can work with uncompressed videos in MATLAB
  • How we can make a time-lapse video in MATLAB
  • How we can process videos in real time

So, let's start!

Using MATLAB for digital video recording

Till now we have used MATLAB as a powerful and versatile image processing tool. In the previous chapter, we also started exploring its video reading and writing capabilities. You may be surprised to find out that MATLAB has another useful functionality. It can be used to capture and record images and videos shot either by external cameras connected to a PC, or by internal cameras embedded in laptops. The tool that supports these capabilities is included in the Image Acquisition Toolbox and it is called Image Acquisition Tool.

The Image Acquisition Tool is a simple, yet effective, Graphical User Interface (GUI) that enables MATLAB to turn your PC to a Digital Video Recorder (DVR). It is invoked by simply typing its function name in the command line as follows:

>> imaqtool 

Once it is called, the window that appears looks like the following screenshot:

Using MATLAB for digital video recording

In the Image Acquisition Tool window, there are several subwindows that are used for acquisition purposes. We will briefly learn their properties here, so that you know your way around when we start using this tool.

The Hardware Browser window

The Hardware Browser window contains the list of acquisition devices that can be used by the Image Acquisition Tool. If your computer does not have a camera connected to it, or an embedded camera, this list will be empty. When a camera is detected by the Hardware Browser window, then its name, followed by its supported video formats are listed in the window. In our case, just one video camera with two supported formats was connected to the PC, so expanding its list of supported formats looked like the following screenshot:

The Hardware Browser window

The default video format was highlighted and written in bold, so that we know that it is preselected for us. Of course, we can choose the alternative format just by clicking on it.

The Information window

The Information window is used to provide additional information about whichever part of the list in the Hardware Browser window you have clicked on. In our case, clicking sequentially on the first three items of the list, leads to the following results:

The Information window

The Desktop Help window

The Desktop Help window contains helpful information about all the other windows in the GUI. It will change its contents depending on the window you have chosen to click on.

The Preview window

The Preview window is the part of the tool that provides a visual guide for you on what the camera is capturing. You can start or stop previewing what your camera sees, by clicking on the Start Preview or Stop Preview button respectively. From here, you can also start or stop the acquisition process, by clicking on the Start Acquisition or Stop Acquisition button respectively. There is also a Trigger button that may be used when you have set the acquisition trigger to be manual (through the Acquisition Parameters window) and finally there is an Export Data... button to save the acquired video, or sequence of images. This option allows you to export the video to a MAT-file, to the MATLAB Workspace, to the Movie Player, or to a video file using VideoWriter. All previews are displayed in a figure embedded in the Preview window, the size of which changes dynamically when you change the size of the window.

The Acquisition Parameters window

The Acquisition Parameters window is the panel in which all the settings for the acquisition process are defined. It contains five different tabs, named General, Device Properties, Logging, Triggering, and Region of Interest. Let's see what their settings are.

The General tab

The General tab is used to define two things:

  • The number of Frames per trigger that will be acquired (either a user-defined integer number or infinite).
  • The Color space tab that will be used for the acquired frames. It can be rgb, grayscale, or YCbCr.

The Device Properties tab

The Device Properties tab will be useful only in cases where your acquisition device allows it. In our examples it won't be the case, since our camera did not support its properties to be set. Other cameras may give you the choice of setting properties such as its exposure, or frame rate.

The Logging tab

The Logging tab allows you to define where the acquired frames will be saved, under which filename, how high your allowed memory limit will be set, and finally the output file format you want to use. More specifically:

  • The Log to setting defines where the acquired frames will be saved. The possible choices are as follows:
    • Save to Memory, in which case the data will be lost if you do not use the Export Data field which is available in the Preview window.
    • Save to Disk, in which case the data will be saved to your computer's disk using the VideoWriter function, in the path and under the filename that you choose in the Disk Logging (VideoWriter) setting.
    • Save to Disk and memory, in which case the data will be stored both in your computer's disk and memory.
  • The Memory logging setting lets you define the Memory limit (in MB) for the stored data, in case you have selected it to be saved in the RAM.
  • Disk Logging (VideoWriter) allows you to define the folder where the data will be stored, as well as the Filename of the video stream, or sequence of images (depending on your choice of output format). Optionally, you can also select the option Automatically increment filename that ensures that your consecutive video files will be saved with the same name, but different consecutive numbering (for example, name_001.avi, name_002.avi, and so on).

The Triggering tab

The Triggering tab allows you to change the following settings:

  • Number of triggers should either be set to a user-specified number, or to Infinite (you get to decide when your video will stop, by clicking on the Stop Acquisition button in the Preview window).
  • Trigger type should either be set to Immediate (you start the acquisition by clicking on the Start Acquisition button in the Preview window), or to Manual (it allows usage of the Trigger button in the Preview window).
  • Hardware triggering is a setting that is visible only when your device supports it (it will not be available in our examples). This setting allows for your device to perform triggering based on parameters that can be altered, but are device-specific.

The Region of Interest tab

The Region of Interest tab allows you to define a region of the frame that you want to be acquired. By default, the entire frame will be saved, but you can limit the area either by clicking on the Select or Edit button and then defining a rectangular area of the frame shown in the Preview window (you must have clicked on Start Preview first), or by setting the X-offset and the Y-offset as shown in the following screenshot:

The Region of Interest tab

The Session Log window

The Session Log window is similar to the Command History window of the MATLAB environment, which was presented in the first chapter. It is a very useful part of the Image Acquisition Tool, since it dynamically presents the equivalent command line actions for every choice you make in the GUI. It can be used to teach you some of the core functions used for image acquisition, so that later on you can use them in your own MATLAB code.

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

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