Rotate the bounding box of object detection and crop it

Hi,

I got some trouble with rotating the bounding box of object detection.
I have an source image and a bounding box (xmin, ymin, xmax, ymax) of the object, and I rotate the image around its center by an angle WITHOUT cutting off the edge:

def rotate_image(mat, angle):
    # angle in degrees

    height, width = mat.shape[:2]
    image_center = (width / 2, height / 2)

    rotation_mat = cv2.getRotationMatrix2D(image_center, angle, 1.)
    abs_cos = abs(rotation_mat[0, 0])
    abs_sin = abs(rotation_mat[0, 1])

    bound_w = int(height * abs_sin + width * abs_cos)
    bound_h = int(height * abs_cos + width * abs_sin)

    rotation_mat[0, 2] += bound_w / 2 - image_center[0]
    rotation_mat[1, 2] += bound_h / 2 - image_center[1]
    rotated_mat = cv2.warpAffine(mat, rotation_mat, (bound_w, bound_h))
    return rotated_mat

I want to rotate the bounding box by the same degree and crop it out. How can i do that? Many thanks.

Best,

you have the rotation matrix. apply it to the corners of your bounding box.

of course, for angles other than 0, 90, 180, 270, 360, the box won’t be axis-aligned anymore.