Open In App

How To Identifying Objects Based On Label in MATLAB?

Last Updated : 11 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

labeling means identifying and placing labels on each program of an object project in the image. in Binary image, it’s classified as 4-connected and 8-connected. for performing the labeling in MATLAB we use the Built-in function bwlabel() used to label the object in the binary image.

So, In this article, we will understand how to Identify Objects based on labels in MATLAB.

Syntax for Labeling

L = bwlabel(BW)      % return the label matrix that contains labels 
                                        for the 8-connected object found.
                
L = bwlabel(BW, con)     % return the label matrix where specify 
                                            the con connectivity.

[L,n] = bwlabel(__)          %label function also return number of object
                                           found in BW.

Steps for Identifying Objects Based On Label

For identifying the object based on labeling following steps:

Step 1: First open your MATLAB and make a new file in MATLAB editor, and make sure that which image used to perform that image should be inside the MATLAB. then convert the RGB image to a binary image. follow the line of code.

Example 1:

Matlab




% A program to perform identify object on basis of label
 % read the image via imread function.
I = imread('gfg.png'); 
figure
 
% show the image via this function.
imshow(I);  


Output:

 

Step 2: Now we implement our code for the detection of the particular object, using the Labeling algorithm. as you can see the below code.

Matlab




% A program to perform identify
% object on basis of label
 % Read the image.
I=imread('gfg.png');
figure,
 
% show the image in figure.
imshow(I);  
title('Original image');
 
% conversion grayscale image to binary image
C=~im2bw(I); 
 
 % fills holes in the input binary image.
B=imfill(C,'holes');
label=bwlabel(B,8);
for j=1:max(max(label))
 
 % traverse the binary image by row and column.
[row, col] = find(label==j);
len=max(row)-min(row)+2;
breadth=max(col)-min(col)+2;
target=uint8(zeros([len breadth 3] ));
sy=min(col)-1;
sx=min(row)-1;


In the above code, we use some specific function which is used to identify the particular object image, fill the holes in the image then label the image based on 8-connected.

  • bwlabel() it’s used to identify the object and place labels on the binary image.
  • imfill() this function is used to fill holes in the input image BW.
  • imshow() it’s used to display the binary image BW in a figure.
  • ~im2bw() this function is used to convert the grayscale image to binary BW.

After running the above-implemented code you will get a new figure.

Output:

 

Step 3:  So this is the final step where we display the image in RGB format and identify the object of basis labeling, for that we just implement the code and store the coordinate (x, y) as object 1 labeled as 1.

After then compute the image size for object 1

Example 3:

Matlab




% A program to perform identify object on basis of label
% Read the image.
I=imread('gfg.png'); 
figure,
 
% show the image in figure.
imshow(I);  
title('Original image');
 
% conversion grayscale image to binary image
C=~im2bw(I); 
 
 % fills holes in the input binary image.
B=imfill(C,'holes');
label=bwlabel(B,8);
for j=1:max(max(label))
 
% traverse the binary image by row and column.
[row, col] = find(label==j);
len=max(row)-min(row)+2;
breadth=max(col)-min(col)+2;
target=uint8(zeros([len breadth 3] ));
sy=min(col)-1;
sx=min(row)-1;
 
for i=1:size(row,1)
    x=row(i,1)-sx;
    y=col(i,1)-sy;
    target(x,y,:)=I(row(i,1),col(i,1),:);
end
 
% contains an object.
mytitle=strcat('Object Number:',num2str(j));
figure
imshow(target);
title(mytitle);
end


So after computing the image size, the length can be founded by subtracting the maximum y values. Then map the pixel value to a new image. 

Output:

 

If you want to execute this program then you can choose any random image. and keep remembering the thing is whatever image you selected that should be inside the MATLAB folder. 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads