matlabimage-processingmathematical-morphology

Counting the largest connecting objects/component by using bwlabel


Suppose I have a binary image BW and I'm using bwlabel to find the connected area of 1. I want to find the largest group of this result (which is not 0) and also the location where this group is located in the picture. How to do this?


Solution

  • there are several ways to accomplish that, for example histogram the content of the nonzero elements:

    L=bwlabel(I);
    [a val]=hist(L(:),1:max(L(:)))
    val(a==max(a))
    

    will yield the value that is most occuring

    another way is to use tabulate :

    a=tabulate(L(:))
    a(a(:,2)==max(a(2:end,2)),1)
    

    tabulate creates a frequency table of data in vector L(:). The information is arranged as follows:

    1st column — The unique values of L

    2nd column — The number of instances of each value

    3rd column — The percentage of each value

    etc...