Video Acquisition and Viewing
This documentation shows how to acquire and view video from a USB Camera or a file when running a MATLAB Function or Simulink Model directly on a desktop computer.
To acquire video from a usb camera, you first need to setup the driver for the usb camera. This only needs to be done once. To do so type the following at the MATLAB Command Window:
If you are using a generic video camera for a Windows, Linux or Mac computer, you will need to select the "OS Generic Device" driver and install. For the purpose of this demo, we will use this support package.
If you do not have a generic video device (i.e. DCAM, GigE, Point Grey, etc...), see the following documentation to determine which support package to install:
To import video from a usb camera, use the imaq.VideoDevice system object:
videoReaderObject = imaq.VideoDevice('winvideo'); videoReaderObject.VideoFormat = 'MJPG_640x480'; videoReaderObject.ReturnedDataType = 'uint8';
To import video from a video file i.e. AVI OR MPEG, use the vision.VideoFileReader system object:
videoReaderObject = vision.VideoFileReader('visionface.avi'); videoReaderObject.VideoOutputDataType = 'uint8';
To extract each frame, the process is the same for both methods:
frame = step(videoReaderObject);
To import video from a usb camera, use the From Video Device block:
To import video from a video file i.e. AVI OR MPEG, use the From Multimedia File block:
To visualize video, use the following functions and blocks:
- MATLAB: imshow - easy to use for one-time visualizations, but executes slowly. On my computer, it visualizes one frame in 200 ms whereas the vision.VideoPlayer system object executes in 4 ms.
- MATLAB: vision.VideoPlayer - Does not execute as quickly as vision.DeployableVideoPlayer. Not supported for code generation.
- Simulink: Video Viewer - has a better user interface, but executes slowly. On my computer, it visualizes one frame in 15 ms whereas the "To Video Display" block executes in 4 ms.
Here is an example in MATLAB that shows how to acquire and visualize video. An if-else statement is used to allow the user to switch between using the camera or file for the video source:
Here is an example in Simulink that shows how to acquire and visualize video. A Manual Switch block is used to switch the video source from a usb camera to a video file. Double click on the "Manual Switch" block to switch the video source.