简体   繁体   中英

Draw a rotated box in openCV in python

I would like to draw a rotated rectangle I've got the top left point and bottom right point, width and height of box. As well as the angle. But I can't seem work out how you draw the rotated rectangle using OpenCV in Python . Please note that I do not want to rotate the image.

Thanks

There are many ways to draw a rectangle in OpenCV.

From the OpenCV documentatation: Drawing Functions

rectangle

Draws a simple, thick, or filled up-right rectangle.

So this function doesn't help as you want to draw it rotated.

A rectangle is nothing but a special 4-sided polygon. So simply use the function for drawing polygons instead.

polylines

Draws several polygonal curves.

Python:

 cv2.polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]]) → img

and insert the 4 vertices of your rotated rectangle

or draw the 4 sides separately using

line

Draws a line segment connecting two points.

or

drawContours

Draws contours outlines or filled contours.

The points can be obtained using simple math or for example using OpenCV's RotatedRect https://docs.opencv.org/2.4/modules/core/doc/basic_structures.html#rotatedrect

   class Point:

        def __init__(self, x, y):
            self.x = int(x)
            self.y = int(y)


    class Rectangle:

        def __init__(self, x, y, w, h, angle):

            # Center Point
            self.x = x
            self.y = y
            # Height and Width
            self.w = w
            self.h = h
            self.angle = angle

        def rotate_rectangle(self, theta):
            pt0, pt1, pt2, pt3 = self.get_vertices_points()

            # Point 0
            rotated_x = math.cos(theta) * (pt0.x - self.x) - math.sin(theta) * (pt0.y - self.y) + self.x
            rotated_y = math.sin(theta) * (pt0.x - self.x) + math.cos(theta) * (pt0.y - self.y) + self.y
            point_0 = Point(rotated_x, rotated_y)

            # Point 1
            rotated_x = math.cos(theta) * (pt1.x - self.x) - math.sin(theta) * (pt1.y - self.y) + self.x
            rotated_y = math.sin(theta) * (pt1.x - self.x) + math.cos(theta) * (pt1.y - self.y) + self.y
            point_1 = Point(rotated_x, rotated_y)

            # Point 2
            rotated_x = math.cos(theta) * (pt2.x - self.x) - math.sin(theta) * (pt2.y - self.y) + self.x
            rotated_y = math.sin(theta) * (pt2.x - self.x) + math.cos(theta) * (pt2.y - self.y) + self.y
            point_2 = Point(rotated_x, rotated_y)

            # Point 3
            rotated_x = math.cos(theta) * (pt3.x - self.x) - math.sin(theta) * (pt3.y - self.y) + self.x
            rotated_y = math.sin(theta) * (pt3.x - self.x) + math.cos(theta) * (pt3.y - self.y) + self.y
            point_3 = Point(rotated_x, rotated_y)

            return point_0, point_1, point_2, point_3

Returns four new points that have been translated by theta

https://github.com/rij12/YOPO/blob/yopo/darkflow/net/yopo/calulating_IOU.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM