Ask Your Question
0

Measuring Width & Height of Bounding Box

asked 2015-10-04 20:19:43 -0600

morgand536 gravatar image

I am a noob in OpenCV and i would like to use OpenCV to take some basic measurement of the object in photos and videos. I have read and tried a sample code about bounding box for contours, however I would like to modify it to measuring the value of the width and height of the bounding box.

Creating Bounding rotated boxes and ellipses for contours

I have read some posts here about this and the most obvious solution is to retrieve the 4 points of the bounding box, however how can i retrieve those variable in the code?

I have very little background in programming, so it would be appreciated if more detailed steps or codes are available. Thanks a lot.

edit retag flag offensive close merge delete

Comments

It is here

LBerger gravatar imageLBerger ( 2015-10-05 01:31:06 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
3

answered 2015-10-05 05:55:51 -0600

updated 2015-10-05 06:00:13 -0600

This is actually quite straightforward, just use the getters from the Rect that defines the bounding box.

Code sample

// Make sure that the Rect is filled in any possible way
Rect test;
int x = test.x;
int y = test.y;
int width = test.width;
int height = test.height;
// Now with those parameters you can calculate the 4 points
Point top_left(x,y);
Point top_right(x+width,y);
Point bottom_left(x,y+height);
Point bottom_right(x+width,y+height);

For all of this keep in mind that OpenCV uses a coordinate system where y is pointing downwards and where the origin is in the top left corner of the image.

EDIT: this is offcourse for a standard Rect, if you want to apply to a RotatedRect, then use the functions in the link of @LBerger. A code sample can be.

RotatedRect test;
vector<Point> corners;
corners = test.points;
Point top_left = corners[0];
Point top_right = corners[1];
Point bottom_left = corners[2];
Point bottom_right = corners[3];
edit flag offensive delete link more

Comments

1

Thank you very much! It is a very detailed explanation!

morgand536 gravatar imagemorgand536 ( 2015-10-06 03:37:07 -0600 )edit

You are welcome! As long as we can help newcomers like this, I will keep up this job :)

StevenPuttemans gravatar imageStevenPuttemans ( 2015-10-06 06:33:26 -0600 )edit
1

answered 2015-10-05 07:47:06 -0600

theodore gravatar image

Actually if you have a Rect you can also call the member data .width and .height. Pretty much simple and straight forward:

Rect rect;

rect.width;
rect.height;

In case of RotatedRect you just follow what Steven and Laurent pointed you.

edit flag offensive delete link more

Comments

1

pssst ;) my 5th and 6th line :D

StevenPuttemans gravatar imageStevenPuttemans ( 2015-10-05 07:50:54 -0600 )edit
1

yup, you're right my bad :-$. Too much speed :-p

theodore gravatar imagetheodore ( 2015-10-05 08:01:48 -0600 )edit

Thanks for your answer!

morgand536 gravatar imagemorgand536 ( 2015-10-06 03:37:21 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-10-04 20:19:43 -0600

Seen: 8,382 times

Last updated: Oct 05 '15