Pergunta

I want to find out the width of a connected component using regionprops. Width is not one of the default properties of regionprops but I think BooundingBox has this option. From the documentation I read:

'BoundingBox' — The smallest rectangle containing the region, a 1-by-Q *2 vector, where Q is the number of image dimensions: ndims(L), ndims(BW), or numel(CC.ImageSize). BoundingBox is [ul_corner width], where:

ul_corner is in the form [x y z ...] and specifies the upper-left corner of the bounding box width is in the form [x_width y_width ...] and specifies the width of the bounding box along each dimension

So if I type in s(1).BoundingBox in the console I get this

ans =

    7.5000   27.5000  197.0000  132.0000

Am I right to assume that the third element (197 in this case) is the width of the connected component? The first two are the xy coords and the last one the height?

If so how do I extract it?

I tried this code and it does not seem to work.

BB = [stats.BoundingBox];
for m = 3:3:length(BB) % start from third element, skip next three and so on
  width = BB(m);
end

It just appears to sum all the elements of BB and store the result in width.

Am I right in extracting the width from BoundingBox? And what is wrong with my indexing method? why is not giving me a width vector?

Thank you

Foi útil?

Solução

BoundingBox is [ul_corner_x ul_corner_y x_width y_width] where the 1st 2 elements specify the upper-left corner of the bounding box, and the last 2 elements specify the width of the bounding box along each dimension. Note that it is not the width of the connected component, but the width of bounding box. And each bounding box just includes all the connected components with the same specified label. Actually it doesn't make sense to define the width for a connected component (polygonal).

A = randi([0 1], 10, 10);
bw=bwlabel(A);
cc=regionprops(bw,'BoundingBox');

and you have,

>> cc(1)

ans = 

    BoundingBox: [0.5000 0.5000 10 10]

>> cc(2)

ans = 

    BoundingBox: [3.5000 0.5000 1 1]

>> cc(3)

ans = 

    BoundingBox: [8.5000 8.5000 1 2]

with

bw =

     0     0     0     2     0     0     1     0     0     1
     0     1     0     0     0     0     0     1     1     0
     0     0     1     0     0     1     0     0     1     0
     0     1     0     1     1     1     1     1     0     0
     0     0     1     1     1     0     1     1     1     0
     0     1     1     0     0     1     0     1     0     0
     1     1     1     1     1     1     0     1     0     0
     0     0     0     1     0     1     0     0     0     0
     1     1     1     1     1     0     0     0     3     0
     1     0     1     1     1     1     0     0     3     0
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top