vision.ConnectedComponentLabeler System object

Package: vision

Label and count the connected regions in a binary image

Description

The ConnectedComponentLabeler object labels and counts the connected regions in a binary image. The System object can output a label matrix, where pixels equal to 0 represent the background, pixels equal to 1 represent the first object, pixels equal to 2 represent the second object, and so on. The object can also output a scalar that represents the number of labeled objects.

Construction

H = vision.ConnectedComponentLabeler returns a System object, H, that labels and counts connected regions in a binary image.

H = vision.ConnectedComponentLabeler(Name,Value) returns a label System object, H, with each property set to the specified value. You can specify additional name-value pair arguments in any order as (Name1, Value1,...,NameN,ValueN).

Code Generation Support
Supports MATLAB® Function block: Yes
System Objects in MATLAB Code Generation.
Code Generation Support, Usage Notes, and Limitations.

Properties

Connectivity

Which pixels are connected to each other

Specify which pixels are connected to each other as either 4 or 8. If a pixel should be connected to the pixels on the top, bottom, left, and right, set this property to 4. If a pixel should be connected to the pixels on the top, bottom, left, right, and diagonally, set this property to 8. The default is 8.

LabelMatrixOutputPort

Enable output of label matrix

Set to true to output the label matrix. Both the LabelMatrixOutputPort and LabelCountOutputPort properties cannot be set to false at the same time. The default is true.

LabelCountOutputPort

Enable output of number of labels

Set to true to output the number of labels. Both the LabelMatrixOutputPort and LabelCountOutputPort properties cannot be set to false at the same time. The default is true.

OutputDataType

Output data type

Set the data type of the output to one of Automatic, uint32 , uint16, uint8. If this property is set to Automatic, the System object determines the appropriate data type for the output. If it is set to uint32, uint16, or uint8, the data type of the output is 32-, 16-, or 8-bit unsigned integers, respectively. The default is Automatic.

OverflowAction

Behavior if number of found objects exceeds data type size of output

Specify the System object's behavior if the number of found objects exceeds the maximum number that can be represented by the output data type as Use maximum value of the output data type, or Use zero . If this property is set to Use maximum value of the output data type, the remaining regions are labeled with the maximum value of the output data type. If this property is set to Use zero, the remaining regions are labeled with zeroes. This property applies when you set the OutputDataType property to uint16 or uint8. The default is Use maximum value of the output data type.

Methods

cloneCreate connected component labeler object with same property values
getNumInputsNumber of expected inputs to step method
getNumOutputsNumber of outputs from step method
isLockedLocked status for input attributes and nontunable properties
release Allow property value and input characteristics changes
stepLabel and count connected regions in input

Examples

expand all

Label Connected Regions in an Image

 img = logical([0 0 0 0 0 0 0 0 0 0 0 0 0; ...
 								0 1 1 1 1 0 0 0 0 0 0 1 0; ...
								0 1 1 1 1 1 0 0 0 0 1 1 0; ...
								0 1 1 1 1 1 0 0 0 1 1 1 0; ...
								0 1 1 1 1 0 0 0 1 1 1 1 0; ...
								0 0 0 0 0 0 0 1 1 1 1 1 0; ...
								0 0 0 0 0 0 0 0 0 0 0 0 0])
 hlabel = vision.ConnectedComponentLabeler;
 hlabel.LabelMatrixOutputPort = true;
 hlabel.LabelCountOutputPort = false;
 labeled = step(hlabel, img)
img =

     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     1     1     1     1     0     0     0     0     0     0     1     0
     0     1     1     1     1     1     0     0     0     0     1     1     0
     0     1     1     1     1     1     0     0     0     1     1     1     0
     0     1     1     1     1     0     0     0     1     1     1     1     0
     0     0     0     0     0     0     0     1     1     1     1     1     0
     0     0     0     0     0     0     0     0     0     0     0     0     0


labeled =

    0    0    0    0    0    0    0    0    0    0    0    0    0
    0    1    1    1    1    0    0    0    0    0    0    2    0
    0    1    1    1    1    1    0    0    0    0    2    2    0
    0    1    1    1    1    1    0    0    0    2    2    2    0
    0    1    1    1    1    0    0    0    2    2    2    2    0
    0    0    0    0    0    0    0    2    2    2    2    2    0
    0    0    0    0    0    0    0    0    0    0    0    0    0

Algorithms

This object implements the algorithm, inputs, and outputs described on the Label block reference page. The object properties correspond to the block parameters, except:

  • The LabelCountOutputPort and LabelMatrixOutputPort object properties correspond to the Output block parameter.

Was this topic helpful?