Clear Filters
Clear Filters

Union of two bounding boxes

1 view (last 30 days)
FARHAD
FARHAD on 22 May 2014
Commented: Image Analyst on 24 May 2014
Hi, I have two bounding boxes xg and pr and I want to know why we subtract 1 from width_g in the following code to calculate the union coordinates of two bounding boxes.
unionCoords=[min(x_g,x_p),min(y_g,y_p),max(x_g+width_g-1,x_p+width_p-1),max(y_g+height_g-1,y_p+height_p-1)];

Answers (1)

Image Analyst
Image Analyst on 22 May 2014
That's not the union of two bounding boxes. That's the bounding box of the two bounding boxes. There is a difference.
They have a bounding box with values of top, left, width, and height and those are in units of pixels, not center to center. So if the left is at 11 and the width is 4, they consider it going not from 11 to 15 but from 11 to 14, in other words pixels # 11, 12, 13, and 14 (4 pixels). So that's why they subtract 1.
  2 Comments
FARHAD
FARHAD on 22 May 2014
Edited: FARHAD on 23 May 2014
Thanks a lot. Could you explain me the following line of the previous one. Why they have added 1(one) here? unionArea=(unionCoords(3)-unionCoords(1)+1)*(unionCoords(4)-unionCoords(2)+1);
Image Analyst
Image Analyst on 24 May 2014
That's because they want to use whole pixels in calculating the area instead of going from center to center. Look at the square below:
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 1 1 1 1 0
0 0 0 0 0 0 0
What is the width of that square? Is it 3 wide (going from center to center of each pixel), or is it 4 (includes the whole pixel). So is the area 9 or 16? Neither is necessarily right or wrong, and I use both definitions at different times depending on what I want to do. bwarea() will give you a center-to-center area while regionprops() will give you the area in whole pixels.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!