imgaug学习笔记

2019-08-06  本文已影响0人  Cat丹

方法一

seq_det = seq.to_deterministic()
image_aug = seq_det.augment_image(image)
print(image.shape)
mask_aug = seq_det.augment_image(mask)
mask_aug[mask_aug == 127] = 2
mask_aug = ia.SegmentationMapOnImage(mask_aug, nb_classes=3, shape=mask_aug.shape)
ia.imshow(np.hstack(
    [mask_aug.draw_on_image(image_aug),
     mask_aug.draw()]
))

第一次得到正确结果,但是第二次报错

File "aug_test.py", line 144, in <module>
mask_aug = seq_det.augment_image(mask)
File "/usr/local/lib/python3.5/dist-packages/imgaug/augmenters/meta.py", line 470, in augment_image
return self.augment_images([image], hooks=hooks)[0]
File "/usr/local/lib/python3.5/dist-packages/imgaug/augmenters/meta.py", line 603, in augment_images
hooks=hooks
File "/usr/local/lib/python3.5/dist-packages/imgaug/augmenters/meta.py", line 2816, in _augment_images
hooks=hooks
File "/usr/local/lib/python3.5/dist-packages/imgaug/augmenters/meta.py", line 515, in augment_images
hooks=hooks
File "/usr/local/lib/python3.5/dist-packages/imgaug/augmenters/meta.py", line 3118, in _augment_images
hooks=hooks
File "/usr/local/lib/python3.5/dist-packages/imgaug/augmenters/meta.py", line 515, in augment_images
hooks=hooks
File "/usr/local/lib/python3.5/dist-packages/imgaug/augmenters/color.py", line 554, in _augment_images
img_to_cs = cv2.cvtColor(image, from_to_var)
cv2.error: OpenCV(3.4.1) /io/opencv/modules/imgproc/src/color.cpp:11147: error: (-215) scn == 3 || scn == 4 in function cvtColor

解决方法:

添加hooks,mask不做色彩相关操作

def activator_heatmaps(images, augmenter, parents, default):
    if augmenter.name in [
        "CoarseDropout", "GrayScale", "GaussianBlur", "AverageBlur", "MedianBlur", "Sharpen",
        "ContrastNormalization", "FrequencyNoiseAlpha", "Multiply", "AddToHueAndSaturation"]:
        return False
    else:
        # default value for all other augmenters
        return default


hooks_heatmaps = ia.HooksImages(activator=activator_heatmaps)
seq_det = seq.to_deterministic()
image_aug = seq_det.augment_image(image)
mask_aug = seq_det.augment_image(mask, hooks=hooks_heatmaps)
ok.png

方法二

# mask为单通道
from imgaug.augmentables.segmaps import SegmentationMapOnImage
segmap = SegmentationMapOnImage(mask, nb_classes=2, shape=image.shape)
seq_det = seq.to_deterministic()
image_aug = seq_det.augment_image(image)
mask_aug = seq_det.augment_segmentation_maps(segmap)
ia.imshow(np.hstack(
    [mask_aug.draw_on_image(image_aug),
     mask_aug.draw()]
))

程序运行正常,但是mask与原图不匹配


error.jpg

解决方法:Affine变换时mode用"constant"

iaa.Affine(
                scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},
                # scale images to 80-120% of their size, individually per axis
                translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},  # translate by -20 to +20 percent (per axis)
                rotate=(-45, 45),  # rotate by -45 to +45 degrees
                shear=(-16, 16),  # shear by -16 to +16 degrees
                order=[0, 1],  # use nearest neighbour or bilinear interpolation (fast)
                cval=127,  # if mode is constant, use a cval between 0 and 255
                mode="constant" # use any of scikit-image's warping modes (see 2nd image from the top for examples)
            )

给image和mask指定不同的pad值

seq_img = iaa.Sequential([
    iaa.Add((-10, 10), name="MyAdd"),
    iaa.Affine(rotate=(-45, 45), order=1, mode=["constant", "edges"], cval=(0, 255), name="MyAffine")
])
seq_masks = iaa.Sequential([
    iaa.Affine(rotate=(-45, 45), order=0, mode="constant", cval=0, name="MyAffine")
])

for batch in batches:
    seq_masks_i = seq_masks.copy_random_state(seq_img, matching="name")
    seq_masks_i = seq_masks_i.to_deterministic()
    seq_img_i = seq_img.to_deterministic()

    imgs_aug = seq_img_i.augment_images(batch.images)
    masks_aug = seq_masks_i.augment_images(batch.masks.astype(np.uint8) * 255) >= 128

RGB值插入

import numpy as np
import imgaug as ia
from imgaug import augmenters as iaa
from imgaug import parameters as iap

class DeterministicColor(iap.StochasticParameter):
    def __init__(self, color):
        self.color = np.uint8(color)

    def _draw_samples(self, size, random_state):
        assert size[-1] == 3
        arr = np.zeros(size, dtype=np.uint8)
        arr[..., :] = self.color
        return arr

aug = iaa.Affine(rotate=45,
                 cval=DeterministicColor([0, 0, 255]),
                 mode="constant")
image_aug = aug.augment_image(ia.quokka(size=(128, 128)))
ia.imshow(image_aug)
上一篇下一篇

猜你喜欢

热点阅读