Problems with Matlab Projects? You may face many Problems, but do not worry we are ready to solve your Problems. All you need to do is just leave your Comments. We will assure you that you will find a solution to your project along with future tips. On Request we will Mail you Matlab Codes for Registered Members of this site only, at free service...Follow Me.

Extraction of Connected components without using BWLABEL function


 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 functionBWLABEL
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:
  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

0 comments:

Post a Comment

Recent Comments

Popular Matlab Topics

Share your knowledge - help others

Crazy over Matlab Projects ? - Join Now - Follow Me

Sites U Missed to Visit ?

Related Posts Plugin for WordPress, Blogger...

Latest Articles

Special Search For Matlab Projects

MATLAB PROJECTS

counter

Bharadwaj. Powered by Blogger.