Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw rotated rectangle with Python [duplicate]

Tags:

python

opencv

Are there any helper methods to draw a rotated rectangle that is returned by cv2.minAreaRect() presumably as ((x1,y1),(x2,y2),angle)? cv2.rectangle() does not support an angle. And since the tuple returned is not of the "RotatedRect" class (because it seems to not be implemented in the Python bindings) there is no points() method, as shown in the C++ tutorial "Creating Bounding rotated boxes and ellipses for contours¶".

How could a rotated rectangle be drawn from lines - rotate about the center point or the first point given?

like image 408
handle Avatar asked Nov 19 '22 18:11

handle


1 Answers

rect = cv2.minAreaRect(cnt)
box = cv2.cv.BoxPoints(rect) # cv2.boxPoints(rect) for OpenCV 3.x
box = np.int0(box)
cv2.drawContours(im,[box],0,(0,0,255),2)

should do the trick.

sources:

1) http://opencvpython.blogspot.in/2012/06/contours-2-brotherhood.html

2) Python OpenCV Box2D

like image 132
Tobias Hermann Avatar answered Nov 21 '22 08:11

Tobias Hermann