aleju / imgaug

Image augmentation for machine learning experiments.
http://imgaug.readthedocs.io
MIT License
14.42k stars 2.44k forks source link

Incorrect rotation for bounding box #806

Open sarimmehdi opened 2 years ago

sarimmehdi commented 2 years ago

I have an image of width 640 and height 480. I load the image in and rotate it by 90 degrees in the clockwise direction because it is not right side up. Then, I put it through a neural net to get bounding box coordinates. I plot the bbox coordinates and I get the correct result as shown below:

image_0

Now, I want to rotate the bbox so that it is correct for the original image (which I had to rotate in the first place to make it work with my neural net). This means I will have to rotate the bbox 90 degrees anti-clockwise before plotting it on the original image. This is the code I use:

# where image is the original one which is not the right side up
image = cv2.imread(data_obj.image_path_server)
h, w, _ = image.shape

# x1, y1, x2, y2 were obtained earlier through inference with a neural net
bbs = BoundingBoxesOnImage([
    BoundingBox(x1=x1, y1=y1, x2=x2, y2=y2)
], shape=(h, w))

# rotate 270 degrees clockwise
seq = iaa.Sequential([iaa.Affine(rotate=270)])
bbs_aug = seq(bounding_boxes=bbs)
for i in range(len(bbs.bounding_boxes)):
    before = bbs.bounding_boxes[i]

    # rotate the original image clockwise to make it right side up
    rotated_img = cv2.rotate(image, cv2.cv2.ROTATE_90_CLOCKWISE)
    cv2.rectangle(rotated_img, (x1, y1), (x2, y2), (255, 255, 255), 2)
    cv2.imshow('old', rotated_img)      # this works as expected (as shown in image above)
    after = bbs_aug.bounding_boxes[i]
    logger.debug(f'new bbox: {*[after.x1_int, after.y1_int, after.x2_int, after.y2_int], }')
    x1, y1, x2, y2 = after.x1_int, after.y1_int, after.x2_int, after.y2_int
logger.debug(f'new bbox: {*[x1, y1, x2, y2], }')
cv2.rectangle(image, (x1, y1), (x2, y2), (255, 255, 255), 2)
cv2.imshow('new', image)
cv2.waitKey(100000)

This is what the new image looks like:

image_1

Please let me know if there I am not using the library correctly. Thanks!