Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- # Rect: ((x,y), (width, height))
- # RotatedRect: ((cx,cy), (width, height), angle)
- def rotated_rect_corners(rotated_rect):
- center, size, angle = rotated_rect
- cx, cy = center
- width, height = size
- angle = np.radians(angle)
- b = np.cos(angle) * 0.5
- a = np.sin(angle) * 0.5
- p0 = (cx - a * height - b * width, cy + b * height - a * width)
- p1 = (cx + a * height - b * width, cy - b * height - a * width)
- p2 = (2 * cx - p0[0], 2 * cy - p0[1])
- p3 = (2 * cx - p1[0], 2 * cy - p1[1])
- return (p0, p1, p2, p3)
- def rotated_rect_bbox_f(rotated_rect):
- corners = np.array(rotated_rect_corners(rotated_rect))
- min_xy = np.amin(corners, axis=0)
- max_xy = np.amax(corners, axis=0)
- size = max_xy - min_xy
- return (min_xy[0], min_xy[1], size[0], size[1])
- points = np.array([[2,2],[1,3],[4,4],[3,5]])
- rotated_rect = cv2.minAreaRect(points)
- print rotated_rect
- print rotated_rect_corners(rotated_rect)
- print rotated_rect_bbox_f(rotated_rect)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement