Many of the visitors to this blog mailed me to post a

**MATLAB**code for extracting the connected components. In**MATLAB,**a function called**BWLABEL**is available to label the connected components.
Based on the following iterative
expression, the connected components are extracted.

**BWLABEL**

**LABELLING:**

**MATLAB CODE**:

```
I=imread('label3.jpg');
```

```
I=im2bw(I);
%Structuring element
B=strel('square',3);
A=I;
%Find a non-zero element's position.
p=find(A==1);
p=p(1);
Label=zeros([size(A,1) size(A,2)]);
N=0;
while(~isempty(p))
N=N+1;%Label for each component
p=p(1);
X=false([size(A,1) size(A,2)]);
X(p)=1;
Y=A&imdilate(X,B);
while(~isequal(X,Y))
X=Y;
Y=A&imdilate(X,B);
end
Pos=find(Y==1);
A(Pos)=0;
%Label the components
Label(Pos)=N;
p=find(A==1);
end
imtool(Label);
```

EXPLANATION:

- Read an image (A) and convert it into binary image.
- Define a structuring element (B).
- Initialize the Label matrix with zeros.
- Find a non-zero element position in the input matrix A.
- Initialize
a matrix X with zeros and place 1 in the non-zero element position found
in the previous step.
- Perform
dilation using the structuring element B on matrix X. i.e. imdilate(X,B);
- Perform
intersection with the matrix A. Y= A&imdilate(X, B).
- Check
whether Y==X. If no, then X=Y and perform steps 6 and 7 again else stop
the iteration.
- Find
the non-zero elements position in the Y. In matrix Label place a number N
in those positions. N is for labeling the connected components.
- Similarly,
place zero in those positions in the input matrix A.
- Again
find a non-zero element position in the matrix A. If found, goto step 5
else stop the iteration.
- Using
the labels the connected components can be extracted.

**EXTRACTION:**

**MATLAB CODE:**

```
%Extracting the components
Im=zeros([size(A,1) size(A,2)]);
ele=find(Label==1);
Im(ele)=1;
figure,imshow(Im);title('Label:1');
```

To obtain the
first component, find the positions with value=1 in the Label Matrix.
Similarly, other components can be extracted.

```
%Extracting the characters 'I M A G E'
ele=find(Label==2|Label==3|Label==6|Label==7|Label==9);
Im1=zeros([size(A,1) size(A,2)]);
Im1(ele)=1;
figure,imshow(Im1);title('Specific components');
```

From the Label
matrix, I extracted the characters I, M, A, G and E alone by specifying the
label numbers 2,3,6,7 and 9.

%Total
number of Letters

```
Total=sprintf('Total Number of Letters:%d',N);
display(Total);
```

The total number of components extracted in
the above example Image:

Total =

Total Number of
letters:14

```
%Differentiate each component with a specific color
RGBIm=zeros([size(Label,1) size(Label,2) 3]);
R=zeros([size(Label,1) size(Label,2)]);
G=zeros([size(Label,1) size(Label,2)]);
B=zeros([size(Label,1) size(Label,2)]);
U=64;
V=255;
W=128;
for i=1:N
Pos=find(Label==i);
R(Pos)=mod(i,2)*V;
G(Pos)=mod(i,5)*U;
B(Pos)=mod(i,3)*W;
end
RGBIm(:,:,1)=R;
RGBIm(:,:,2)=G;
RGBIm(:,:,3)=B;
RGBIm=uint8(RGBIm);
figure,imshow(RGBIm);title('Labelled Components');
```

For each label,
unique color is given. Here is another
example for labeling and extracting the components. Read the image, ‘coins.png’
, convert it into binary and fill the holes. Then perform labeling and
extraction.

Total =

Total Number of
coins: 10

## 5 comments:

please explain th coloring section of the code.

If i need to color all the labelled objects as only "black" or "white" , depending on the background how can i do it..?

@Pratik Datta

Coloring is done based on the index of the objects. Each component(pixels with same index are assumed as one component) is assigned one color.

For more information:

'label2rgb' is a matlab function that performs similar task.

Thank you for this code, you are a life saver

how can i extract without knowing label number?

Is it possible to implement this without matlab, in C++?

## Enjoyed Reading? Share Your Views