深度学习图像增强---python库imgaug

图像增强python库imgaug

  • landmark 增强
  • segmentation 增强

imgaug用来做图像增强的一个python库 1
图像增强是在小样本以及提高模型泛化能力的通常采用的措施。
下面总结一下我之前用到过的一些内容。

landmark 增强

对应于关键点检测

import imgaug as ia
from imgaug import augmenters as iaa

image = ia.quokka(size=(256, 256))
keypoints = ia.KeypointsOnImage([
    ia.Keypoint(x=65, y=100),
    ia.Keypoint(x=75, y=200),
    ia.Keypoint(x=100, y=100),
    ia.Keypoint(x=200, y=80)
], shape=image.shape)

seq = iaa.Sequential([
    iaa.Multiply((1.2, 1.5)), # change brightness, doesn't affect keypoints
    iaa.Affine(
        rotate=10,
        scale=(0.5, 0.7)
    ) # rotate by exactly 10deg and scale to 50-70%, affects keypoints
])

image_aug = seq_det.augment_images([image])[0] #image arrat
keypoints_aug = seq_det.augment_keypoints([keypoints])[0]
for i in range(len(keypoints.keypoints)):
    before = keypoints.keypoints[i] #using before.x before.y index the coordinate
    after = keypoints_aug.keypoints[i]
    print("Keypoint %d: (%.8f, %.8f) -> (%.8f, %.8f)" % (
        i, before.x, before.y, after.x, after.y)
    )

segmentation 增强

对应于图像分割,需要将图像的分割map与输入图像进行同步变换。

import imgaug as ia
from imgaug import augmenters as iaa
import imageio
import numpy as np
image = ia.quokka(size=(128, 128), extract="square")

segmap = np.zeros((128, 128), dtype=np.int32)
segmap[28:71, 35:85] = 1
segmap[10:25, 30:45] = 2
segmap[10:25, 70:85] = 3
segmap[10:110, 5:10] = 4
segmap[118:123, 10:110] = 5
segmap = ia.SegmentationMapOnImage(segmap, shape=image.shape, nb_classes=1+5)

# Define our augmentation pipeline.
seq = iaa.Sequential([
    iaa.Dropout([0.05, 0.2]),      # drop 5% or 20% of all pixels
    iaa.Sharpen((0.0, 1.0)),       # sharpen the image
    iaa.Affine(rotate=(-45, 45)),  # rotate by -45 to 45 degrees (affects heatmaps)
    iaa.ElasticTransformation(alpha=50, sigma=5)  # apply water effect (affects heatmaps)
], random_order=True)

# Augment images and heatmaps.
images_aug = []
segmaps_aug = []
for _ in range(5):
    seq_det = seq.to_deterministic()
    images_aug.append(seq_det.augment_image(image)[0])# append the augmented img array
    segmaps_aug.append(seq_det.augment_segmentation_maps([segmap])[0].arr)#append the augmented seg map array


  1. imgaug ↩︎

你可能感兴趣的:(pyLearning,深度学习,医疗图像分割)