Ask Your Question
1

Minimal area rectangle for multiple contours

asked 2018-03-02 04:12:25 -0600

updated 2018-03-05 09:36:15 -0600

I have a case with several contours

std::vector<std::vector<cv::Point>> contours;

I want to get the smallest area rectangle RotatedRect containing all these contours. I have tried minAreaRect() but it returns an exception (probably because I pass it more than one contour).

What would be the best way of getting the minimal area rectangle when you're dealing with more than one contour?

edit retag flag offensive close merge delete

Comments

You want a rotated rectangle containing all the contours in the vector, our just a bounding box enclosing all the contours?

Pedro Batista gravatar imagePedro Batista ( 2018-03-02 05:02:02 -0600 )edit

A rotated rectangle, but bonus points if the answer includes bounding box as well! :-)

Björn Larsson gravatar imageBjörn Larsson ( 2018-03-02 05:06:41 -0600 )edit

1 answer

Sort by » oldest newest most voted
3

answered 2018-03-02 05:28:11 -0600

berak gravatar image

both minAreaRect() and boundingRect() expect a single contour, not a std::vector<std::vector<cv::Point>>

you'll have to build a single vector<Point> , and then apply your favourite function:

vector<Point> joined;
for (size_t i=0; i<contours.size(); i++) {
      joined.insert(joined.end(), contours[i].begin(), contours[i].end());
}

boundingRect(joined);
minAreaRect(joined);
edit flag offensive delete link more

Comments

Thanks, that solved my problem! :-)

Björn Larsson gravatar imageBjörn Larsson ( 2018-03-02 09:55:48 -0600 )edit

@berak, a little explanation for the code and how it solves the problem, please..

questerer gravatar imagequesterer ( 2018-03-19 00:54:23 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2018-03-02 04:12:25 -0600

Seen: 2,845 times

Last updated: Mar 02 '18