| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Video and Image Processing Blockset |
| Contents | Index |
| Learn more about Video and Image Processing Blockset |
| On this page… |
|---|
Blocks That Support Multimedia Files |
The Video and Image Processing Blockset software contain blocks that you can use to import and ex[prt multimedia files. These blocks include the From Multimedia File block and the To Multimedia File block. If you are working on a Windows platform, these blocks perform best on platforms with DirectX Version 9.0 or later and Windows Media Version 9.0 or later. These blocks also support code generation.
In this example, you use the From Multimedia File block to import a video stream into a Simulink model and the To Video Display block to view it. This procedure assumes you are working on a Windows platform:
Create a new Simulink model, and add to it the blocks shown in the following table.
Block | Library | Quantity |
|---|---|---|
From Multimedia File | Video and Image Processing Blockset > Sources | 1 |
To Video Display | Video and Image Processing Blockset > Sinks | 1 |
Locate a multimedia file that you want to import into Simulink. If you do not have access to a multimedia file, the Video and Image Processing Blockset software has sample multimedia files you can use to complete this procedure.
Use the From Multimedia File block to import the multimedia file into the model. Double-click the From Multimedia File block:
If you do not have your own multimedia file, enter vipmen.avi for the File name parameter.
If the multimedia file is on your MATLAB path, enter the filename for the File name parameter.
If the file is not on your MATLAB path, use the Browse button to locate the multimedia file.
Set the Image signal parameter to Separate color signals.
By default, the Number of times to play file parameter is set to inf. The model continues to play the file until the simulation stops.
Use the To Video Display block to view the multimedia file. Set the Image signal parameter to Separate color signals.
Connect the blocks so your model looks similar to the following figure.

Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. On the Solver pane, set the parameters as follows:
Stop time = 20
Type = Fixed-step
Solver = Discrete (no continuous states)
View your video in the To Video Display window that automatically appears when you start your simulation. This window closes as soon as the simulation stops.

You have now imported and dobsisplayed a multimedia file in your Simulink model. In Exporting to Multimedia Files, you manipulate your video stream and export it to a multimedia file. For more information on the blocks used in this example, see the From Multimedia File and To Video Display block reference pages in the Video and Image Processing Blockset Reference. To listen to audio associated with an AVI file, use the To Audio Device block in Signal Processing Blockset software.
The Video and Image Processing Blockset blocks enable you to export video data from your Simulink model. In this section, you use the To Multimedia File block to export an multimedia file from your model.
If the model you created in Importing and Viewing Multimedia Files is not open on your desktop, you can open an equivalent model by typing
doc_export2
at the MATLAB command prompt.

Click-and-drag the following blocks into your model.
Block | Library | Quantity |
|---|---|---|
To Multimedia File | Video and Image Processing Blockset > Sinks | 1 |
Gain | Simulink > Math Operations | 3 |
Use the Gain blocks to increase the red, green, and blue values of the video stream. This increases the contrast of the video. Set the block parameters as follows:
Main pane, Gain = 1.2
Signal Attributes pane, Output data type = Inherit: Same as input
Use the To Multimedia File block to export the video to a multimedia file. Set the block parameters as follows:
Output file name = my_output.avi
Write = Video only
Image signal = Separate color signals

Connect the blocks as shown in the following figure. You might need to resize some blocks to do so.

You are now ready to set your block parameters by double-clicking the blocks, modifying the block parameter values, and clicking OK.
If you have not already done so, set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. On the Solver pane, set the parameters as follows:
Stop time = 20
Type = Fixed-step
Solver = Discrete (no continuous states)
You can view your video in the To Video Display window. By increasing the red, green, and blue color values, you increased the contrast of the video. The To Multimedia File block exports the video data from the Simulink model to a multimedia file that it creates in your current folder.

You have now manipulated your video stream and exported it from a Simulink model to a multimedia file. For more information, see the To Multimedia File block reference page in the Video and Image Processing Blockset Reference.
Before you can analyze or operate on your data, you must import it into your Simulink model. Blocks from the Sources library, such as the From Multimedia File block, can help you with this type of task.
In this section, you use the From Multimedia File block to import video from an AVI file into your model and the Video Viewer block to view it:
Create a new Simulink model, and add to it the blocks shown in the following table.
Block | Library | Quantity |
|---|---|---|
From Multimedia File | Video and Image Processing Blockset > Sources | 1 |
Video Viewer | Video and Image Processing Blockset > Sinks | 1 |
Use the From Multimedia File block to import an AVI file into the model. Double-click the From Multimedia File block. The Video and Image Processing Blockset software has sample AVI files you can use to complete this procedure.
If you do not have your own AVI file, enter barcodes.avi for the File name parameter.
If the AVI file is on your MATLAB path, enter the AVI filename for the File name parameter.
If the file is not on your MATLAB path, use the Browse button to locate the AVI filename.
Image signal = Separate color signals
By default, the Number of times to play file parameter is set to inf. The model continues to play the file until the simulation stops.
Use the Video Viewer block to view the AVI file. Click the File menu of the Video Viewer GUI to set the Image signal parameter to Separate color signals.
Connect the blocks so your model looks similar to the following figure.

Set the configuration parameters. Open the Configuration dialog box by selecting Simulation > Configuration Parameters. Set the parameters as follows:
Solver pane, Stop time = 20
Solver pane, Type = Fixed-step
Solver pane, Solver = Discrete (no continuous states)
View your video in the Video Viewer window that automatically appears when you start your simulation.

Note The video that is displayed in the Video Viewer window runs as fast as Simulink processes the video frames. If you are on a Windows platform and you want to run the video at the frame rate that corresponds to the input sample time, use the To Video Display block. |
You have now imported and displayed video data in your Simulink model. In Exporting to AVI Files, you manipulate your video stream and export it to an AVI file. For more information on the blocks used in this example, see the From Multimedia File and Video Viewer block reference pages in the Video and Image Processing Blockset Reference. To listen to audio associated with an AVI file, use the To Audio Device block in Signal Processing Blockset software.
Note The Video Viewer block is supported on all platforms, but it does not support code generation. If you are on a Windows platform, you can use the To Video Display block to display video data. This block supports code generation. For more information, see the To Video Display block reference page in the Video and Image Processing Blockset Reference. |
The Video and Image Processing Blockset blocks enables you to export video data from your Simulink model. In the following procedure, you use the To Multimedia File block to export video data from your model into an AVI file:
If the model you created in Importing and Viewing AVI Files is not open on your desktop, open an equivalent model by typing
doc_export
at the MATLAB command prompt.

Click-and-drag the blocks shown on the following table into your model.
Block | Library | Quantity |
|---|---|---|
To Multimedia File | Video and Image Processing Blockset > Sinks | 1 |
Gain | Simulink > Math Operations | 2 |
Change the inputs to the To Multimedia File block. Set the block parameters as follows:
Write = Video only
Image signal = Separate color signals
Connect the blocks as shown in the following figure. You might need to resize some blocks to do so.

Use the Gain block to change the green values of the video stream. Set the block parameters as follows:
Main pane, Gain = 0.3
Signal Attributes pane, Output data type = Inherit:Same as input
Use the Gain1 block to change the blue values of the video stream. Set the block parameters as follows:
Main pane, Gain = 1.5
Signal Attributes pane, Output data type = Inherit:Same as input
Use the To Multimedia File block to export the video to an AVI file. Set the File name parameter to my_test_file.avi.

If you have not already done so, set the configuration parameters. Open the Configuration dialog box by selecting Simulation > Configuration Parameters. Set the parameters as follows:
Solver pane, Stop time = 20
Solver pane, Type = Fixed-step
Solver pane, Solver = Discrete (no continuous states)
You can view your video in the Video Viewer window. The To Multimedia File block exports the video data from the Simulink model to an AVI file that it creates in your current folder.

You have now manipulated your video stream and exported it from a Simulink model to an AVI file. For more information, see the To Multimedia File block reference page in the Video and Image Processing Blockset Reference.
You can use the Insert Text block to overlay text on video stream. In this example, you add a running count of the number of video frames to a video.
Create a new Simulink model, and add to it the blocks shown in the following table.
Block | Library | Quantity |
|---|---|---|
From Multimedia File | Video and Image Processing Blockset > Sources | 1 |
Insert Text | Video and Image Processing Blockset > Text & Graphics | 1 |
Video Viewer | Video and Image Processing Blockset > Sinks | 2 |
Position the blocks as shown in the following figure.

Use the From Multimedia File block to import the video into the Simulink model. Set the Image color space parameter to Intensity.
Open the Surveillance Recording demo by typing
vipsurveillance
at the MATLAB command prompt.
Click-and-drag the Frame Counter block from the demo model into your model. This block counts the number of frames in an input video.
Use the Insert Text block to annotate the video stream with a running frame count. Set the block parameters as follows:
Main pane, Text = ['Frame count' sprintf('\n') 'Source frame: %d']
Main pane, Location = [85 2]
Main pane, Color value = 1
Font pane, Font face = LucindaTypewriterRegular
By setting the Text parameter to ['Frame count' sprintf('\n') 'Source frame: %d'], you are asking the block to print Frame count on one line and theSource frame: on a new line. Because you specified %d, an ANSI C printf-style format specification, the Variables port appears on the block. The block takes the port input (it is expecting a decimal) and substitutes it for the %d in the string. You used the Location parameter to specify where to print the text. In this case, the location is 85 rows down and 2 columns over from the top left corner of the image.
Use the Video Viewer blocks to view the original and annotated videos. Accept the default parameters.
Connect the blocks as shown in the following figure.

Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows:
Solver pane, Stop time = inf
Solver pane, Type = Fixed-step
Solver pane, Solver = Discrete (no continuous states)
Run the model.
The original video appears in the Video Viewer window.

The annotated video appears in the Video Viewer1 window.

You have now added descriptive text to a video stream. For more information, see the Insert Text block reference page in the Video and Image Processing Blockset Reference. For related information, see the Draw Shapes and Draw Markers block reference pages.
You can use the Insert Text block to overlay text on a video stream at two separate locations in the video frame.
Create a new Simulink model, and add to it the blocks shown in the following table.
Block | Library | Quantity |
|---|---|---|
From Multimedia File | Video and Image Processing Blockset > Sources | 1 |
Insert Text | Video and Image Processing Blockset > Text & Graphics | 1 |
Video Viewer | Video and Image Processing Blockset > Sinks | 1 |
Constant | Simulink > Sources | 1 |
Position the blocks as shown in the following figure.

Use the From Multimedia File block to import the video stream into the Simulink model. Accept the default parameters.
Use the Insert Text block to annotate the video with two text strings. Set the block parameters as follows:
Main pane, Text = 'Text position: Row %d and Column %d'
Main pane, Location = [[5 10]' [80 10]']
By setting the Text parameter to 'Text position: Row %d and Column %d', you are asking the block to replace each conversion specification (%d) with a decimal input to the Variables port. You used the Location parameter to specify where to print each text string. In this case, the block places the top-left corner of the text box that surrounds the first text string 5 rows down and 10 rows over from the top left corner of the image. The block places the second text string 80 rows down and 10 rows over.
Use the Constant block to specify the decimal values input into the Insert Text block's Variables port. Because the conversion specification is %d, the values must be an integer data type. Set the block parameters as follows:
Main pane, Constant value = [[5 10]' [80 10]']
Main pane, clear the Interpret vector parameters as 1–D check box.
Signal Attributes pane, Output data type = uint8
The Insert Text block substitutes the values from the first column of the Constant value parameter into the first text string and the values from the second column into the second text string.
Use the Video Viewer blocks to view the annotated image. Accept the default parameters.
Connect the blocks as shown in the following figure.

Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows:
Solver pane, Stop time = inf
Solver pane, Type = Fixed-step
Solver pane, Solver = Discrete (no continuous states)
Run the model.
The annotated video appears in the Video Viewer window.

Alternatively, you can input two string values at the Variables port.
On the Insert Text block dialog box, set the Text parameter to'%s region of interest'.
You are asking the block to replace the %s conversion specification with a string input to the Variables port.
Use the Constant block to specify the strings to substitute into the first and second text strings. Because the conversion specification is %s, the values must be 8-bit unsigned integer data types. Set the Constant value parameter to [uint8('First') 0 uint8('Second')].
Run the model.
The annotated video appears in the Video Viewer window.

You have now added descriptive text to a video stream. For more information, see the Insert Text block reference page in the Video and Image Processing Blockset Reference.
In this section, you use To Multimedia File and Enabled Subsystem blocks to save portions of one AVI file to three separate AVI files.
Create a new Simulink model, and add to it the blocks shown in the following table.
Block | Library | Quantity |
|---|---|---|
From Multimedia File | Video and Image Processing Blockset > Sources | 1 |
Insert Text | Video and Image Processing Blockset > Text & Graphics | 1 |
Enabled Subsystem | Simulink > Ports & Subsystems | 3 |
To Multimedia File | Video and Image Processing Blockset > Sinks | 3 |
Counter | Signal Processing Blockset > Signal Management > Switches and Counters | 1 |
Compare To Constant | Simulink > Logic and Bit Operations | 5 |
Logical Operator | Simulink > Logic and Bit Operations | 1 |
Stop Simulation | Simulink > Sinks | 1 |
Place the blocks so that your model looks similar to the one in the following figure.

Use the From Multimedia File block to import an AVI file into your model. Set the parameters as follows:
Uncheck Inherit sample time from file checkbox
Set Desired sample time parameter = 1/30
Use the Insert Text block to annotate the video stream with the frame numbers. Set the parameters as follows:
Text = 'Frame %d'
Location = [10 10]
Color = [0 1 0]
The block writes the frame number in green in the upper-left corner of the output video stream.
Double-click each Enabled Subsystem block, and click-and-drag one of the To Multimedia File blocks into it.
Inside each Enabled Subsystem, connect the blocks so that your subsystem looks similar to the one in the following figure.

Use the To Multimedia File blocks to send the video stream to three separate AVI files. Set the block parameters as follows:
Output file name = output1.avi, output2.avi, and output3.avi, respectively
Write = Video only
Each enabled subsystem should now look similar to the subsystem shown in the following figure.

Use the Counter block to count the number of video frames. You use this information to specify which frames are sent to which file. Set the block parameters as follows:
Count event = Free running
Initial count = 1
Output = Count
Clear the Reset input check box.
Sample time = 1/30
Count data type = uint16
Use the Compare to Constant block to send frames 1 to 9 to the first AVI file. Set the block parameters as follows:
Operator = <
Constant value = 10
Use the Compare to Constant1 and Compare to Constant2 blocks to send frames 10 to 19 to the second AVI file. Set the Compare to Constant1 block parameters as follows:
Operator = >=
Constant value = 10
Set the Compare to Constant2 block parameters as follows:
Operator = <
Constant value = 20
Use the Compare to Constant3 block to send frames 20 to 30 to the third AVI file. Set the block parameters as follows:
Operator = >=
Constant value = 20
Use the Compare to Constant4 block to stop the simulation when the video reaches frame 30. Set the block parameters as follows:
Operator = ==
Constant value = 30
Output data type mode = boolean
Connect the blocks so that your model resembles the one in the following figure.

Set the configuration parameters. Open the Configuration dialog box by selecting Simulation > Configuration Parameters. Set the parameters as follows:
Solver pane, Type = Fixed-step
Solver pane, Solver = Discrete (no continuous states)
The model saves the three output AVI files in your current folder.
View the resulting files by typing the following commands at the MATLAB command prompt:
mplay output1.avi mplay output2.avi mplay output3.avi
Then, press the Play button on the MPlay GUI.

You have now sent portions of an AVI file to three separate AVI files using an Enabled Subsystem block, a To Multimedia File block, and a trigger signal. For more information on the blocks used in this example, see the From Multimedia File, Insert Text, Enabled Subsystem, and To Multimedia File block reference pages.
In this example, you use the From Multimedia File block to import a video stream into a Simulink model. You also use Signal Processing Blockset software From Wave File block to import an audio stream into the model. Then you write this audio and video to a single file using the To Multimedia File block.
This procedure assumes you are working on a Windows platform:
Create a new Simulink model, and add to it the blocks shown in the following table.
Block | Library | Quantity |
|---|---|---|
From Multimedia File | Video and Image Processing Blockset > Sources | 1 |
From Wave File | Signal Processing Blockset > Signal Processing Sources | 1 |
To Multimedia File | Video and Image Processing Blockset > Sinks | 1 |
Connect the blocks so your model looks similar to the following figure.

Use the From Multimedia File block to import a multimedia file into the model. Deselect the Inherit sample time from file check box. (Deselecting the checkbox enables Desired sample time parameter.) Accept the following default parameters.

The From Multimedia File block inherits its sample time from vipmen.avi. For video signals, the sample time is equivalent to the frame period. Because this file's frame rate is 30 frames per second (fps) and the frame period is defined as 1/frame rate, the frame period of this block is 0.0333 seconds per frame.
Use the From Wave File block to import an audio file into the model. To calculate the output frame size, divide the frequency of the audio signal (22050 samples per second) by the frame rate (which is approximately 30 frames per second) to get 735 samples per frame. Set the Samples per output frame parameter to 735.

The frame period of the audio signal must match the frame period of the video signals, which is 0.0333 seconds per frame. Since the frame period is also defined as the frame size divided by frequency, you can calculate the frame period of the audio signal by dividing the frame size of the audio signal (735 samples per frame) by the frequency (22050 samples per second) to get 0.0333 seconds per frame. Alternatively, you can verify that the frame period of the audio and video signals is the same using a Simulink Probe block.
Use the To Multimedia File to output the audio and video signals to a single multimedia file. Select Video and audio for the Write parameter and One multidimensional signal for the Image signal parameter. Accept the other default parameters.

Set the configuration parameters. Open the Configuration dialog box by selecting Simulation > Configuration Parameters. On the Solver pane, set the parameters as follows:
Stop time = 10
Type = Fixed-step
Solver = Discrete (no continuous states)
Run your model. The model creates a multimedia file called output.avi in your current folder.
Play the multimedia file using a media player. The original video file now has an audio component to it.
You have now combined audio and video information into a single file using the To Multimedia File block. For more information, see the To Multimedia File block reference page in the Video and Image Processing Blockset Reference.
![]() | Working with Live Video | Working with MATLAB Workspace Variables | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |