10

I am trying to call OpenCV function MinAreaRect2 from within python. I use OpenCV 2.4.2 with python 2.7 and numpy 1.6. I went this far :

import cv
def nda2ipl(arr, dtype=None):
 return cv.fromarray(np.ascontiguousarray(arr, dtype=dtype))
def min_area_rect2(points):
 storage = cv.CreateMemStorage()
 cv_points = nda2ipl(points.reshape((-1, 1, 2)))
 out = cv.MinAreaRect2(cv_points, storage)
return out

I can call this function with a ndarray of shape (N x 2). I get this kind of results :

((476.5, 604.5), (951.0, 1207.0), -0.0)

I assume that the first tuple is the center of the box, the second gives the width and the height and the last is the angle.

The problem is I could not get a clear reference stating this. Actually, the opencv documentation tells me what the functions returns in Python.

I found the official documentation about this function but this is not very helpful.

What are exactly the output of MinAreaRect2 in python ? More generally, where do you get precise documentation about the OpenCV python wrapper ?

asked Aug 2, 2012 at 14:03

2 Answers 2

12

OpenCV Python wrapper documentation is kept along with the normal documentation in same site, www.docs.opencv.org

Earlier Python module used was cv interface, which used native data types from original C++ interface like cvMat, cvSeq etc.

Later it was shifted to more better, advanced and simpler module cv2 interface. In it, everything is returned as either Numpy arrays or native python data types.

Here, tuple returned has the same arguments as that of cvBox2D . You can find more details the differences between different python wrappers here : What is different between all these OpenCV Python interfaces?

Here, your assumption is correct. Those values specified exactly what you mentioned.

If you want to draw rotated rect, you need 4 vertices of the rectangle. For that, you need a function which is never seen in documentation, ie cv2.cv.BoxPoints() (but don't worry, it will be there in documentation when OpenCV 2.4.3 is released. )

enter image description here

ajinzrathod
97012 silver badges35 bronze badges
answered Aug 3, 2012 at 16:26
Sign up to request clarification or add additional context in comments.

1 Comment

Somehow disappointing, it seems they have postponed your request for adding BoxPoints to documentation, until OpenCV 3.0
1

As stated in the documentation to which you linked, MinAreaRect2 returns a Box2D object:

A CvBox2D object is defined by its center, size and angle, as you correctly assumed, as described here.

Generally speaking, the documentation of the Python wrapper is rather poor. Your best bet is to refer to the C++ documentation and to read the source code.

answered Aug 3, 2012 at 12:24

2 Comments

Thanks for your answer. But the python function does not return a Box2D object but a python tuple. So I wondered about the relation between the tuple elements and the C++ Box2D struct. I tried looking at the source code but could not find a place which could give me the answer. I have difficulties understanding how the opencv library is wrapped into python, actually. Any hint about this would be very welcomed.
I found this post quite clarifying: opencvpython.blogspot.com.es/2012/06/…

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.