2

I have recently started working with openCV and and python. I have a project where I am finding contours using findContours. I get roughly around 6-8 contours on which I am looping to get the bounding box that fits the contour.

For that I have used minAreaRect(contours) which gives me rotated rectangle that should fit the contour. Now the output of this command is a list of tuples.

Each tuple looks like this ((81.0, 288.0), (22.0, 10.0), -0.0) I couldnt get any description on what each of that number mean?

I think it might be ((x-coordinate, y-coordinate),(width, height), rotation).

zero-one
  • 196
  • 7
RC0993
  • 898
  • 1
  • 13
  • 44
  • 2
    Please show full code. Did you get only the outer contour first? Did you do any searches on Google or StackOverflow? It outputs the same as Box2D. See https://docs.opencv.org/2.4/modules/core/doc/old_basic_structures.html?highlight=box2d#cvbox2d and https://stackoverflow.com/questions/18207181/opencv-python-draw-minarearect-rotatedrect-not-implemented. Please read this forum's help section for how to ask a good question. – fmw42 Sep 17 '19 at 05:31

2 Answers2

7

You are correct. Having a look at OpenCV's (C++) documentation on cv::minAreaRect, we see that a cv::RotatedRect is returned. The full construcor of cv::RotatedRect is:

cv::RotatedRect::RotatedRect(const cv::Point2f& center, const cv::Size2f& size, float angle)    

The description of the corresponding parameters is:

center    The rectangle mass center.
size      Width and height of the rectangle.
angle     The rotation angle in a clockwise direction. When the angle is 0, 90, 180, 270 etc., the rectangle becomes an up-right rectangle.

Obviously, center and size are treated as tuples in the Python API, and all three parameters are returned as a tuple also. So, all in all this fits quite well your assumption.

Hope that helps!

HansHirse
  • 18,010
  • 10
  • 38
  • 67
  • How does the decimal number define rotation? what to do if I want to draw that rectangle. also how to calc its area? – RC0993 Sep 17 '19 at 05:18
  • @RC0993 _The rotation angle in a clockwise direction._ I assume, `-0.0` is just plain `0`. For drawing rotated rectangles, please have a look at [`cv::boxPoints`](https://docs.opencv.org/4.1.1/d3/dc0/group__imgproc__shape.html#gaf78d467e024b4d7936cf9397185d2f5c). You have width and height of a rectangle, so the area is just `width * height`!? – HansHirse Sep 17 '19 at 05:26
  • For area I tried`.width` times `.height` it didnt work like that :P – RC0993 Sep 17 '19 at 05:30
  • These are all follow-up questions outside the scope of your initial question, and not answerable without seeing any of your code. Please see the comment of @fmw42 on your question. Feel free to ask a new question, but be sure to follow some guidelines, i.e. see [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – HansHirse Sep 17 '19 at 05:33
0

Based on my observations that function gives u back following: (center(x, y), (width, height), angle of rotation) = cv2.minAreaRect(points)

Serghei
  • 21
  • 3
  • 1
    please take the [tour] and review [answer]. your answer adds no value because it's a subset of an existing answer. – Christoph Rackwitz Sep 13 '22 at 11:20
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 17 '22 at 17:28