Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing the rotating bbox issue #250

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions augly/image/utils/bboxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,20 +395,18 @@ def get_enclosing_bbox(

# Get rotated bbox corner coefficients
rotation_center = (src_w // 2, src_h // 2)
angle_rad = -math.radians(degrees)
angle_rad = math.radians(degrees)
rotation_matrix = [
round(math.cos(angle_rad), 15),
round(math.sin(angle_rad), 15),
0.0,
round(math.sin(angle_rad), 15),
round(-math.cos(angle_rad), 15),
round(-math.sin(angle_rad), 15),
round(math.cos(angle_rad), 15),
0.0,
]
rotation_matrix[2], rotation_matrix[5] = transform(
-rotation_center[0], -rotation_center[1], rotation_matrix
)
rotation_matrix[2] += rotation_center[0]
rotation_matrix[5] += rotation_center[1]

# Get rotated image dimensions
src_img_corners = [(0, 0), (src_w, 0), (src_w, src_h), (0, src_h)]
Expand All @@ -418,6 +416,10 @@ def get_enclosing_bbox(
rotated_img_max_x,
rotated_img_max_y,
) = get_enclosing_bbox(src_img_corners, rotation_matrix)

rotation_matrix[2] += rotated_img_max_x
rotation_matrix[5] += rotated_img_max_y

rotated_img_w = rotated_img_max_x - rotated_img_min_x
rotated_img_h = rotated_img_max_y - rotated_img_min_y

Expand All @@ -435,10 +437,10 @@ def get_enclosing_bbox(
# Crop bbox as src image is cropped inside `rotate`
cropped_w, cropped_h = imutils.rotated_rect_with_max_area(src_w, src_h, degrees)
cropped_img_left, cropped_img_upper, cropped_img_right, cropped_img_lower = (
(rotated_img_w - cropped_w) // 2 + rotated_img_min_x,
(rotated_img_h - cropped_h) // 2 + rotated_img_min_y,
(rotated_img_w + cropped_w) // 2 + rotated_img_min_x,
(rotated_img_h + cropped_h) // 2 + rotated_img_min_y,
(rotated_img_w - cropped_w) // 2 + rotated_img_min_x + rotated_img_max_x,
(rotated_img_h - cropped_h) // 2 + rotated_img_min_y + rotated_img_max_y,
(rotated_img_w + cropped_w) // 2 + rotated_img_min_x + rotated_img_max_x,
(rotated_img_h + cropped_h) // 2 + rotated_img_min_y + rotated_img_max_y,
)
return crop_bboxes_helper(
bbox_enclosing_bbox,
Expand Down
535 changes: 535 additions & 0 deletions augly/tests/image_tests/issue_of_bbox_rotation_AugLy_image.ipynb

Large diffs are not rendered by default.

171 changes: 171 additions & 0 deletions augly/tests/image_tests/test_file_after_fixing.ipynb

Large diffs are not rendered by default.