1
$\begingroup$

I have image (think of image of small plant) with bounding box. For data augmentation I want to rotate (45 degrees) the image (plant) in bounding box, and place it at same place in original image. so that I don't have to annotate again in image. Only difference is that image(plant) in box is rotated by 45 degrees. I am using cv2 and pil. I have below code writtend and got help up after that. Looking for inputs. Thanks for your time.

demo_test_img = 'repa_trn_23_4598958_2920949_png.rf.996ba731acd8b0d4b75c57acf85cc91b.jpg'
demo_test_img_path = os.path.join(f'{DIR_TRAIN}',demo_test_img)

# read image
demo_read_test_img = cv2.imread(demo_test_img_path, cv2.IMREAD_COLOR)

# colors for boxes. 
colour_dict = {1:(220, 0, 0), 2:(0, 0, 220)}

# get number of boxes present in image.
demo_img_boxes_info = train_df[train_df["image_id"] == demo_test_img]
print('Number of boxes for demo image ', len(demo_img_boxes_info))

for index, row in demo_img_boxes_info.iterrows():
  if (row['class'] == 1 or row['class'] == 2):

    H = int(row['h'])
    W = int(row['w'])
    X = int(row['x'])
    Y = int(row['y'])
    cropped_image = demo_read_test_img[Y:Y+H, X:X+W]
    print('Box for  idx is ', index, 'is ',[X,Y,W,H])

    **# TODO: rotate cropped image and place it at same location of original demo test image.** 
    
    

fig, ax = plt.subplots(1, 1, figsize=(16, 8))
ax.imshow(demo_read_test_img) 
$\endgroup$
3
  • $\begingroup$ I believe you are solving the incorrect task. The question should be - Find the bounding rectangle coordinate after rotation. Check Here $\endgroup$
    – 10xAI
    May 16, 2021 at 15:54
  • $\begingroup$ @10xAI I want to rotate only images present in bounding boxes. Thanks $\endgroup$ May 17, 2021 at 4:16
  • $\begingroup$ @10xAI I want to rotate only images present in bounding boxes and place it on original image with out change in bounding box. after giving a taught i think if we rotate the image in box the bounding box is going to change. As you mentioned the task i am trying to achieve is not correct. Is my understanding correct. Thanks $\endgroup$ May 17, 2021 at 4:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.