-
-
Save daejungkim/529e4f1d79fac96c12e534ac9bfd74fe to your computer and use it in GitHub Desktop.
Rotate bounding box and get new bounding coords
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # answer to this reddit post: | |
| # https://www.reddit.com/r/learnmachinelearning/comments/o6br1e/calculate_bounding_box_coordinates_from_contour/ | |
| import numpy as np | |
| from numpy import sin, cos, sqrt, pi | |
| import math | |
| import matplotlib.pyplot as plt | |
| center = (332, 209) | |
| width = 56 | |
| height = 33 | |
| degrees = -77 | |
| # topleft | |
| pt1 = [center[0] - width, center[1] - height] | |
| # btmleft | |
| pt2 = [center[0] - width, center[1] + height] | |
| # btmright | |
| pt3 = [center[0] + width, center[1] + height] | |
| # topright | |
| pt4 = [center[0] + width, center[1] - height] | |
| def rotate(origin, point, angle): | |
| """ | |
| Rotate a point counterclockwise by a given angle around a given origin. | |
| The angle should be given in radians. | |
| modified from answer here: https://stackoverflow.com/questions/34372480/rotate-point-about-another-point-in-degrees-python | |
| """ | |
| ox, oy = origin | |
| px, py = point | |
| qx = ox + math.cos(angle) * (px - ox) - math.sin(angle) * (py - oy) | |
| qy = oy + math.sin(angle) * (px - ox) + math.cos(angle) * (py - oy) | |
| return int(qx), int(qy) | |
| rectangle = [pt1, pt2, pt3, pt4, pt1] | |
| rectangle_rotated = [rotate(center, pt, math.radians(degrees)) for pt in rectangle] | |
| rectangle = np.array(rectangle) | |
| rectangle_rotated = np.array(rectangle_rotated) | |
| # these are what you need | |
| x_min, y_min = np.min(rectangle_rotated,axis=0) | |
| x_max, y_max = np.max(rectangle_rotated,axis=0) | |
| # create bounding rect points | |
| pt1 = [x_min, y_min] | |
| pt2 = [x_min, y_max] | |
| pt3 = [x_max, y_max] | |
| pt4 = [x_max, y_min ] | |
| rectangle_bounding = [pt1, pt2, pt3, pt4, pt1] | |
| # plot input rectangle in blue, rotated in red, bounding in green | |
| plt.plot(np.array(rectangle)[:,0], np.array(rectangle)[:,1], "bo-") | |
| plt.plot(np.array(rectangle_rotated)[:,0], np.array(rectangle_rotated)[:,1], "ro-") | |
| plt.plot(np.array(rectangle_bounding)[:,0], np.array(rectangle_bounding)[:,1], "go-") | |
| plt.show() | |
| print(f"Rotated bounding rectangle: ({x_min},{y_min}), ({x_max},{y_max})") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment