augmenters.geometric¶
Affine¶
Augmenter to apply affine transformations to images.
API link: Affine
Example. Scale images to a value of 50 to 150% of their original size:
import imgaug.augmenters as iaa
aug = iaa.Affine(scale=(0.5, 1.5))
Example. Scale images to a value of 50 to 150% of their original size, but do this independently per axis (i.e. sample two values per image):
aug = iaa.Affine(scale={"x": (0.5, 1.5), "y": (0.5, 1.5)})
Example. Translate images by -20 to +20% on x- and y-axis independently:
aug = iaa.Affine(translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)})
Example. Translate images by -20 to 20 pixels on x- and y-axis independently:
aug = iaa.Affine(translate_px={"x": (-20, 20), "y": (-20, 20)})
Example. Rotate images by -45 to 45 degrees:
aug = iaa.Affine(rotate=(-45, 45))
Example. Shear images by -16 to 16 degrees:
aug = iaa.Affine(shear=(-16, 16))
Example.
When applying affine transformations, new pixels are often generated, e.g. when
translating to the left, pixels are generated on the right. Various modes
exist to set how these pixels are ought to be filled. Below code shows an
example that uses all modes, sampled randomly per image. If the mode is
constant
(fill all with one constant value), then a random brightness
between 0 and 255 is used:
aug = iaa.Affine(translate_percent={"x": -0.20}, mode=ia.ALL, cval=(0, 255))
PiecewiseAffine¶
Apply affine transformations that differ between local neighbourhoods.
This augmenter places a regular grid of points on an image and randomly moves the neighbourhood of these point around via affine transformations. This leads to local distortions.
This is mostly a wrapper around scikit-image’s PiecewiseAffine
.
See also Affine
for a similar technique.
Note
This augmenter is very slow. See Performance.
Try to use ElasticTransformation
instead, which is at least 10x
faster.
Note
For coordinate-based inputs (keypoints, bounding boxes, polygons, …), this augmenter still has to perform an image-based augmentation, which will make it significantly slower for such inputs than other augmenters. See Performance.
API link: PiecewiseAffine
Example.
Distort images locally by moving points around, each with a distance v (percent
relative to image size), where v is sampled per point from N(0, z)
z
is sampled per image from the range 0.01 to 0.05:
import imgaug.augmenters as iaa
aug = iaa.PiecewiseAffine(scale=(0.01, 0.05))
Example.
Effect of increasing scale
from 0.01
to 0.3
in eight steps:
Example. PiecewiseAffine works by placing a regular grid of points on the image and moving them around. By default this grid consists of 4x4 points. The below image shows the effect of increasing that value from 2x2 to 16x16 in 8 steps:
PerspectiveTransform¶
Apply random four point perspective transformations to images.
Each of the four points is placed on the image using a random distance from its respective corner. The distance is sampled from a normal distribution. As a result, most transformations don’t change the image very much, while some “focus” on polygons far inside the image.
The results of this augmenter have some similarity with Crop
.
API link: PerspectiveTransform
Example.
Apply perspective transformations using a random scale between 0.01
and 0.15
per image, where the scale is roughly a measure of how far
the perspective transformation’s corner points may be distanced from the
image’s corner points:
import imgaug.augmenters as iaa
aug = iaa.PerspectiveTransform(scale=(0.01, 0.15))
Example. Same as in the previous example, but images are not resized back to the input image size after augmentation. This will lead to smaller output images.
aug = iaa.PerspectiveTransform(scale=(0.01, 0.15), keep_size=False)
ElasticTransformation¶
Transform images by moving pixels locally around using displacement fields.
The augmenter has the parameters alpha
and sigma
. alpha
controls the strength of the displacement: higher values mean that pixels
are moved further. sigma
controls the smoothness of the displacement:
higher values lead to smoother patterns – as if the image was below water
– while low values will cause indivdual pixels to be moved very
differently from their neighbours, leading to noisy and pixelated images.
A relation of 10:1 seems to be good for alpha
and sigma
, e.g.
alpha=10
and sigma=1
or alpha=50
, sigma=5
. For 128x128
a setting of alpha=(0, 70.0)
, sigma=(4.0, 6.0)
may be a good
choice and will lead to a water-like effect.
For a detailed explanation, see
Simard, Steinkraus and Platt
Best Practices for Convolutional Neural Networks applied to Visual
Document Analysis
in Proc. of the International Conference on Document Analysis and
Recognition, 2003
Note
For coordinate-based inputs (keypoints, bounding boxes, polygons, …), this augmenter still has to perform an image-based augmentation, which will make it significantly slower for such inputs than other augmenters. See Performance.
API link: ElasticTransformation
Example. Distort images locally by moving individual pixels around following a distortions field with strength 0.25. The strength of the movement is sampled per pixel from the range 0 to 5.0:
import imgaug.augmenters as iaa
aug = iaa.ElasticTransformation(alpha=(0, 5.0), sigma=0.25)
Example.
Effect of keeping sigma fixed at 0.25
and increasing alpha from 0
to
5.0
in eight steps:
Example.
Effect of keeping alpha fixed at 2.5
and increasing sigma from 0.01
to 1.0
in eight steps:
Rot90¶
Rotate images clockwise by multiples of 90 degrees.
This could also be achieved using Affine
, but Rot90
is
significantly more efficient.
API link: Rot90
Example. Rotate all images by 90 degrees. Resize these images afterwards to keep the size that they had before augmentation. This may cause the images to look distorted.
import imgaug.augmenters as iaa
aug = iaa.Rot90(1)
Example. Rotate all images by 90 or 270 degrees. Resize these images afterwards to keep the size that they had before augmentation. This may cause the images to look distorted.
aug = iaa.Rot90([1, 3])
Example. Rotate all images by 90, 180 or 270 degrees. Resize these images afterwards to keep the size that they had before augmentation. This may cause the images to look distorted.
aug = iaa.Rot90((1, 3))
Example. Rotate all images by 90, 180 or 270 degrees. Does not resize to the original image size afterwards, i.e. each image’s size may change.
aug = iaa.Rot90((1, 3), keep_size=False)