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.
You can open the example model by typing
on the MATLAB command line.
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
DSP System Toolbox > Sinks
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 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.
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 Computer Vision System Toolbox 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 DSP System Toolbox. For more information about STREL objects, see strel in the Image Processing Toolbox documentation.