albumentations-team / albumentations

Fast and flexible image augmentation library. Paper about the library: https://www.mdpi.com/2078-2489/11/2/125
https://albumentations.ai
MIT License
14.11k stars 1.64k forks source link

SafeRotate for bbox can not get right bbox #1149

Open aa12356jm opened 2 years ago

aa12356jm commented 2 years ago

🐛 Bug

To Reproduce

Steps to reproduce the behavior:

  1. run the code debug.py

Expected behavior

Environment

`

import albumentations as A
import cv2
import numpy as np
def draw_bbox_landmark(image, keypoints, bboxes):
    overlay = image.copy()
    for index,kp in enumerate(keypoints):
        cv2.putText(overlay,str(index),(int(kp[0]), int(kp[1])),cv2.FONT_HERSHEY_COMPLEX,2,(0,255,0),5)
        cv2.circle(overlay, (int(kp[0]), int(kp[1])), 10, (0, 0, 255), thickness=2, lineType=cv2.LINE_AA)

    for box in bboxes:
        cv2.rectangle(overlay, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), (255, 0, 0), thickness=2)

    return overlay

def visualize(image, keypoints, bboxes):
    overlay = image.copy()
    for kp in keypoints:
        cv2.circle(overlay, (int(kp[0]), int(kp[1])), 20, (0, 200, 200), thickness=2, lineType=cv2.LINE_AA)

    for box in bboxes:
        cv2.rectangle(overlay, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), (200, 0, 0), thickness=2)

    return overlay

def main():
    imgs_path_list = ['0.jpg,261,747,630,1116,501,1123,374,762,451,758,524,770,594,796,387,1052,295,932']
    aug = A.Compose(
        [
        # A.VerticalFlip(p=1.0),
        # A.HorizontalFlip(p=1.0),
        A.SafeRotate(limit=45,interpolation=cv2.INTER_LINEAR,border_mode=cv2.BORDER_CONSTANT,value=0,p=1.0),
        # A.ShiftScaleRotate(shift_limit=0.05, scale_limit=0.01, rotate_limit = 45, border_mode=cv2.BORDER_CONSTANT, p=1.0),
        ],
        bbox_params=A.BboxParams(format="pascal_voc",min_visibility=0.0, label_fields=["bbox_labels"]),
        keypoint_params=A.KeypointParams(format="xy"),)

    for index,line in enumerate(imgs_path_list):
        # if index % 500!=0:
        #     continue
        line_list = line.split(",")
        img_path = line_list[0]
        img_data_raw = cv2.imread(img_path)
        img_height,img_width,_ = img_data_raw.shape
        img_data = cv2.cvtColor(img_data_raw, cv2.COLOR_BGR2RGB)

        label_list = [eval(x) for x in line_list[1:]]

        palm_bbox_array = np.array(label_list[0:4]).reshape((2,-1))
        palm_bbox_array[:,0] = np.clip(palm_bbox_array[:,0],0,img_width)
        palm_bbox_array[:,1] = np.clip(palm_bbox_array[:,1],0,img_height)
        palm_bbox =  [tuple(palm_bbox_array.reshape(-1).tolist())]

        palm_landmark = np.array(label_list[4:]).reshape((7,-1))
        palm_landmark[:,0] = np.clip(palm_landmark[:,0],0,img_width)
        palm_landmark[:,1] = np.clip(palm_landmark[:,1],0,img_height)

        aug_data = aug(image=img_data, keypoints=palm_landmark, bboxes=palm_bbox, bbox_labels=np.ones(len(palm_bbox)))

        aug_image_draw = draw_bbox_landmark(aug_data["image"], aug_data["keypoints"], aug_data["bboxes"])
        aug_image_save = cv2.cvtColor(aug_image_draw, cv2.COLOR_RGB2BGR)

        img_data_raw_draw = draw_bbox_landmark(img_data_raw, palm_landmark, palm_bbox)

        merge_img=cv2.hconcat([img_data_raw_draw,aug_image_save])

        raw_img_save_path ="draw_"+img_path
        cv2.imwrite(raw_img_save_path, merge_img)

if __name__ == "__main__":
    main()

`

Additional context

draw_0 0

aa12356jm commented 2 years ago

@Dipet