albumentations
albumentations copied to clipboard
Use key points augmentation option for polygons augmentation
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?
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'])]
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
@Dipet yeah good idea, but do not forget for A.KeypointParams(format='xy', remove_invisible=False)
.
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
How would to i did this (augmentation from scratch) for segmentation task in yolo format.
Is there a solution for horizontal&vertical flip? These augmentations change point order after transformation, and the trick above does not work