imgaug

imgaug.imgaug.BackgroundAugmenter(*args, **kwargs)
imgaug.imgaug.Batch(*args, **kwargs)
imgaug.imgaug.BatchLoader(*args, **kwargs)
imgaug.imgaug.BoundingBox(*args, **kwargs)
imgaug.imgaug.BoundingBoxesOnImage(*args, **kwargs)
exception imgaug.imgaug.DeprecationWarning[source]

Bases: Warning

Warning for deprecated calls.

Since python 2.7 DeprecatedWarning is silent by default. So we define our own DeprecatedWarning here so that it is not silent by default.

imgaug.imgaug.HeatmapsOnImage(*args, **kwargs)
class imgaug.imgaug.HooksHeatmaps(activator=None, propagator=None, preprocessor=None, postprocessor=None)[source]

Bases: imgaug.imgaug.HooksImages

Class to intervene with heatmap augmentation runs.

This is e.g. useful to dynamically deactivate some augmenters.

This class is currently the same as the one for images. This may or may not change in the future.

Methods

is_activated(self, images, augmenter, …) Estimate whether an augmenter may be executed.
is_propagating(self, images, augmenter, …) Estimate whether an augmenter may call its children.
postprocess(self, images, augmenter, parents) Postprocess input data per augmenter after augmentation.
preprocess(self, images, augmenter, parents) Preprocess input data per augmenter before augmentation.
class imgaug.imgaug.HooksImages(activator=None, propagator=None, preprocessor=None, postprocessor=None)[source]

Bases: object

Class to intervene with image augmentation runs.

This is e.g. useful to dynamically deactivate some augmenters.

Parameters:
  • activator (None or callable, optional) – A function that gives permission to execute an augmenter. The expected interface is:

    ``f(images, augmenter, parents, default)``
    

    where images are the input images to augment, augmenter is the instance of the augmenter to execute, parents are previously executed augmenters and default is an expected default value to be returned if the activator function does not plan to make a decision for the given inputs.

  • propagator (None or callable, optional) – A function that gives permission to propagate the augmentation further to the children of an augmenter. This happens after the activator. In theory, an augmenter may augment images itself (if allowed by the activator) and then execute child augmenters afterwards (if allowed by the propagator). If the activator returned False, the propagation step will never be executed. The expected interface is:

    ``f(images, augmenter, parents, default)``
    

    with all arguments having identical meaning to the activator.

  • preprocessor (None or callable, optional) – A function to call before an augmenter performed any augmentations. The interface is:

    f(images, augmenter, parents)

    with all arguments having identical meaning to the activator. It is expected to return the input images, optionally modified.

  • postprocessor (None or callable, optional) – A function to call after an augmenter performed augmentations. The interface is the same as for the preprocessor.

Examples

>>> import numpy as np
>>> import imgaug as ia
>>> import imgaug.augmenters as iaa
>>> seq = iaa.Sequential([
>>>     iaa.GaussianBlur(3.0, name="blur"),
>>>     iaa.Dropout(0.05, name="dropout"),
>>>     iaa.Affine(translate_px=-5, name="affine")
>>> ])
>>> images = [np.zeros((10, 10), dtype=np.uint8)]
>>>
>>> def activator(images, augmenter, parents, default):
>>>     return False if augmenter.name in ["blur", "dropout"] else default
>>>
>>> seq_det = seq.to_deterministic()
>>> images_aug = seq_det.augment_images(images)
>>> heatmaps = [np.random.rand(*(3, 10, 10))]
>>> heatmaps_aug = seq_det.augment_images(
>>>     heatmaps,
>>>     hooks=ia.HooksImages(activator=activator)
>>> )

This augments images and their respective heatmaps in the same way. The heatmaps however are only modified by Affine, not by GaussianBlur or Dropout.

Methods

is_activated(self, images, augmenter, …) Estimate whether an augmenter may be executed.
is_propagating(self, images, augmenter, …) Estimate whether an augmenter may call its children.
postprocess(self, images, augmenter, parents) Postprocess input data per augmenter after augmentation.
preprocess(self, images, augmenter, parents) Preprocess input data per augmenter before augmentation.
is_activated(self, images, augmenter, parents, default)[source]

Estimate whether an augmenter may be executed.

This also affects propagation of data to child augmenters.

Returns:If True, the augmenter may be executed. Otherwise False.
Return type:bool
is_propagating(self, images, augmenter, parents, default)[source]

Estimate whether an augmenter may call its children.

This function decides whether an augmenter with children is allowed to call these in order to further augment the inputs. Note that if the augmenter itself performs augmentations (before/after calling its children), these may still be executed, even if this method returns False.

Returns:If True, the augmenter may propagate data to its children. Otherwise False.
Return type:bool
postprocess(self, images, augmenter, parents)[source]

Postprocess input data per augmenter after augmentation.

Returns:The input images, optionally modified.
Return type:(N,H,W,C) ndarray or (N,H,W) ndarray or list of (H,W,C) ndarray or list of (H,W) ndarray
preprocess(self, images, augmenter, parents)[source]

Preprocess input data per augmenter before augmentation.

Returns:The input images, optionally modified.
Return type:(N,H,W,C) ndarray or (N,H,W) ndarray or list of (H,W,C) ndarray or list of (H,W) ndarray
class imgaug.imgaug.HooksKeypoints(activator=None, propagator=None, preprocessor=None, postprocessor=None)[source]

Bases: imgaug.imgaug.HooksImages

Class to intervene with keypoint augmentation runs.

This is e.g. useful to dynamically deactivate some augmenters.

This class is currently the same as the one for images. This may or may not change in the future.

Methods

is_activated(self, images, augmenter, …) Estimate whether an augmenter may be executed.
is_propagating(self, images, augmenter, …) Estimate whether an augmenter may call its children.
postprocess(self, images, augmenter, parents) Postprocess input data per augmenter after augmentation.
preprocess(self, images, augmenter, parents) Preprocess input data per augmenter before augmentation.
imgaug.imgaug.Keypoint(*args, **kwargs)
imgaug.imgaug.KeypointsOnImage(*args, **kwargs)
imgaug.imgaug.MultiPolygon(*args, **kwargs)
imgaug.imgaug.Polygon(*args, **kwargs)
imgaug.imgaug.PolygonsOnImage(*args, **kwargs)
imgaug.imgaug.SegmentationMapsOnImage(*args, **kwargs)
imgaug.imgaug.angle_between_vectors(v1, v2)[source]

Calculcate the angle in radians between vectors v1 and v2.

From http://stackoverflow.com/questions/2827393/angles-between-two-n-dimensional-vectors-in-python

Parameters:
  • v1 ((N,) ndarray) – First vector.
  • v2 ((N,) ndarray) – Second vector.
Returns:

Angle in radians.

Return type:

float

Examples

>>> angle_between_vectors(np.float32([1, 0, 0]), np.float32([0, 1, 0]))
1.570796...
>>> angle_between_vectors(np.float32([1, 0, 0]), np.float32([1, 0, 0]))
0.0
>>> angle_between_vectors(np.float32([1, 0, 0]), np.float32([-1, 0, 0]))
3.141592...
imgaug.imgaug.avg_pool(arr, block_size, pad_mode='reflect', pad_cval=128, preserve_dtype=True, cval=None)[source]

Resize an array using average pooling.

Defaults to pad_mode="reflect" to ensure that padded values do not affect the average.

dtype support:

See :func:`imgaug.imgaug.pool`.
Parameters:
  • arr ((H,W) ndarray or (H,W,C) ndarray) – Image-like array to pool. See imgaug.imgaug.pool() for details.
  • block_size (int or tuple of int or tuple of int) – Size of each block of values to pool. See imgaug.imgaug.pool() for details.
  • pad_mode (str, optional) – Padding mode to use if the array cannot be divided by block_size without remainder. See imgaug.imgaug.pad() for details.
  • pad_cval (number, optional) – Padding value. See imgaug.imgaug.pool() for details.
  • preserve_dtype (bool, optional) – Whether to preserve the input array dtype. See imgaug.imgaug.pool() for details.
  • cval (None or number, optional) – Deprecated. Old name for pad_cval.
Returns:

Array after average pooling.

Return type:

(H’,W’) ndarray or (H’,W’,C’) ndarray

imgaug.imgaug.caller_name()[source]

Return the name of the caller, e.g. a function.

Returns:The name of the caller as a string
Return type:str
imgaug.imgaug.compute_geometric_median(*args, **kwargs)
imgaug.imgaug.compute_line_intersection_point(x1, y1, x2, y2, x3, y3, x4, y4)[source]

Compute the intersection point of two lines.

Taken from https://stackoverflow.com/a/20679579 .

Parameters:
  • x1 (number) – x coordinate of the first point on line 1. (The lines extends beyond this point.)
  • y1 (number) – y coordinate of the first point on line 1. (The lines extends beyond this point.)
  • x2 (number) – x coordinate of the second point on line 1. (The lines extends beyond this point.)
  • y2 (number) – y coordinate of the second point on line 1. (The lines extends beyond this point.)
  • x3 (number) – x coordinate of the first point on line 2. (The lines extends beyond this point.)
  • y3 (number) – y coordinate of the first point on line 2. (The lines extends beyond this point.)
  • x4 (number) – x coordinate of the second point on line 2. (The lines extends beyond this point.)
  • y4 (number) – y coordinate of the second point on line 2. (The lines extends beyond this point.)
Returns:

The coordinate of the intersection point as a tuple (x, y). If the lines are parallel (no intersection point or an infinite number of them), the result is False.

Return type:

tuple of number or bool

imgaug.imgaug.compute_paddings_for_aspect_ratio(arr, aspect_ratio)[source]

Compute pad amounts required to fulfill an aspect ratio.

“Pad amounts” here denotes the number of pixels that have to be added to each side to fulfill the desired constraint.

The aspect ratio is given as ratio = width / height. Depending on which dimension is smaller (height or width), only the corresponding sides (top/bottom or left/right) will be padded.

The axis-wise padding amounts are always distributed equally over the sides of the respective axis (i.e. left and right, top and bottom). For odd pixel amounts, one pixel will be left over after the equal distribution and could be added to either side of the axis. This function will always add such a left over pixel to the bottom (y-axis) or right (x-axis) side.

Parameters:
  • arr ((H,W) ndarray or (H,W,C) ndarray) – Image-like array for which to compute pad amounts.
  • aspect_ratio (float) – Target aspect ratio, given as width/height. E.g. 2.0 denotes the image having twice as much width as height.
Returns:

Required padding amounts to reach the target aspect ratio, given as a tuple of the form (top, right, bottom, left).

Return type:

tuple of int

imgaug.imgaug.compute_paddings_to_reach_multiples_of(arr, height_multiple, width_multiple)[source]

Compute pad amounts until img height/width are multiples of given values.

See imgaug.imgaug.compute_paddings_for_aspect_ratio() for an explanation of how the required padding amounts are distributed per image axis.

Parameters:
  • arr ((H,W) ndarray or (H,W,C) ndarray) – Image-like array for which to compute pad amounts.
  • height_multiple (None or int) – The desired multiple of the height. The computed padding amount will reflect a padding that increases the y axis size until it is a multiple of this value.
  • width_multiple (None or int) – The desired multiple of the width. The computed padding amount will reflect a padding that increases the x axis size until it is a multiple of this value.
Returns:

Required padding amounts to reach multiples of the provided values, given as a tuple of the form (top, right, bottom, left).

Return type:

tuple of int

imgaug.imgaug.copy_random_state(random_state, force_copy=False)[source]

Deprecated. Use imgaug.random.copy_generator_unless_global_rng instead.

Copy an existing numpy (random number) generator.

Parameters:
  • random_state (numpy.random.Generator or numpy.random.RandomState) – The generator to copy.
  • force_copy (bool, optional) – If True, this function will always create a copy of every random state. If False, it will not copy numpy’s default random state, but all other random states.
Returns:

rs_copy – The copied random state.

Return type:

numpy.random.RandomState

imgaug.imgaug.current_random_state()[source]

Deprecated. Use imgaug.random.get_global_rng instead.

Get or create the current global RNG of imgaug.

Note that the first call to this function will create a global RNG.

Returns:The global RNG to use.
Return type:imgaug.random.RNG
class imgaug.imgaug.deprecated(alt_func=None, behavior='warn', removed_version=None, comment=None)[source]

Bases: object

Decorator to mark deprecated functions with warning.

Adapted from <https://github.com/scikit-image/scikit-image/blob/master/skimage/_shared/utils.py>.

Parameters:
  • alt_func (None or str, optional) – If given, tell user what function to use instead.
  • behavior ({‘warn’, ‘raise’}, optional) – Behavior during call to deprecated function: warn means that the user is warned that the function is deprecated; raise means that an error is raised.
  • removed_version (None or str, optional) – The package version in which the deprecated function will be removed.
  • comment (None or str, optional) – An optional comment that will be appended to the warning message.

Methods

__call__(self, func) Call self as a function.
imgaug.imgaug.derive_random_state(random_state)[source]

Deprecated. Use imgaug.random.derive_generator_ instead.

Derive a child numpy random generator from another one.

Parameters:random_state (numpy.random.Generator or numpy.random.RandomState) – The generator from which to derive a new child generator.
Returns:In numpy <=1.16 a RandomState, in 1.17+ a Generator. In both cases a derived child generator.
Return type:numpy.random.Generator or numpy.random.RandomState
imgaug.imgaug.derive_random_states(random_state, n=1)[source]

Deprecated. Use imgaug.random.derive_generators_ instead.

Derive child numpy random generators from another one.

Parameters:
  • random_state (numpy.random.Generator or numpy.random.RandomState) – The generator from which to derive new child generators.
  • n (int, optional) – Number of child generators to derive.
Returns:

In numpy <=1.16 a list of RandomState s, in 1.17+ a list of Generator s. In both cases lists of derived child generators.

Return type:

list of numpy.random.Generator or list of numpy.random.RandomState

imgaug.imgaug.do_assert(condition, message='Assertion failed.')[source]

Assert that a condition holds or raise an Exception otherwise.

This was added because assert statements are removed in optimized code. It replaced assert statements throughout the library, but that was reverted again for readability and performance reasons.

Parameters:
  • condition (bool) – If False, an exception is raised.
  • message (str, optional) – Error message.
imgaug.imgaug.draw_grid(images, rows=None, cols=None)[source]

Combine multiple images into a single grid-like image.

Calling this function with four images of the same shape and rows=2, cols=2 will combine the four images to a single image array of shape (2*H, 2*W, C), where H is the height of any of the images (analogous W) and C is the number of channels of any image.

Calling this function with four images of the same shape and rows=4, cols=1 is analogous to calling numpy.vstack() on the images.

dtype support:

* ``uint8``: yes; fully tested
* ``uint16``: yes; fully tested
* ``uint32``: yes; fully tested
* ``uint64``: yes; fully tested
* ``int8``: yes; fully tested
* ``int16``: yes; fully tested
* ``int32``: yes; fully tested
* ``int64``: yes; fully tested
* ``float16``: yes; fully tested
* ``float32``: yes; fully tested
* ``float64``: yes; fully tested
* ``float128``: yes; fully tested
* ``bool``: yes; fully tested
Parameters:
  • images ((N,H,W,3) ndarray or iterable of (H,W,3) array) – The input images to convert to a grid.
  • rows (None or int, optional) – The number of rows to show in the grid. If None, it will be automatically derived.
  • cols (None or int, optional) – The number of cols to show in the grid. If None, it will be automatically derived.
Returns:

Image of the generated grid.

Return type:

(H’,W’,3) ndarray

imgaug.imgaug.draw_text(img, y, x, text, color=(0, 255, 0), size=25)[source]

Draw text on an image.

This uses by default DejaVuSans as its font, which is included in this library.

dtype support:

* ``uint8``: yes; fully tested
* ``uint16``: no
* ``uint32``: no
* ``uint64``: no
* ``int8``: no
* ``int16``: no
* ``int32``: no
* ``int64``: no
* ``float16``: no
* ``float32``: yes; not tested
* ``float64``: no
* ``float128``: no
* ``bool``: no

TODO check if other dtypes could be enabled
Parameters:
  • img ((H,W,3) ndarray) – The image array to draw text on. Expected to be of dtype uint8 or float32 (expected value range is [0.0, 255.0]).
  • y (int) – x-coordinate of the top left corner of the text.
  • x (int) – y- coordinate of the top left corner of the text.
  • text (str) – The text to draw.
  • color (iterable of int, optional) – Color of the text to draw. For RGB-images this is expected to be an RGB color.
  • size (int, optional) – Font size of the text to draw.
Returns:

Input image with text drawn on it.

Return type:

(H,W,3) ndarray

imgaug.imgaug.dummy_random_state()[source]

Deprecated. Use imgaug.random.convert_seed_to_rng instead.

Create a dummy random state using a seed of 1.

Returns:The new random state.
Return type:imgaug.random.RNG
imgaug.imgaug.flatten(nested_iterable)[source]

Flatten arbitrarily nested lists/tuples.

Code partially taken from https://stackoverflow.com/a/10824420.

Parameters:nested_iterable – A list or tuple of arbitrarily nested values.
Yields:any – All values in nested_iterable, flattened.
imgaug.imgaug.forward_random_state(random_state)[source]

Deprecated. Use imgaug.random.advance_generator_ instead.

Advance a numpy random generator’s internal state.

Parameters:random_state (numpy.random.Generator or numpy.random.RandomState) – Generator of which to advance the internal state.
imgaug.imgaug.imresize_many_images(images, sizes=None, interpolation=None)[source]

Resize each image in a list or array to a specified size.

dtype support:

* ``uint8``: yes; fully tested
* ``uint16``: yes; tested
* ``uint32``: no (1)
* ``uint64``: no (2)
* ``int8``: yes; tested (3)
* ``int16``: yes; tested
* ``int32``: limited; tested (4)
* ``int64``: no (2)
* ``float16``: yes; tested (5)
* ``float32``: yes; tested
* ``float64``: yes; tested
* ``float128``: no (1)
* ``bool``: yes; tested (6)

- (1) rejected by ``cv2.imresize``
- (2) results too inaccurate
- (3) mapped internally to ``int16`` when interpolation!="nearest"
- (4) only supported for interpolation="nearest", other interpolations
      lead to cv2 error
- (5) mapped internally to ``float32``
- (6) mapped internally to ``uint8``
Parameters:
  • images ((N,H,W,[C]) ndarray or list of (H,W,[C]) ndarray) – Array of the images to resize. Usually recommended to be of dtype uint8.

  • sizes (float or iterable of int or iterable of float) – The new size of the images, given either as a fraction (a single float) or as a (height, width) tuple of two integers or as a (height fraction, width fraction) tuple of two floats.

  • interpolation (None or str or int, optional) – The interpolation to use during resize. If int, then expected to be one of:

    • cv2.INTER_NEAREST (nearest neighbour interpolation)
    • cv2.INTER_LINEAR (linear interpolation)
    • cv2.INTER_AREA (area interpolation)
    • cv2.INTER_CUBIC (cubic interpolation)

    If str, then expected to be one of:

    • nearest (identical to cv2.INTER_NEAREST)
    • linear (identical to cv2.INTER_LINEAR)
    • area (identical to cv2.INTER_AREA)
    • cubic (identical to cv2.INTER_CUBIC)

    If None, the interpolation will be chosen automatically. For size increases, area interpolation will be picked and for size decreases, linear interpolation will be picked.

Returns:

Array of the resized images.

Return type:

(N,H’,W’,[C]) ndarray

Examples

>>> import imgaug as ia
>>> images = np.zeros((2, 8, 16, 3), dtype=np.uint8)
>>> images_resized = ia.imresize_many_images(images, 2.0)
>>> images_resized.shape
(2, 16, 32, 3)

Convert two RGB images of height 8 and width 16 to images of height 2*8=16 and width 2*16=32.

>>> images_resized = ia.imresize_many_images(images, (2.0, 4.0))
>>> images_resized.shape
(2, 16, 64, 3)

Convert two RGB images of height 8 and width 16 to images of height 2*8=16 and width 4*16=64.

>>> images_resized = ia.imresize_many_images(images, (16, 32))
>>> images_resized.shape
(2, 16, 32, 3)

Converts two RGB images of height 8 and width 16 to images of height 16 and width 32.

imgaug.imgaug.imresize_single_image(image, sizes, interpolation=None)[source]

Resize a single image.

dtype support:

See :func:`imgaug.imgaug.imresize_many_images`.
Parameters:
Returns:

The resized image.

Return type:

(H’,W’,C) ndarray or (H’,W’) ndarray

imgaug.imgaug.imshow(image, backend='matplotlib')[source]

Show an image in a window.

dtype support:

* ``uint8``: yes; not tested
* ``uint16``: ?
* ``uint32``: ?
* ``uint64``: ?
* ``int8``: ?
* ``int16``: ?
* ``int32``: ?
* ``int64``: ?
* ``float16``: ?
* ``float32``: ?
* ``float64``: ?
* ``float128``: ?
* ``bool``: ?
Parameters:
  • image ((H,W,3) ndarray) – Image to show.
  • backend ({‘matplotlib’, ‘cv2’}, optional) – Library to use to show the image. May be either matplotlib or OpenCV (‘cv2’). OpenCV tends to be faster, but apparently causes more technical issues.
imgaug.imgaug.is_callable(val)[source]

Check whether a variable is a callable, e.g. a function.

Parameters:val – The variable to check.
Returns:True if the variable is a callable. Otherwise False.
Return type:bool
imgaug.imgaug.is_float_array(val)[source]

Check whether a variable is a numpy float array.

Parameters:val – The variable to check.
Returns:True if the variable is a numpy float array. Otherwise False.
Return type:bool
imgaug.imgaug.is_generator(val)[source]

Check whether a variable is a generator.

Parameters:val – The variable to check.
Returns:True is the variable is a generator. Otherwise False.
Return type:bool
imgaug.imgaug.is_integer_array(val)[source]

Check whether a variable is a numpy integer array.

Parameters:val – The variable to check.
Returns:True if the variable is a numpy integer array. Otherwise False.
Return type:bool
imgaug.imgaug.is_iterable(val)[source]

Checks whether a variable is iterable.

Parameters:val – The variable to check.
Returns:True if the variable is an iterable. Otherwise False.
Return type:bool
imgaug.imgaug.is_np_array(val)[source]

Check whether a variable is a numpy array.

Parameters:val – The variable to check.
Returns:True if the variable is a numpy array. Otherwise False.
Return type:bool
imgaug.imgaug.is_np_scalar(val)[source]

Check whether a variable is a numpy scalar.

Parameters:val – The variable to check.
Returns:True if the variable is a numpy scalar. Otherwise False.
Return type:bool
imgaug.imgaug.is_single_bool(val)[source]

Check whether a variable is a bool.

Parameters:val – The variable to check.
Returns:True if the variable is a bool. Otherwise False.
Return type:bool
imgaug.imgaug.is_single_float(val)[source]

Check whether a variable is a float.

Parameters:val – The variable to check.
Returns:True if the variable is a float. Otherwise False.
Return type:bool
imgaug.imgaug.is_single_integer(val)[source]

Check whether a variable is an int.

Parameters:val – The variable to check.
Returns:True if the variable is an int. Otherwise False.
Return type:bool
imgaug.imgaug.is_single_number(val)[source]

Check whether a variable is a number, i.e. an int or float.

Parameters:val – The variable to check.
Returns:True if the variable is a number. Otherwise False.
Return type:bool
imgaug.imgaug.is_string(val)[source]

Check whether a variable is a string.

Parameters:val – The variable to check.
Returns:True if the variable is a string. Otherwise False.
Return type:bool
imgaug.imgaug.max_pool(arr, block_size, pad_mode='edge', pad_cval=0, preserve_dtype=True, cval=None)[source]

Resize an array using max-pooling.

Defaults to pad_mode="edge" to ensure that padded values do not affect the maximum, even if the dtype was something else than uint8.

dtype support:

See :func:`imgaug.imgaug.pool`.
Parameters:
  • arr ((H,W) ndarray or (H,W,C) ndarray) – Image-like array to pool. See imgaug.imgaug.pool() for details.
  • block_size (int or tuple of int or tuple of int) – Size of each block of values to pool. See imgaug.imgaug.pool() for details.
  • pad_mode (str, optional) – Padding mode to use if the array cannot be divided by block_size without remainder. See imgaug.imgaug.pad() for details.
  • pad_cval (number, optional) – Padding value. See imgaug.imgaug.pool() for details.
  • preserve_dtype (bool, optional) – Whether to preserve the input array dtype. See imgaug.imgaug.pool() for details.
  • cval (None or number, optional) – Deprecated. Old name for pad_cval.
Returns:

Array after max-pooling.

Return type:

(H’,W’) ndarray or (H’,W’,C’) ndarray

imgaug.imgaug.median_pool(arr, block_size, pad_mode='reflect', pad_cval=128, preserve_dtype=True)[source]

Resize an array using median-pooling.

Defaults to pad_mode="reflect" to ensure that padded values do not affect the average.

dtype support:

See :func:`imgaug.imgaug.pool`.
Parameters:
  • arr ((H,W) ndarray or (H,W,C) ndarray) – Image-like array to pool. See imgaug.imgaug.pool() for details.
  • block_size (int or tuple of int or tuple of int) – Size of each block of values to pool. See imgaug.imgaug.pool() for details.
  • pad_mode (str, optional) – Padding mode to use if the array cannot be divided by block_size without remainder. See imgaug.imgaug.pad() for details.
  • pad_cval (number, optional) – Padding value. See imgaug.imgaug.pool() for details.
  • preserve_dtype (bool, optional) – Whether to preserve the input array dtype. See imgaug.imgaug.pool() for details.
Returns:

Array after min-pooling.

Return type:

(H’,W’) ndarray or (H’,W’,C’) ndarray

imgaug.imgaug.min_pool(arr, block_size, pad_mode='edge', pad_cval=255, preserve_dtype=True)[source]

Resize an array using min-pooling.

Defaults to pad_mode="edge" to ensure that padded values do not affect the minimum, even if the dtype was something else than uint8.

dtype support:

See :func:`imgaug.imgaug.pool`.
Parameters:
  • arr ((H,W) ndarray or (H,W,C) ndarray) – Image-like array to pool. See imgaug.imgaug.pool() for details.
  • block_size (int or tuple of int or tuple of int) – Size of each block of values to pool. See imgaug.imgaug.pool() for details.
  • pad_mode (str, optional) – Padding mode to use if the array cannot be divided by block_size without remainder. See imgaug.imgaug.pad() for details.
  • pad_cval (number, optional) – Padding value. See imgaug.imgaug.pool() for details.
  • preserve_dtype (bool, optional) – Whether to preserve the input array dtype. See imgaug.imgaug.pool() for details.
Returns:

Array after min-pooling.

Return type:

(H’,W’) ndarray or (H’,W’,C’) ndarray

imgaug.imgaug.new_random_state(seed=None, fully_random=False)[source]

Deprecated. Use imgaug.random.convert_seed_to_rng instead.

Create a new numpy random number generator.

Parameters:
  • seed (None or int, optional) – The seed value to use. If None and fully_random is False, the seed will be derived from the global RNG. If fully_random is True, the seed will be provided by the OS.
  • fully_random (bool, optional) – Whether the seed will be provided by the OS.
Returns:

In numpy <=1.16 a RandomState, in 1.17+ a Generator. Both are initialized with the provided seed.

Return type:

numpy.random.Generator or numpy.random.RandomState

imgaug.imgaug.normalize_random_state(random_state)[source]

Deprecated. Use imgaug.random.normalize_generator instead.

Normalize various inputs to a numpy random generator.

Parameters:random_state (None or int or numpy.random.Generator or numpy.random.bit_generator.BitGenerator or numpy.random.bit_generator.SeedSequence or numpy.random.RandomState) – See imgaug.random.normalize_generator().
Returns:In numpy <=1.16 a RandomState, in 1.17+ a Generator (even if the input was a RandomState).
Return type:numpy.random.Generator or numpy.random.RandomState
imgaug.imgaug.pad(arr, top=0, right=0, bottom=0, left=0, mode='constant', cval=0)[source]

Pad an image-like array on its top/right/bottom/left side.

This function is a wrapper around numpy.pad().

dtype support:

* ``uint8``: yes; fully tested (1)
* ``uint16``: yes; fully tested (1)
* ``uint32``: yes; fully tested (2) (3)
* ``uint64``: yes; fully tested (2) (3)
* ``int8``: yes; fully tested (1)
* ``int16``: yes; fully tested (1)
* ``int32``: yes; fully tested (1)
* ``int64``: yes; fully tested (2) (3)
* ``float16``: yes; fully tested (2) (3)
* ``float32``: yes; fully tested (1)
* ``float64``: yes; fully tested (1)
* ``float128``: yes; fully tested (2) (3)
* ``bool``: yes; tested (2) (3)

- (1) Uses ``cv2`` if `mode` is one of: ``"constant"``, ``"edge"``,
      ``"reflect"``, ``"symmetric"``. Otherwise uses ``numpy``.
- (2) Uses ``numpy``.
- (3) Rejected by ``cv2``.
Parameters:
  • arr ((H,W) ndarray or (H,W,C) ndarray) – Image-like array to pad.
  • top (int, optional) – Amount of pixels to add to the top side of the image. Must be 0 or greater.
  • right (int, optional) – Amount of pixels to add to the right side of the image. Must be 0 or greater.
  • bottom (int, optional) – Amount of pixels to add to the bottom side of the image. Must be 0 or greater.
  • left (int, optional) – Amount of pixels to add to the left side of the image. Must be 0 or greater.
  • mode (str, optional) – Padding mode to use. See numpy.pad() for details. In case of mode constant, the parameter cval will be used as the constant_values parameter to numpy.pad(). In case of mode linear_ramp, the parameter cval will be used as the end_values parameter to numpy.pad().
  • cval (number, optional) – Value to use for padding if mode is constant. See numpy.pad() for details. The cval is expected to match the input array’s dtype and value range.
Returns:

Padded array with height H'=H+top+bottom and width W'=W+left+right.

Return type:

(H’,W’) ndarray or (H’,W’,C) ndarray

imgaug.imgaug.pad_to_aspect_ratio(arr, aspect_ratio, mode='constant', cval=0, return_pad_amounts=False)[source]

Pad an image array on its sides so that it matches a target aspect ratio.

See imgaug.imgaug.compute_paddings_for_aspect_ratio() for an explanation of how the required padding amounts are distributed per image axis.

dtype support:

See :func:`imgaug.imgaug.pad`.
Parameters:
  • arr ((H,W) ndarray or (H,W,C) ndarray) – Image-like array to pad.
  • aspect_ratio (float) – Target aspect ratio, given as width/height. E.g. 2.0 denotes the image having twice as much width as height.
  • mode (str, optional) – Padding mode to use. See imgaug.imgaug.pad() for details.
  • cval (number, optional) – Value to use for padding if mode is constant. See numpy.pad() for details.
  • return_pad_amounts (bool, optional) – If False, then only the padded image will be returned. If True, a tuple with two entries will be returned, where the first entry is the padded image and the second entry are the amounts by which each image side was padded. These amounts are again a tuple of the form (top, right, bottom, left), with each value being an int.
Returns:

  • (H’,W’) ndarray or (H’,W’,C) ndarray – Padded image as (H',W') or (H',W',C) ndarray, fulfilling the given aspect_ratio.
  • tuple of int – Amounts by which the image was padded on each side, given as a tuple (top, right, bottom, left). This tuple is only returned if return_pad_amounts was set to True.

imgaug.imgaug.pad_to_multiples_of(arr, height_multiple, width_multiple, mode='constant', cval=0, return_pad_amounts=False)[source]

Pad an image array until its side lengths are multiples of given values.

See imgaug.imgaug.compute_paddings_for_aspect_ratio() for an explanation of how the required padding amounts are distributed per image axis.

dtype support:

See :func:`imgaug.imgaug.pad`.
Parameters:
  • arr ((H,W) ndarray or (H,W,C) ndarray) – Image-like array to pad.
  • height_multiple (None or int) – The desired multiple of the height. The computed padding amount will reflect a padding that increases the y axis size until it is a multiple of this value.
  • width_multiple (None or int) – The desired multiple of the width. The computed padding amount will reflect a padding that increases the x axis size until it is a multiple of this value.
  • mode (str, optional) – Padding mode to use. See imgaug.imgaug.pad() for details.
  • cval (number, optional) – Value to use for padding if mode is constant. See numpy.pad() for details.
  • return_pad_amounts (bool, optional) – If False, then only the padded image will be returned. If True, a tuple with two entries will be returned, where the first entry is the padded image and the second entry are the amounts by which each image side was padded. These amounts are again a tuple of the form (top, right, bottom, left), with each value being an integer.
Returns:

  • (H’,W’) ndarray or (H’,W’,C) ndarray – Padded image as (H',W') or (H',W',C) ndarray.
  • tuple of int – Amounts by which the image was padded on each side, given as a tuple (top, right, bottom, left). This tuple is only returned if return_pad_amounts was set to True.

imgaug.imgaug.pool(arr, block_size, func, pad_mode='constant', pad_cval=0, preserve_dtype=True, cval=None)[source]

Resize an array by pooling values within blocks.

dtype support:

* ``uint8``: yes; fully tested
* ``uint16``: yes; tested
* ``uint32``: yes; tested (2)
* ``uint64``: no (1)
* ``int8``: yes; tested
* ``int16``: yes; tested
* ``int32``: yes; tested (2)
* ``int64``: no (1)
* ``float16``: yes; tested
* ``float32``: yes; tested
* ``float64``: yes; tested
* ``float128``: yes; tested (2)
* ``bool``: yes; tested

- (1) results too inaccurate (at least when using np.average as func)
- (2) Note that scikit-image documentation says that the wrapped
      pooling function converts inputs to ``float64``. Actual tests
      showed no indication of that happening (at least when using
      preserve_dtype=True).
Parameters:
  • arr ((H,W) ndarray or (H,W,C) ndarray) – Image-like array to pool. Ideally of datatype float64.

  • block_size (int or tuple of int) –

    Spatial size of each group of values to pool, aka kernel size.

    • If a single int, then a symmetric block of that size along height and width will be used.
    • If a tuple of two values, it is assumed to be the block size along height and width of the image-like, with pooling happening per channel.
    • If a tuple of three values, it is assumed to be the block size along height, width and channels.
  • func (callable) – Function to apply to a given block in order to convert it to a single number, e.g. numpy.average(), numpy.min(), numpy.max().

  • pad_mode (str, optional) – Padding mode to use if the array cannot be divided by block_size without remainder. See imgaug.imgaug.pad() for details.

  • pad_cval (number, optional) – Value to use for padding if mode is constant. See numpy.pad() for details.

  • preserve_dtype (bool, optional) – Whether to convert the array back to the input datatype if it is changed away from that in the pooling process.

  • cval (None or number, optional) – Deprecated. Old name for pad_cval.

Returns:

Array after pooling.

Return type:

(H’,W’) ndarray or (H’,W’,C’) ndarray

imgaug.imgaug.quokka(size=None, extract=None)[source]

Return an image of a quokka as a numpy array.

Parameters:
  • size (None or float or tuple of int, optional) – Size of the output image. Input into imgaug.imgaug.imresize_single_image(). Usually expected to be a tuple (H, W), where H is the desired height and W is the width. If None, then the image will not be resized.

  • extract (None or ‘square’ or tuple of number or imgaug.augmentables.bbs.BoundingBox or imgaug.augmentables.bbs.BoundingBoxesOnImage) –

    Subarea of the quokka image to extract:

    • If None, then the whole image will be used.
    • If str square, then a squared area (x: 0 to max 643, y: 0 to max 643) will be extracted from the image.
    • If a tuple, then expected to contain four number s denoting (x1, y1, x2, y2).
    • If a imgaug.augmentables.bbs.BoundingBox, then that bounding box’s area will be extracted from the image.
    • If a imgaug.augmentables.bbs.BoundingBoxesOnImage, then expected to contain exactly one bounding box and a shape matching the full image dimensions (i.e. (643, 960, *)). Then the one bounding box will be used similar to BoundingBox above.
Returns:

The image array of dtype uint8.

Return type:

(H,W,3) ndarray

imgaug.imgaug.quokka_bounding_boxes(size=None, extract=None)[source]

Return example bounding boxes on the standard example quokke image.

Currently only a single bounding box is returned that covers the quokka.

Parameters:
  • size (None or float or tuple of int or tuple of float, optional) – Size of the output image on which the BBs are placed. If None, then the BBs are not projected to any new size (positions on the original image are used). float s lead to relative size changes, int s to absolute sizes in pixels.
  • extract (None or ‘square’ or tuple of number or imgaug.augmentables.bbs.BoundingBox or imgaug.augmentables.bbs.BoundingBoxesOnImage) – Subarea to extract from the image. See imgaug.imgaug.quokka().
Returns:

Example BBs on the quokka image.

Return type:

imgaug.augmentables.bbs.BoundingBoxesOnImage

imgaug.imgaug.quokka_heatmap(size=None, extract=None)[source]

Return a heatmap (here: depth map) for the standard example quokka image.

Parameters:
  • size (None or float or tuple of int, optional) – See imgaug.imgaug.quokka().
  • extract (None or ‘square’ or tuple of number or imgaug.augmentables.bbs.BoundingBox or imgaug.augmentables.bbs.BoundingBoxesOnImage) – See imgaug.imgaug.quokka().
Returns:

Depth map as an heatmap object. Values close to 0.0 denote objects that are close to the camera. Values close to 1.0 denote objects that are furthest away (among all shown objects).

Return type:

imgaug.augmentables.heatmaps.HeatmapsOnImage

imgaug.imgaug.quokka_keypoints(size=None, extract=None)[source]

Return example keypoints on the standard example quokke image.

The keypoints cover the eyes, ears, nose and paws.

Parameters:
  • size (None or float or tuple of int or tuple of float, optional) – Size of the output image on which the keypoints are placed. If None, then the keypoints are not projected to any new size (positions on the original image are used). float s lead to relative size changes, int s to absolute sizes in pixels.
  • extract (None or ‘square’ or tuple of number or imgaug.augmentables.bbs.BoundingBox or imgaug.augmentables.bbs.BoundingBoxesOnImage) – Subarea to extract from the image. See imgaug.imgaug.quokka().
Returns:

Example keypoints on the quokka image.

Return type:

imgaug.augmentables.kps.KeypointsOnImage

imgaug.imgaug.quokka_polygons(size=None, extract=None)[source]

Returns example polygons on the standard example quokke image.

The result contains one polygon, covering the quokka’s outline.

Parameters:
  • size (None or float or tuple of int or tuple of float, optional) – Size of the output image on which the polygons are placed. If None, then the polygons are not projected to any new size (positions on the original image are used). float s lead to relative size changes, int s to absolute sizes in pixels.
  • extract (None or ‘square’ or tuple of number or imgaug.augmentables.bbs.BoundingBox or imgaug.augmentables.bbs.BoundingBoxesOnImage) – Subarea to extract from the image. See imgaug.imgaug.quokka().
Returns:

Example polygons on the quokka image.

Return type:

imgaug.augmentables.polys.PolygonsOnImage

imgaug.imgaug.quokka_segmentation_map(size=None, extract=None)[source]

Return a segmentation map for the standard example quokka image.

Parameters:
  • size (None or float or tuple of int, optional) – See imgaug.imgaug.quokka().
  • extract (None or ‘square’ or tuple of number or imgaug.augmentables.bbs.BoundingBox or imgaug.augmentables.bbs.BoundingBoxesOnImage) – See imgaug.imgaug.quokka().
Returns:

Segmentation map object.

Return type:

imgaug.augmentables.segmaps.SegmentationMapsOnImage

imgaug.imgaug.quokka_square(size=None)[source]

Return an (square) image of a quokka as a numpy array.

Parameters:size (None or float or tuple of int, optional) – Size of the output image. Input into imgaug.imgaug.imresize_single_image(). Usually expected to be a tuple (H, W), where H is the desired height and W is the width. If None, then the image will not be resized.
Returns:The image array of dtype uint8.
Return type:(H,W,3) ndarray
imgaug.imgaug.seed(entropy=None, seedval=None)[source]

Set the seed of imgaug’s global RNG.

The global RNG controls most of the “randomness” in imgaug.

The global RNG is the default one used by all augmenters. Under special circumstances (e.g. when an augmenter is switched to deterministic mode), the global RNG is replaced with a local one. The state of that replacement may be dependent on the global RNG’s state at the time of creating the child RNG.

Note

This function is not yet marked as deprecated, but might be in the future. The preferred way to seed imgaug is via imgaug.random.seed().

Parameters:
  • entropy (int) – The seed value to use.
  • seedval (None or int, optional) – Deprecated.
imgaug.imgaug.show_grid(images, rows=None, cols=None)[source]

Combine multiple images into a single image and plot the result.

This will show a window of the results of imgaug.imgaug.draw_grid().

dtype support:

minimum of (
    :func:`imgaug.imgaug.draw_grid`,
    :func:`imgaug.imgaug.imshow`
)
Parameters:
imgaug.imgaug.warn(msg, category=<class 'UserWarning'>, stacklevel=2)[source]

Generate a a warning with stacktrace.

Parameters:
  • msg (str) – The message of the warning.
  • category (class) – The class of the warning to produce.
  • stacklevel (int, optional) – How many steps above this function to “jump” in the stacktrace when displaying file and line number of the error message. Usually 2.
imgaug.imgaug.warn_deprecated(msg, stacklevel=2)[source]

Generate a non-silent deprecation warning with stacktrace.

The used warning is imgaug.imgaug.DeprecationWarning.

Parameters:
  • msg (str) – The message of the warning.
  • stacklevel (int, optional) – How many steps above this function to “jump” in the stacktrace when displaying file and line number of the error message. Usually 2