Code Monkey home page Code Monkey logo

Comments (3)

LeoLee7 avatar LeoLee7 commented on September 27, 2024

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

from augly.

LeoLee7 avatar LeoLee7 commented on September 27, 2024

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

from augly.

LeoLee7 avatar LeoLee7 commented on September 27, 2024

Test case after fixing : [aligned rotating bbox]

from augly.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.