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.
Running this example requires a DSP System Toolbox™ license.
You can open the example model by typing
Create a new Simulink® model, and add to it the blocks shown in the following table.
Image From File
Computer Vision System Toolbox™ > Sources
Computer Vision System Toolbox> Morphological Operations
Computer Vision System Toolbox > Morphological Operations
Computer Vision System Toolbox > Sinks
Simulink > Sources
Simulink > Logic and Bit Operations
Simulink > Sinks
Use the Image
From File block to import your image. Set the File
name parameter to
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
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.
strel object 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
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 Model Configuration Parameters from the Simulation menu. Set the parameters as follows:
Solver pane, Stop
Solver pane, Type =
Solver pane, Solver =
(no continuous states)
Run the model.
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
Vision System Toolbox Reference. If you want to send the number of spokes to the
MATLAB workspace, use the To Workspace block in Simulink. For more information about STREL objects, see
strel in the Image Processing