| Products & Services | Solutions | 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 |
In this example, you import an intensity image of a wheel from the MATLAB workspace and convert it to binary. Then, using the Opening and Label blocks, you count the number of spokes in the wheel. You can use similar techniques to count objects in other intensity images. However, you might need to use additional morphological operators and different structuring elements:
Create a new Simulink model, and add to it the blocks shown in the following table.
Block | Library | Quantity |
|---|---|---|
Image From File | Video and Image Processing Blockset > Sources | 1 |
Opening | Video and Image Processing Blockset > Morphological Operations | 1 |
Label | Video and Image Processing Blockset > Morphological Operations | 1 |
Video Viewer | Video and Image Processing Blockset > Sinks | 2 |
Constant | Simulink > Sources | 1 |
Relational Operator | Simulink > Logic and Bit Operations | 1 |
Display | Signal Processing Blockset > Signal Processing Sinks | 1 |
Position the blocks as shown in the following figure. The unconnected ports disappear when you set block parameters.

You are now ready to set your block parameters by double-clicking the blocks, modifying the block parameter values, and clicking OK.
Use the Image From File block to import your image. Set the File name parameter to testpat1.png. This is a 256-by-256 matrix image of 8-bit unsigned integers.
Use the Constant block to define a threshold value for the Relational Operator block. Set the Constant value parameter to 200.
Use the Video Viewer block to view the original image. Accept the default parameters.
Use the Relational Operator block to perform a thresholding operation that converts your intensity image to a binary image. Set the Relational Operator parameter to <.
If the input to the Relational Operator block is less than 200, its output is 1; otherwise, its output is 0. You must threshold your intensity image because the Label block expects binary input. Also, the objects it counts must be white.
Use the Opening block to separate the spokes from the rim and from each other at the center of the wheel. Use the default parameters.

The strel function creates a circular STREL object with a radius of 5 pixels. When working with the Opening block, pick a STREL object that fits within the objects you want to keep. It often takes experimentation to find the neighborhood or STREL object that best suits your application.
Use the Video Viewer1 block to view the opened image. Accept the default parameters.
Use the Label block to count the number of spokes in the input image. Set the Output parameter to Number of labels.

The Display block displays the number of spokes in the input image. Use the default parameters.
Connect the block 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 = 0
Solver pane, Type = Fixed-step
Solver pane, Solver = discrete (no continuous states)
The original image appears in the Video Viewer1 window. To view the image at its true size, right-click the window and select Set Display To True Size.

The opened image appears in the Video Viewer window. The following image is shown at its true size.

As you can see in the preceding figure, the spokes are now separate white objects. In the model, the Display block correctly indicates that there are 24 distinct spokes.

You have used the Opening and Label blocks to count the number of spokes in an image. For more information about these blocks, see the Opening and Label block reference pages in the Video and Image Processing Blockset Reference. If you want to send the number of spokes to the MATLAB workspace, use the To Workspace block in Simulink or the Signal to Workspace block in Signal Processing Blockset. For more information about STREL objects, see strel in the Image Processing Toolbox documentation.
![]() | Overview of Morphology | Correcting for Nonuniform Illumination | ![]() |

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 |