Ask Your Question

Revision history [back]

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.

This is actually quite straightforward, just use the getters from the rectRect 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.

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, Rect, if you want to apply to a RotatedRect, RotatedRect, then use the functions in the link of @LBerger.@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];