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

在这里,augmentor 给出与原始坐标相同的多边形坐标。它适用于 Fliplr 和 Affine,但不知何故不适用于 Flipud。我也尝试过带有边界框的 Flipud,它工作正常。

4

1 回答 1

0

我已经稍微编辑了您的代码,它在我的系统中运行。我附上以下代码:

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
于 2020-04-17T07:47:27.227 回答