Skip to content

Functional transforms (augmentations.functional)

add_fog(img, fog_coef, alpha_coef, haze_list)

Add fog to the image.

From https://github.com/UjjwalSaxena/Automold--Road-Augmentation-Library

Parameters:

Name Type Description Default
img numpy.ndarray

Image.

required
fog_coef float

Fog coefficient.

required
alpha_coef float

Alpha coefficient.

required
haze_list list required

Returns:

Type Description
numpy.ndarray

Image.

add_rain(img, slant, drop_length, drop_width, drop_color, blur_value, brightness_coefficient, rain_drops)

From https://github.com/UjjwalSaxena/Automold--Road-Augmentation-Library

Parameters:

Name Type Description Default
img numpy.ndarray

Image.

required
slant int required
drop_length required
drop_width required
drop_color required
blur_value int

Rainy view are blurry.

required
brightness_coefficient float

Rainy days are usually shady.

required
rain_drops required

Returns:

Type Description
numpy.ndarray

Image.

add_shadow(img, vertices_list)

Add shadows to the image.

From https://github.com/UjjwalSaxena/Automold--Road-Augmentation-Library

Parameters:

Name Type Description Default
img numpy.ndarray required
vertices_list list required

Returns:

Type Description
numpy.ndarray

add_snow(img, snow_point, brightness_coeff)

Bleaches out pixels, imitation snow.

From https://github.com/UjjwalSaxena/Automold--Road-Augmentation-Library

Parameters:

Name Type Description Default
img numpy.ndarray

Image.

required
snow_point

Number of show points.

required
brightness_coeff

Brightness coefficient.

required

Returns:

Type Description
numpy.ndarray

Image.

add_sun_flare(img, flare_center_x, flare_center_y, src_radius, src_color, circles)

Add sun flare.

From https://github.com/UjjwalSaxena/Automold--Road-Augmentation-Library

Parameters:

Name Type Description Default
img numpy.ndarray required
flare_center_x float required
flare_center_y float required
src_radius required
src_color int, int, int required
circles list required

Returns:

Type Description
numpy.ndarray

bbox_crop(bbox, x_min, y_min, x_max, y_max, rows, cols)

Crop a bounding box.

Parameters:

Name Type Description Default
bbox tuple

A bounding box (x_min, y_min, x_max, y_max).

required
x_min int required
y_min int required
x_max int required
y_max int required
rows int

Image rows.

required
cols int

Image cols.

required

Returns:

Type Description
tuple

A cropped bounding box (x_min, y_min, x_max, y_max).

bbox_flip(bbox, d, rows, cols)

Flip a bounding box either vertically, horizontally or both depending on the value of d.

Parameters:

Name Type Description Default
bbox tuple

A bounding box (x_min, y_min, x_max, y_max).

required
d int required
rows int

Image rows.

required
cols int

Image cols.

required

Returns:

Type Description
tuple

A bounding box (x_min, y_min, x_max, y_max).

Exceptions:

Type Description
ValueError

if value of d is not -1, 0 or 1.

bbox_hflip(bbox, rows, cols)

Flip a bounding box horizontally around the y-axis.

Parameters:

Name Type Description Default
bbox tuple

A bounding box (x_min, y_min, x_max, y_max).

required
rows int

Image rows.

required
cols int

Image cols.

required

Returns:

Type Description
tuple

A bounding box (x_min, y_min, x_max, y_max).

bbox_rot90(bbox, factor, rows, cols)

Rotates a bounding box by 90 degrees CCW (see np.rot90)

Parameters:

Name Type Description Default
bbox tuple

A bounding box tuple (x_min, y_min, x_max, y_max).

required
factor int

Number of CCW rotations. Must be in set {0, 1, 2, 3} See np.rot90.

required
rows int

Image rows.

required
cols int

Image cols.

required

Returns:

Type Description
tuple

A bounding box tuple (x_min, y_min, x_max, y_max).

bbox_rotate(bbox, angle, rows, cols)

Rotates a bounding box by angle degrees.

Parameters:

Name Type Description Default
bbox tuple

A bounding box (x_min, y_min, x_max, y_max).

required
angle int

Angle of rotation in degrees.

required
rows int

Image rows.

required
cols int

Image cols.

required

Returns:

Type Description

A bounding box (x_min, y_min, x_max, y_max).

bbox_transpose(bbox, axis, rows, cols)

Transposes a bounding box along given axis.

Parameters:

Name Type Description Default
bbox tuple

A bounding box (x_min, y_min, x_max, y_max).

required
axis int

0 - main axis, 1 - secondary axis.

required
rows int

Image rows.

required
cols int

Image cols.

required

Returns:

Type Description
tuple

A bounding box tuple (x_min, y_min, x_max, y_max).

Exceptions:

Type Description
ValueError

If axis not equal to 0 or 1.

bbox_vflip(bbox, rows, cols)

Flip a bounding box vertically around the x-axis.

Parameters:

Name Type Description Default
bbox tuple

A bounding box (x_min, y_min, x_max, y_max).

required
rows int

Image rows.

required
cols int

Image cols.

required

Returns:

Type Description
tuple

A bounding box (x_min, y_min, x_max, y_max).

crop_bbox_by_coords(bbox, crop_coords, crop_height, crop_width, rows, cols)

Crop a bounding box using the provided coordinates of bottom-left and top-right corners in pixels and the required height and width of the crop.

Parameters:

Name Type Description Default
bbox tuple

A cropped box (x_min, y_min, x_max, y_max).

required
crop_coords tuple

Crop coordinates (x1, y1, x2, y2).

required
crop_height int required
crop_width int required
rows int

Image rows.

required
cols int

Image cols.

required

Returns:

Type Description
tuple

A cropped bounding box (x_min, y_min, x_max, y_max).

crop_keypoint_by_coords(keypoint, crop_coords, crop_height, crop_width, rows, cols)

Crop a keypoint using the provided coordinates of bottom-left and top-right corners in pixels and the required height and width of the crop.

Parameters:

Name Type Description Default
keypoint tuple

A keypoint (x, y, angle, scale).

required
crop_coords tuple

Crop box coords (x1, x2, y1, y2).

required
crop height (int

Crop height.

required
crop_width int

Crop width.

required
rows int

Image height.

required
cols int

Image width.

required

Returns:

Type Description

A keypoint (x, y, angle, scale).

elastic_transform(img, alpha, sigma, alpha_affine, interpolation=1, border_mode=4, value=None, random_state=None, approximate=False)

Elastic deformation of images as described in [Simard2003]_ (with modifications). Based on https://gist.github.com/erniejunior/601cdf56d2b424757de5

.. [Simard2003] 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.

elastic_transform_approx(img, alpha, sigma, alpha_affine, interpolation=1, border_mode=4, value=None, random_state=None)

Elastic deformation of images as described in [Simard2003]_ (with modifications for speed). Based on https://gist.github.com/erniejunior/601cdf56d2b424757de5

.. [Simard2003] 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.

equalize(img, mask=None, mode='cv', by_channels=True)

Equalize the image histogram.

Parameters:

Name Type Description Default
img numpy.ndarray

RGB or grayscale image.

required
mask numpy.ndarray

An optional mask. If given, only the pixels selected by the mask are included in the analysis. Maybe 1 channel or 3 channel array.

None
mode str

{'cv', 'pil'}. Use OpenCV or Pillow equalization method.

'cv'
by_channels bool

If True, use equalization by channels separately, else convert image to YCbCr representation and use equalization by Y channel.

True

Returns:

Type Description
numpy.ndarray

Equalized image.

fancy_pca(img, alpha=0.1)

Perform 'Fancy PCA' augmentation from: http://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf

Parameters:

Name Type Description Default
img

numpy array with (h, w, rgb) shape, as ints between 0-255)

required
alpha

how much to perturb/scale the eigen vecs and vals the paper used std=0.1

0.1

Returns:

Type Description

numpy image-like array as float range(0, 1)

grid_distortion(img, num_steps=10, xsteps=(), ysteps=(), interpolation=1, border_mode=4, value=None)

Reference

http://pythology.blogspot.sg/2014/03/interpolation-on-regular-distorted-grid.html

iso_noise(image, color_shift=0.05, intensity=0.5, random_state=None, **kwargs)

Apply poisson noise to image to simulate camera sensor noise.

Parameters:

Name Type Description Default
image numpy.ndarray

Input image, currently, only RGB, uint8 images are supported.

required
color_shift float 0.05
intensity float

Multiplication factor for noise values. Values of ~0.5 are produce noticeable, yet acceptable level of noise.

0.5
random_state None
**kwargs {}

Returns:

Type Description
numpy.ndarray

Noised image

keypoint_center_crop(keypoint, crop_height, crop_width, rows, cols)

Keypoint center crop.

Parameters:

Name Type Description Default
keypoint tuple

A keypoint (x, y, angle, scale).

required
crop_height int

Crop height.

required
crop_width int

Crop width.

required
h_start int

Crop height start.

required
w_start int

Crop width start.

required
rows int

Image height.

required
cols int

Image width.

required

Returns:

Type Description
tuple

A keypoint (x, y, angle, scale).

keypoint_flip(keypoint, d, rows, cols)

Flip a keypoint either vertically, horizontally or both depending on the value of d.

Parameters:

Name Type Description Default
keypoint tuple

A keypoint (x, y, angle, scale).

required
d int

Number of flip. Must be -1, 0 or 1: * 0 - vertical flip, * 1 - horizontal flip, * -1 - vertical and horizontal flip.

required
rows int

Image height.

required
cols int

Image width.

required

Returns:

Type Description
tuple

A keypoint (x, y, angle, scale).

Exceptions:

Type Description
ValueError

if value of d is not -1, 0 or 1.

keypoint_hflip(keypoint, rows, cols)

Flip a keypoint horizontally around the y-axis.

Parameters:

Name Type Description Default
keypoint tuple

A keypoint (x, y, angle, scale).

required
rows int

Image height.

required
cols int

Image width.

required

Returns:

Type Description
tuple

A keypoint (x, y, angle, scale).

keypoint_random_crop(keypoint, crop_height, crop_width, h_start, w_start, rows, cols)

Keypoint random crop.

Parameters:

Name Type Description Default
keypoint

(tuple): A keypoint (x, y, angle, scale).

required
crop_height int

Crop height.

required
crop_width int

Crop width.

required
h_start int

Crop height start.

required
w_start int

Crop width start.

required
rows int

Image height.

required
cols int

Image width.

required

Returns:

Type Description

A keypoint (x, y, angle, scale).

keypoint_rot90(keypoint, factor, rows, cols, **params)

Rotates a keypoint by 90 degrees CCW (see np.rot90)

Parameters:

Name Type Description Default
keypoint tuple

A keypoint (x, y, angle, scale).

required
factor int

Number of CCW rotations. Must be in range [0;3] See np.rot90.

required
rows int

Image height.

required
cols int

Image width.

required

Returns:

Type Description
tuple

A keypoint (x, y, angle, scale).

Exceptions:

Type Description
ValueError

if factor not in set {0, 1, 2, 3}

keypoint_rotate(keypoint, angle, rows, cols, **params)

Rotate a keypoint by angle.

Parameters:

Name Type Description Default
keypoint tuple

A keypoint (x, y, angle, scale).

required
angle float

Rotation angle.

required
rows int

Image height.

required
cols int

Image width.

required

Returns:

Type Description
tuple

A keypoint (x, y, angle, scale).

keypoint_scale(keypoint, scale_x, scale_y)

Scales a keypoint by scale_x and scale_y.

Parameters:

Name Type Description Default
keypoint tuple

A keypoint (x, y, angle, scale).

required
scale_x int

Scale coefficient x-axis.

required
scale_y int

Scale coefficient y-axis.

required

Returns:

Type Description

A keypoint (x, y, angle, scale).

keypoint_transpose(keypoint)

Rotate a keypoint by angle.

Parameters:

Name Type Description Default
keypoint tuple

A keypoint (x, y, angle, scale).

required

Returns:

Type Description
tuple

A keypoint (x, y, angle, scale).

keypoint_vflip(keypoint, rows, cols)

Flip a keypoint vertically around the x-axis.

Parameters:

Name Type Description Default
keypoint tuple

A keypoint (x, y, angle, scale).

required
rows int

Image height.

required
cols( int

Image width.

required

Returns:

Type Description
tuple

A keypoint (x, y, angle, scale).

multiply(img, multiplier)

Parameters:

Name Type Description Default
img numpy.ndarray

Image.

required
multiplier numpy.ndarray

Multiplier coefficient.

required

Returns:

Type Description
numpy.ndarray

Image multiplied by multiplier coefficient.

optical_distortion(img, k=0, dx=0, dy=0, interpolation=1, border_mode=4, value=None)

Barrel / pincushion distortion. Unconventional augment.

Reference

| https://stackoverflow.com/questions/6199636/formulas-for-barrel-pincushion-distortion | https://stackoverflow.com/questions/10364201/image-transformation-in-opencv | https://stackoverflow.com/questions/2477774/correcting-fisheye-distortion-programmatically | http://www.coldvision.io/2017/03/02/advanced-lane-finding-using-opencv/

posterize(img, bits)

Reduce the number of bits for each color channel.

Parameters:

Name Type Description Default
img numpy.ndarray

image to posterize.

required
bits int

number of high bits. Must be in range [0, 8]

required

Returns:

Type Description
numpy.ndarray

Image with reduced color channels.

preserve_channel_dim(func)

Preserve dummy channel dim.

preserve_shape(func)

Preserve shape of the image

py3round(number)

Unified rounding in all python versions.

solarize(img, threshold=128)

Invert all pixel values above a threshold.

Parameters:

Name Type Description Default
img numpy.ndarray

The image to solarize.

required
threshold int

All pixels above this greyscale level are inverted.

128

Returns:

Type Description
numpy.ndarray

Solarized image.

swap_tiles_on_image(image, tiles)

Swap tiles on image.

Parameters:

Name Type Description Default
image np.ndarray

Input image.

required
tiles np.ndarray

array of tuples( current_left_up_corner_row, current_left_up_corner_col, old_left_up_corner_row, old_left_up_corner_col, height_tile, width_tile)

required

Returns:

Type Description
np.ndarray

Output image.