0
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])

Here, augmentor is giving same polygon coordinates as the original coordinates. It works in case of Fliplr and Affine but somehow is not working for Flipud. I have also tried Flipud with bounding box too and it is working fine.

Sanchit.Jain
  • 568
  • 2
  • 7

1 Answers1

0

I have edited your code a little bit and it worked in my system. I am attaching the code below:

import imgaug as ia
import imgaug.augmenters as iaa
import cv2
import numpy as np
from imgaug.augmentables.polys import PolygonsOnImage
import matplotlib.pyplot as plt

# image is numpy array of size (327, 327, 3).
images = np.expand_dims(image, axis=0)

# define polygons
# remove list(list(polygon)), in single list put two 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)
])
# You forgot to mention one line of code
new_polygons = PolygonsOnImage(polygons, shape=image.shape)

# apply augmentation
images_aug, polygons_aug = seq(images=images, polygons=new_polygons)
image_augmented = polygons_aug.draw_on_image(images_aug)
cv2.imshow("augmented image", image_augmented)
cv2.waitKey(0)
cv2.destroyAllWindows



enter code here