【问题标题】:Why 'imgaug Flipud' is not working for polygons?为什么“imgaug Flipud”不适用于多边形?
【发布时间】:2019-10-19 09:53:09
【问题描述】:
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,效果很好。

【问题讨论】:

    标签: python data-augmentation


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 2019-11-16
      • 1970-01-01
      • 2020-01-10
      • 2020-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多