import imgaug as ia
import imgaug.augmenters as iaa
import cv2
import numpy as np
import matplotlib.pyplot as plt
# image is numpy array of size (327, 327, 3).
images = np.expand_dims(image, axis=0)
# define polygons
polygons = [
[ ia.Polygon([(169, 173),
(140, 196),
(134, 225),
(142, 239),
(156, 250),
(174, 245),
(187, 230),
(187, 212),
(174, 197),
(165, 194),
(182, 178)]),
ia.Polygon([(149, 96),
(164, 72),
(183, 99),
(200, 93),
(171, 51),
(135, 92)])
]
]
# define augmentor
seq = iaa.OneOf([
#iaa.Affine(rotate=(-20, 20)),
#iaa.Fliplr(1),
iaa.Flipud(1)
])
# apply augmentation
images_aug, polygons_aug = seq(images=images, polygons=polygons)
# manipulate polygons obtained from augmentation
p1 = np.array(list(zip(polygons_aug[0][0].xx_int.tolist(), polygons[0][0].yy_int.tolist())))
p2 = np.array(list(zip(polygons_aug[0][1].xx_int.tolist(), polygons[0][1].yy_int.tolist())))
# combine polygons into list and draw onto the augmented image
p = [p1, p2]
cv2.drawContours(images_aug[0], p, -1, 255, 3)
plt.imshow(images_aug[0])
在这里,augmentor 给出与原始坐标相同的多边形坐标。它适用于 Fliplr 和 Affine,但不知何故不适用于 Flipud。我也尝试过带有边界框的 Flipud,它工作正常。