Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of Matlab's bwlabel,regionprops & centroid functions

I have spent all day reading up on the above MATLAB functions. I can't seem to find any good explanations online, even on the MathWorks website!

I would be very grateful if anyone could explain bwlabel, regionprops and centroid. How do they work if applied to a grayscale image?

Specifically, they are being used in this code below. How do the above functions apply to the code below?

fun=@minutie; L = nlfilter(K,[3 3],fun); 
%% Termination LTerm=(L==1); 
figure; imshow(LTerm) 
LTermLab=bwlabel(LTerm); 
propTerm=regionprops(LTermLab,'Centroid'); 
CentroidTerm=round(cat(1,LTerm(:).Centroid)); 
figure; imshow(~K) 
set(gcf,'position',[1 1 600 600]); hold on 
plot(CentroidTerm(:,1),CentroidTerm(:,2),'ro') 
like image 319
user1877082 Avatar asked Sep 22 '14 23:09

user1877082


People also ask

How does Regionprops work in Matlab?

The regionprops function returns the centroids in a structure array. s = regionprops(BW,'centroid'); Store the x- and y-coordinates of the centroids into a two-column matrix. centroids = cat(1,s.

What does Bwlabel do in Matlab?

Description. L = bwlabel( BW ) returns the label matrix L that contains labels for the 8-connected objects found in BW .

How do you find the area of an image in Matlab?

total = bwarea( BW ) estimates the area of the objects in binary image BW . total is a scalar whose value corresponds roughly to the total number of on pixels in the image, but might not be exactly the same because different patterns of pixels are weighted differently.

How do you Binarize an image in Matlab?

BW = imbinarize( I , method ) creates a binary image from image I using the thresholding method specified by method : 'global' or 'adaptive' . BW = imbinarize( I , T ) creates a binary image from image I using the threshold value T .


1 Answers

That's quite a mouthful to explain!... nevertheless, I'd love to explain it to you. However, I'm a bit surprised that you couldn't understand the documentation from MathWorks. It's actually quite good at explaining a lot (if not all...) of their functions.

BTW, bwlabel and regionprops are not defined for grayscale images. You can only apply these to binary images.

Update: bwlabel still has the restriction of accepting a binary image but regionprops no longer has this restriction. It can also take in a label matrix that is usually output from bwlabel as well as binary images.

Assuming binary images is what you want, my explanations for each function is as follows.


bwlabel

bwlabel takes in a binary image. This binary image should contain a bunch of objects that are separated from each other. Pixels that belong to an object are denoted with 1 / true while those pixels that are the background are 0 / false. For example, suppose we have a binary image that looks like this:

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

You can see in this image that there are four objects in this image. The definition of an object are those pixels that are 1 that are connected in a chain by looking at local neighbourhoods. We usually look at 8-pixel neighbourhoods where you look at the North, Northeast, East, Southeast, South, Southwest, West, Northwest directions. Another way of saying this is that the objects are 8-connected. For simplicity, sometimes people look at 4-pixel neighbourhoods, where you just look at the North, East, South and West directions. This woudl mean that the objects are 4-connected.

The output of bwlabel will give you an integer map where each object is assigned a unique ID. As such, the output of bwlabel would look something like this:

0  0  0  0  0  3  3  3  0  0
0  1  0  1  0  0  3  3  0  0
0  1  1  1  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  4
0  0  0  0  0  0  0  0  4  4
0  0  2  2  2  2  0  0  4  4

Because MATLAB processes things in column major, that's why the labelling is how you see above. As such, bwlabel gives you the membership of each pixel. This tells you where each pixel belongs to if it falls on an object. 0 in this map corresponds to the background. To call bwlabel, you can do:

L = bwlabel(img);

img would be the binary image that you supply to the function and L is the integer map I just talked about. Additionally, you can provide 2 outputs to bwlabel, and the second parameter tells you how many objects exist in the image. As such:

[L, num] = bwlabel(img);

With our above example, num would be 4. As another method of invocation, you can specify the connected pixel neighbourhoods you would examine, and so you can do this:

[L, num] = bwlabel(img, N);

N would be the pixel neighbourhood you want to examine (i.e. 4 or 8).


regionprops

regionprops is a very useful function that I use daily. regionprops measures a variety of image quantities and features in a black and white image. Specifically, given a black and white image it automatically determines the properties of each contiguous white region that is 8-connected. One of these particular properties is the centroid. This is also the centre of mass. You can think of this as the "middle" of the object. This would be the (x,y) locations of where the middle of each object is located. As such, the Centroid for regionprops works such that for each object that is seen in your image, this would calculate the centre of mass for the object and the output of regionprops would return a structure where each element of this structure would tell you what the centroid is for each of the objects in your black and white image. Centroid is just one of the properties. There are other useful features as well, but I'm assuming you don't want to do this. To call regionprops, you would do this:

s = regionprops(img, 'Centroid');

The above code will calculate the centroids of each of your objects in the image. You can specify additional flags to regionprops to specify each feature that you want. I do highly encourage that you take a look at all of the possible features that regionprops can calculate, as there are many that are useful in a variety of different applications and situations.

Also, by omitting any flags as input into the function, you would calculate all of the features in your image by default. Therefore, if we were to declare the image that we have seen above in MATLAB, this is what would happen after I run regionprops. After, let's calculate what the centroids are:

img = logical(...
   [0  0  0  0  0  1  1  1  0  0;
    0  1  0  1  0  0  1  1  0  0;
    0  1  1  1  0  0  0  0  0  0;
    0  0  0  0  0  0  0  0  0  1;
    0  0  0  0  0  0  0  0  1  1;
    0  0  1  1  1  1  0  0  1  1]);
s = regionprops(img, 'Centroid');

... and finally when we display the centroids:

>> disp(cat(1,s.Centroid))

3.0000    2.6000
4.5000    6.0000
7.2000    1.4000
9.6000    5.2000

As such, the first centroid is located at (x,y) = (3, 2.6), the next centroid is located at (x,y) = (4.5, 6) and so on. Take special note that the x co-ordinate is the column while the y co-ordinate is the row.


Hope this is clear!

like image 188
rayryeng Avatar answered Sep 30 '22 13:09

rayryeng