albumentations icon indicating copy to clipboard operation
albumentations copied to clipboard

Use key points augmentation option for polygons augmentation

Open xmba15 opened this issue 4 years ago • 5 comments

Hi, I would like to ask a question related to using albumentations for polygons augmentation. As also pointed out in this issue, polygons augmentation can be done by augmentation of keypoints. However, for polygons (that are used for instance segmentation), the order of the vertex points are important, I wonder if keypoints augmentation will preserve this order all the time?

xmba15 avatar Nov 12 '20 02:11 xmba15

Order of keypoints does not changed, but this is not guaranteed. You can add additional data to the tail of your keypoint to restore order order. For example:

keypoints = [val + [i] for i, val in enumerate(keypoints)]
res = transform(image=image, keypoints=keypoints)
keypoints = [i[:-1] for i in sorted(res['keypoints'])]

Dipet avatar Nov 16 '20 09:11 Dipet

you cannot do that, the keypoints augmentation is total different from polygons,the number of polygon vertex points maybe increase or decrease while some part of polygon is outside the new image

wangwei880403 avatar Sep 24 '21 05:09 wangwei880403

@Dipet yeah good idea, but do not forget for A.KeypointParams(format='xy', remove_invisible=False).

Sigura avatar Oct 22 '21 21:10 Sigura

but it looks like I found the tricky solution

transform = A.Compose([
   # ...
    A.Rotate(limit=15, border_mode=cv2.BORDER_REPLICATE),
   # ...
], keypoint_params=A.KeypointParams(format='xy', remove_invisible=False))

keypoints = [val + [i] for i, val in enumerate(keypoints)]
transformed = transform(image=image, keypoints=keypoints)
keypoints_trans = [i[:-1] for i in sorted(transformed['keypoints'], key=lambda x: x[2])]

# and return points to image
keypoints = [[max(min(x[0], width-1), 0), max(min(x[1], height-1), 0)] for x in keypoints_trans]

it works for me

@wangwei880403 it may helps

Sigura avatar Oct 22 '21 22:10 Sigura

How would to i did this (augmentation from scratch) for segmentation task in yolo format. . I looking for albumentations, but i dont find nothing about this, annotations following polygon yolo format

Felipecordeiiro avatar Feb 17 '24 19:02 Felipecordeiiro

Is there a solution for horizontal&vertical flip? These augmentations change point order after transformation, and the trick above does not work

XZLancer avatar Sep 04 '24 03:09 XZLancer