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

Fix for rotating bounding box #249

Open
LeoLee7 opened this issue Nov 8, 2023 · 3 comments
Open

Fix for rotating bounding box #249

LeoLee7 opened this issue Nov 8, 2023 · 3 comments

Comments

@LeoLee7
Copy link

LeoLee7 commented Nov 8, 2023

🐛 Bug

To Reproduce

Steps to reproduce the behavior:

  1. When I tried to visualize the bounding box after rotation (90 degree), it seems not aligned with the original bounded area.
  2. We can not get the original bbox when with apply a 90 degree rotation first and then apply a -90 degree rotation operation. (But the image looks good) - - We can also try the 360 degree rotation, and the bbox is changed. Ideally if we perform 360 degree rotation, the image and bbox should be identical to the original status.
  3. The test case is at https://github.com/LeoLee7/AugLy/blob/main/augly/tests/image_tests/issue_of_bbox_rotation_AugLy_image.ipynb
@LeoLee7
Copy link
Author

LeoLee7 commented Nov 9, 2023

I think the issue might be at rotate_bbox_helper and I fixed it with two changes

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),
    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)]
(
    rotated_img_min_x,
    rotated_img_min_y,
    rotated_img_max_x,
    rotated_img_max_y,
) = get_enclosing_bbox(src_img_corners, rotation_matrix)
rotated_img_w = rotated_img_max_x - rotated_img_min_x
rotated_img_h = rotated_img_max_y - rotated_img_min_y

Potential solution:

1. rotation matrix initialization:
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),
    0.0,
]
2. coordinate translation: update the transform matrix after the calculation of rotated image corners.
src_img_corners = [(0, 0), (src_w, 0), (src_w, src_h), (0, src_h)]
(
    rotated_img_min_x,
    rotated_img_min_y,
    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

@LeoLee7
Copy link
Author

LeoLee7 commented Nov 9, 2023

One more fixing:
3. the cropping operation: add the translation operation to the cropped-img rotate_bbox_helper

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_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,
)

@LeoLee7
Copy link
Author

LeoLee7 commented Nov 9, 2023

Test case after fixing : [aligned rotating bbox]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant