This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Count Objects in an Image

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.

    Note:   Running this example requires a DSP System Toolbox™ license.

You can open the example model by typing

on the MATLAB command line.

  1. 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


    Video Viewer

    Computer Vision System Toolbox > Sinks



    Simulink > Sources


    Relational Operator

    Simulink > Logic and Bit Operations



    Simulink > Sinks


  2. 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.

  3. Use the Constant block to define a threshold value for the Relational Operator block. Set the Constant value parameter to 200.

  4. Use the Video Viewer block to view the original image. Accept the default parameters.

  5. 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.

  6. 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 class 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.

  7. Use the Video Viewer1 block to view the opened image. Accept the default parameters.

  8. Use the Label block to count the number of spokes in the input image. Set the Output parameter to Number of labels.

  9. The Display block displays the number of spokes in the input image. Use the default parameters.

  10. Connect the block as shown in the following figure.

  11. 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 time = 0

    • Solver pane, Type = Fixed-step

    • Solver pane, Solver = discrete (no continuous states)

  12. 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 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. For more information about STREL objects, see strel in the Image Processing Toolbox™ documentation.

Was this topic helpful?