dippykit.adjustments module

Module of image adjustment functions

This module contains an assortment of functions for use in adjusting the specifics of images. This includes noise, domain expansion/contraction, shifts, etc.

dippykit.adjustments.image_noise(im: numpy.ndarray, mode: str = 'gaussian', **kwargs) → numpy.ndarray[source]

Adds random noise to an image

Adds random noise to an input image. For images with an integer dtype, the returned image will retain the original image’s dtype. In all other cases, the returned image will have a float dtype and be within the normalized range from 0 to 1.

This function is essentially a wrapper for skimage.util.random_noise, so more detailed documentation may be found there.

Parameters:
  • im (numpy.ndarray) – The original image.
  • mode (str) – (default=’gaussian’) The type of noise to add to the image. May be one of the following: ‘gaussian’, ‘localvar’, ‘poisson’, ‘salt’, ‘pepper’, ‘s&p’, ‘speckle’. Passed as the mode argument to skimage.util.random_noise.
  • kwargs – See below.
Return type:

numpy.ndarray

Returns:

The noisy image.

Keyword Arguments:
 
  • mean (int or float) – The mean of the randomly distributed noise. This value’s meaning is dependent on the dtype of the image (e.g. mean=127 for an image with dtype=uint8 is analogous to mean=(127 / 255) for an image with dtype=float).
  • var (int or float) – The variance of the randomly distributed noise. This value’s meaning is dependent on the dtype of the image (e.g. var=(0.01 * ( 255 ** 2)) for an image with dtype=uint8 is analogous to var=0.01 for an image with dtype=float).
  • local_vars (numpy.ndarray) – The array of local variances for the randomly distributed noise. This value’s meaning is dependent on the dtype of the image. Each element in the array is treated in the same way the var keyword argument would be.
  • A full list of keyword arguments along with descriptions can be found at skimage.util.random_noise.

Note

This function wraps around functions from other packages. Reading these functions’ documentations may be useful. See the See also section for more information.

See also

skimage.util.random_noise
Documentation of the random_noise function from Scikit Image

Examples:

>>> import numpy as np
>>> im = np.random.randint(0, 256, size=(8, 8), dtype=np.uint8)
>>> im
array([[238, 208, 243, 228, 198, 186,  72, 181],
       [ 92, 247,  98, 197, 203, 210, 219, 175],
       [124,  66, 231, 193, 154, 153, 136,  59],
       [218, 162,  65, 196, 181, 160,  13, 148],
       [164,  26, 150, 202,  55, 138,  40, 116],
       [ 46, 151,  97,  78, 169, 249, 181, 167],
       [ 79,  72,  31,  98, 152,  80, 220, 152],
       [ 19, 233, 111, 126, 176,  68,  40,  49]], dtype=uint8)
>>> image_noise(im)
array([[246, 220, 211, 216, 216, 157,  51, 176],
       [ 79, 230,  68, 217, 211, 175, 244, 180],
       [ 86,  75, 184, 178, 123, 183, 106,  61],
       [201, 154,  73, 193, 163, 180,  33, 180],
       [192,  19, 144, 197,  64,  89,  36,  99],
       [ 58, 182,  81,  53, 154, 255, 193, 145],
       [ 60,  62,  55, 122, 132,  97, 244, 167],
       [ 33, 181, 132, 163, 190,   9,  41,  45]], dtype=uint8)
dippykit.adjustments.image_adjust(im: numpy.ndarray, lower_in: typing.Union[int, float] = None, upper_in: typing.Union[int, float] = None, lower_out: typing.Union[int, float] = None, upper_out: typing.Union[int, float] = None, keep_dtype: bool = True) → numpy.ndarray[source]

Adjusts a given image by clipping and scaling its range

Given an image and parameters, this function clips the values in the image to within a specific range and subsequently scales the image to a new range.

Parameters:
  • im (numpy.ndarray) – The original image.
  • lower_in (NumericType) – The lower clipping bound for the input image. All values less than this will be set to this in the image before scaling. By default, this value will be set to 1st percentile of the image.
  • upper_in (NumericType) – The upper clipping bound for the input image. All values greater than this will be set to this in the image before scaling. By default, this value will be set to 99th percentile of the image.
  • lower_out (NumericType) – The lower bound for the range of the scaled output image. By default, this value is the minimum value of the dtype for integer dtypes or 0.0 for float dtypes.
  • upper_out (NumericType) – The upper bound for the range of the scaled output image. By default, this value is the maximum value of the dtype for integer dtypes or 1.0 for float dtypes.
  • keep_dtype (bool) – (default=True) Whether or not to cast the output image back to the dtype of the input image
Return type:

numpy.ndarray

Returns:

The adjusted image.

Examples:

>>> import numpy as np
>>> im = np.array([[  0,  32,  64],
...                [ 96, 128, 160],
...                [192, 224, 255]], dtype=np.uint8)
>>> image_adjust(im, 64, 192)
array([[  0,   0,   0],
       [ 63, 127, 191],
       [255, 255, 255]], dtype=uint8)
>>> image_adjust(im, 64, 192, 64, 192)
array([[ 64,  64,  64],
       [ 96, 128, 160],
       [192, 192, 192]], dtype=uint8)
dippykit.adjustments.image_translate(im: numpy.ndarray, dist_vec: typing.Union[typing.Tuple[typing.Union[int, float], typing.Union[int, float]], int, float], pad_value: typing.Union[int, float] = 0) → numpy.ndarray[source]

Translates an image

Given an image and a two-dimensional vector of distances to translate, this function translates the original image. Values left in the wake of the translation are padded with pad_value, which is by default set to 0.

Parameters:
  • im (numpy.ndarray) – The original image.
  • dist_vec (ShapeType) – A vector of distances to translate.
  • pad_value (NumericType) – The value to pad for elements left in the wake of the translation.
Return type:

numpy.ndarray

Returns:

The translated image.

Examples:

>>> import numpy as np
>>> im = np.array(array([[ 1,  2,  3,  4,  5],
...                      [ 6,  7,  8,  9, 10],
...                      [11, 12, 13, 14, 15],
...                      [16, 17, 18, 19, 20],
...                      [21, 22, 23, 24, 25]])
>>> image_translate(im, (0, 2))
array([[ 0,  0,  1,  2,  3],
       [ 0,  0,  6,  7,  8],
       [ 0,  0, 11, 12, 13],
       [ 0,  0, 16, 17, 18],
       [ 0,  0, 21, 22, 23]])
>>> image_translate(im, (-3, 2), pad_value=-1)
array([[-1, -1, 16, 17, 18],
       [-1, -1, 21, 22, 23],
       [-1, -1, -1, -1, -1],
       [-1, -1, -1, -1, -1],
       [-1, -1, -1, -1, -1]])
dippykit.adjustments.image_shift(im: numpy.ndarray, dist_vec: typing.Union[typing.Tuple[typing.Union[int, float], typing.Union[int, float]], int, float]) → numpy.ndarray[source]

Shifts an image by consecutively applying and summing translations

Given an image and a two-dimensional vector of distances to shift, this function shifts the image by weighting and summing many intermediate translations. This simulates an image taken with a shaky camera.

Parameters:
  • im (numpy.ndarray) – The original image.
  • dist_vec (ShapeType) – A vector of distances to shift.
Return type:

numpy.ndarray

Returns:

The translated image.

Examples:

>>> import numpy as np
>>> im = np.array(array([[  1,   2,   4],
...                      [  8,  16,  32],
...                      [ 64, 128, 256]])
>>> image_translate(im, (0, 1))
array([[  1,   1,   3],
       [  8,  12,  24],
       [ 64,  96, 192]])
>>> image_translate(im, (-1, 1))
array([[  1,   5,  10],
       [  8,  40,  80],
       [ 64, 128, 256]])