Advertisement
dan-masek

Untitled

May 20th, 2019
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. # Rect: ((x,y), (width, height))
  5. # RotatedRect: ((cx,cy), (width, height), angle)
  6.  
  7. def rotated_rect_corners(rotated_rect):
  8.     center, size, angle = rotated_rect
  9.     cx, cy = center
  10.     width, height = size
  11.    
  12.     angle = np.radians(angle)
  13.     b = np.cos(angle) * 0.5
  14.     a = np.sin(angle) * 0.5
  15.    
  16.     p0 = (cx - a * height - b * width, cy + b * height - a * width)
  17.     p1 = (cx + a * height - b * width, cy - b * height - a * width)
  18.     p2 = (2 * cx - p0[0], 2 * cy - p0[1])
  19.     p3 = (2 * cx - p1[0], 2 * cy - p1[1])
  20.    
  21.     return (p0, p1, p2, p3)
  22.    
  23. def rotated_rect_bbox_f(rotated_rect):
  24.     corners = np.array(rotated_rect_corners(rotated_rect))
  25.     min_xy = np.amin(corners, axis=0)
  26.     max_xy = np.amax(corners, axis=0)
  27.     size = max_xy - min_xy
  28.     return (min_xy[0], min_xy[1], size[0], size[1])
  29.  
  30. points = np.array([[2,2],[1,3],[4,4],[3,5]])
  31. rotated_rect = cv2.minAreaRect(points)
  32.  
  33. print rotated_rect
  34. print rotated_rect_corners(rotated_rect)
  35. print rotated_rect_bbox_f(rotated_rect)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement