What are the properties in a BoundingBox from regionprops?

I'm trying to figure out what coordinates are being stored from a BoundingBox. For instance
L=logical(bw);
box=regionprops(L,'Area', 'BoundingBox');
box(2)
And the output
ans =
Area: 127
BoundingBox: [10.5000 11.5000 10 19]
What are those 4 values? Is it the [top left, top right, bottom left, bottom right] coordinates?

2 comentarios

Please Sir, why the values of the two components x and y is real values? Also, the last two values width and heigh are integer values?
There is the question of whether coordinates represent pixel centers or pixel edges.

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 27 de Mzo. de 2016
It's [left, top, width, height]. Just be aware that the left and top are 0.5 pixels to the left and above, respectively, than the actual first column and first row of the binary image. It's because the Bounding box is defined such that it "contains" the blob, and if it ran right through the center of the top-most and left-most pixel, then that definition becomes somewhat ambiguous.

17 comentarios

Recap
Recap el 27 de Mzo. de 2016
Thank you for the answer. But i dont understand what you mean by left and top?
Recap
Recap el 27 de Mzo. de 2016
Is that the row and column from where it starts?
Left is column. Top is row. The order is column followed by row, which is x followed by y, because x corresponds to columns and y corresponds to row.
Recap
Recap el 27 de Mzo. de 2016
@Walter Roberson thanks for clarifying that. I greatly appreciate it.
Hi I want to save these 4 values of Bounding box for a set of images how can i do that .
Try
props = regionprops(L, 'Area', 'BoundingBox');
t = [props.BoundingBox];
allX = t(1:4:end);
allY = t(2:4:end);
allWidths = t(3:4:end);
allHeights = t(4:4:end);
Thankyou !!!!
Ali
Ali el 10 de Mayo de 2018
how can i get greater boundingbox rectangle out of all detected boundingbox rectangles?
See the attached function.
That was very good explanation!
Hello,
I want to have the values of bounding box like this [xmin, ymin, xmax, ymax].
From the command i get like this [x y width heigth]. And one thing more i want to save the result as attached in the image. I want Origin to be shift from top left to bottom left that we usually tthink of on an axis.20181214_195417.jpg
You get BoundingBox = xLeft, yTop, width, height] so to get the coordinates you want, do
xMin = ceil(BoundingBox(1))
xMax = xMin + BoundingBox(3) - 1
yMin = ceil(BoundingBox(2))
yMax = yMin + BoundingBox(4) - 1
To flip the image upside down, use
flippedImage = flipud(originalImage);
Note though that the first line will always be the first line, it will just contain what used to be the last/bottom line of the image.
@Image Analyst Could `floor` be used instead? That way the subtraction wouldn't be needed.
Suppose BoundingBox(1) is 1 and BoundingBox(3) is 5 -- width 5 starting at 1.
IA's code would give xMin = ceil(1) --> 1, xMax = 1 + 5 - 1 --> 5. Using this as subscripts, YourImage(:,1:5) would indeed select 5 elements starting from position 1.
Your proposal is xMin = floor(1) --> 1, xMax = 1 + 5 --> 6. Using this as subscripts, YourImage(:,1:6) would be 6 elements, not 5.
Okay, what about if BoundingBox(1) is 0.9 and BoundingBox(3) is 5. Then IA's could would give xMin = ceil(0.9) --> 1, xMax = 1 + 5 - 1 --> 5, subscripts would be YourImage(:,1:5) which would be valid. Your proposal would be xMin = floor(0.9) --> 0, xMax = 0 + 5 --> 5, subscripts YourImage(:,0:5) which would be invalid because of the 0 subscript. If BoundingBox(1) had been (say) 2.9 instead then floor(2.9) --> 2, xMax = 2 + 5 --> 7, subscripts YourImage(:, 2:7) which would be 6 columns -- columns 2, 3, 4, 5, 6, 7
We see from here that no matter whether you use floor() or ceil() you still need to subtract 1 from xMax.
@cpaniaguam the bounding box is [xLeft, yTop, width, height], and xLeft and yTop are half a pixel outside of the left most and uppermost white pixel. xLeft and yTop always end in .5 since it's outisde the blob, not on the blob, and this is just the convention they use. So if you want the actual left column and top row of the blob, you must call ceil to round them up.
And the width and height are the width and height of the bounding box, which is larger than the actual blob by half a pixel all the way around. But the width and height are of the actual blob, not the bounding box of the blob. So if you want xRight and yBottom, you must add the width-1 and height -1 to the xLeft and yTop. See this example:
img = false(5,7);
img(2:4, 3:6) = true
img = 5×7 logical array
0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 1 1 1 1 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0
props = regionprops(img, 'BoundingBox');
boundingBox = props.BoundingBox
boundingBox = 1×4
2.5000 1.5000 4.0000 3.0000
xMin = ceil(boundingBox(1))
xMin = 3
xMax = xMin + boundingBox(3) - 1
xMax = 6
yMin = ceil(boundingBox(2))
yMin = 2
yMax = yMin + boundingBox(4) - 1
yMax = 4
I hope that explains it better.
It certainly does, thanks @Image Analyst!

Iniciar sesión para comentar.

Más respuestas (2)

elvis okacha
elvis okacha el 12 de Abr. de 2019
Editada: Walter Roberson el 12 de Abr. de 2019
I am trying to find length and width.is this ok?:
I=Imread(filename)
Info=imfinfo(filename)
Thres=graythresh(I)
I2=~(im2bw(I,thresh))
cmp=bwconncomp(I2)
s=regionprops(cmp,'BoundingBox')
x=s.BoundingBox(3)
y=s.BoundingBox(4)
res=info.ResolutionUnit
resX=info.XResolutionUnit
resY=info.YResolutionUnit

3 comentarios

Error 'field reference for multiple structure elements that is followed by more reference blocks is an error
Your image has multiple regions, so s is returning a nonscalar structure.
bb = vertcat(s.BoundingBox);
x = bb(:,1);
y = bb(:,2);
width = bb(:,3);
height = bb(:,4);
These will each be vectors, reflecting the fact that you have multiple regions.
Thanks

Iniciar sesión para comentar.

SOURABH BHATTACHARYA
SOURABH BHATTACHARYA el 22 de En. de 2022
Editada: SOURABH BHATTACHARYA el 22 de En. de 2022
The first two are the top left (x,y) coordinates while the later ones are for max width and height respectively along the respectives axes.

Categorías

Más información sobre Images en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 27 de Mzo. de 2016

Comentada:

el 9 de Nov. de 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by