Discussions>Augmentation techniques for images>

Augmentation techniques for images

I'm trying to train a model for image segmentation and I need augmentation techniques. Could you suggest some useful ways to do it?

3 votesJO295.00
1 Answers

Just use these functions.

from PIL import Image

def rotate(image, mask):
    angel = np.random.randint(-5, 5)
    img_rotate = image.rotate(angel)
    mask_rotate = None

    if mask:
        mask_rotate = mask.rotate(angel)

    return img_rotate, mask_rotate

def flip(image, mask):
    img_flip = image.transpose(Image.FLIP_LEFT_RIGHT)
    mask_flip = None

    if mask:
        mask_flip = mask.transpose(Image.FLIP_LEFT_RIGHT)

    return img_flip, mask_flip

def contrast(image, mask):
    factor = .4 * np.random.random() + .5
    enh = ImageEnhance.Contrast(image)
    image = enh.enhance(factor)

    return image, mask

def brightness(image, mask):
    factor = .4 * np.random.random() + .5
    enh = ImageEnhance.Brightness(image)
    image = enh.enhance(factor)

    return image, mask

def sharpness(image, mask):
    factor = np.random.random() + 1
    enh = ImageEnhance.Sharpness(image)
    image = enh.enhance(factor)

    return image, mask

def color(image, mask):
    factor = .5 * np.random.random() + .3
    enh = ImageEnhance.Color(image)
    image = enh.enhance(factor)

    return image, mask

Very useful. It saved my time so much.


Oh, cool. Thanks. This is a great answer

Couldn't find what you were looking for?and we will find an expert to answer.
How helpful was this page?