# IMAGE PROCESSING

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

## Recent Posts

### Extraction of Connected components without using BWLABEL function

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.
where k=1,2,3…

Using the above expression, connected components are extracted without using the function BWLABEL
LABELLING:

MATLAB CODE:

``````
```
```

``````

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:
1. Read an image (A) and convert it into binary image.
2. Define a structuring element (B).
3. Initialize the Label matrix with zeros.
4. Find a non-zero element position in the input matrix A.
5. Initialize a matrix X with zeros and place 1 in the non-zero element position found in the previous step.
6. Perform dilation using the structuring element B on matrix X. i.e. imdilate(X,B);
7. Perform intersection with the matrix A. Y= A&imdilate(X, B).
8. Check whether Y==X. If no, then X=Y and perform steps 6 and 7 again else stop the iteration.
9. 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.
10. Similarly, place zero in those positions in the input matrix A.
11. Again find a non-zero element position in the matrix A. If found, goto step 5 else stop the iteration.
12. 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

Like "IMAGE PROCESSING" page

Pratik Datta said...

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

Aaron Angel said...

@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.
'label2rgb' is a matlab function that performs similar task.

cuthbert tendaupenyu said...

Thank you for this code, you are a life saver

Unknown said...

how can i extract without knowing label number?

rs said...

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