spandana
spandana

Reputation: 13

extraction of largest connected component

I have binarized an image and found all the connected components in it.Now my aim is to obtain the coordinates of connected component having larger area.i hve used the follwing code but it is showing errors..

           [l,n]=bwconncomp(binext,8);
          for   k1=1:n
               [r,c]=find(l==k1);
               rbar=mean(r(k1));
               cbar=mean(c(k1));
               r1(k1)=max(r(k1));
               c1(k1)=max(c(k1));
               r2(k1)=min(r(k1));
               c2(k1)=min(c(k1));
               len(k1)=r1(k1)-r2(k1);
               wid(k1)=c1(k1)-c2(k1);

          end

Upvotes: 1

Views: 280

Answers (1)

lennon310
lennon310

Reputation: 12699

You need to show us the errors, although I guess it's due to the out-of-range of your array c(k1) and r(k1).

The regions are not necessarily rectangular, so your method to calculate the length and width is only an approximation. Try

L = regionprops(binext,'area','PixelIdxList');

instead. L(i).Area will return you the areas of every region,where i = 1:length(L);

Upvotes: 3

Related Questions