genalog.degradation

genalog.degradation

Image Degrader

class genalog.degradation.degrader.Degrader(effects)[source]

An object for applying multiple degradation effects onto an image

Parameters

effects (list) –

a list of 2-element tuple (method_name, method_kwargs) where:

method_name

the name of the degradation method (method must be defined in ‘genalog.degradation.effect’)

method_kwargs

the keyword arguments of the corresponding method

Example:

[
    ("blur", {"radius": 3}),
    ("bleed_through", {"alpha": 0.8),
    ("morphology", {"operation": "open", "kernel_shape": (3,3), "kernel_type": "ones"})
]

The example above will apply degradation effects to the images in the following sequence:

"blur" -> "bleed_through" -> "morphological operation (open)"
apply_effects(src)[source]

Apply degradation effects in sequence

Parameters

src (numpy.ndarray) – source image of shape (rows, cols)

Returns

a copy of the source image {numpy.ndarray} after apply the effects

insert_image_state(kwargs)[source]

Replace the enumeration (ImageState) with the actual image in the keyword argument dictionary

Parameters
  • kwargs (dict) – keyword argument dictionary

  • Ex – {“src”: ImageState.ORIGINAL_STATE, “radius”: 5}

Returns

return keyword argument dictionary replaced with reference to the image

static validate_effects(effects)[source]

Validate the effects list

Parameters

effects (list) –

a list of 2-element tuple (method_name, method_kwargs) that defines:

  1. method_name : the name of the degradation method (method must be defined in genalog.degradation.effect)

  2. method_kwargs : the keyword arguments of the corresponding method

Example:

[
    ("blur", {"radius": "3"}),
    ("bleed_through", {"alpha":"0.8"}),
    ("morphology", {"operation": "open", "kernel_shape": (3,3), "kernel_type": "ones"}),
]
Raises

ValueError – raise this error when: method_name not defined in “genalog.degradation.effect” method_kwargs is not a valid keyword arguments in the corresponding method

class genalog.degradation.degrader.ImageState(value)[source]

An enumeration.

Degration Effects

genalog.degradation.effect.bleed_through(src, background=None, alpha=0.8, gamma=0, offset_x=0, offset_y=5)[source]

Apply bleed through effect, background is flipped horizontally.

Parameters
  • src (numpy.ndarray) – source image of shape (rows, cols)

  • background (numpy.ndarray, optional) – background image. Must be in same shape as foreground. Defaults to None.

  • alpha (float, optional) – transparent factor for the foreground. Defaults to 0.8.

  • gamma (int, optional) – luminance constant. Defaults to 0.

  • offset_x (int, optional) – background translation offset. Defaults to 0. Positive value shifts right and negative shifts right.

  • offset_y (int, optional) – background translation offset. Defaults to 5. Positive value shifts down and negative shifts up.

Returns

a copy of the source image after apply the effect. Pixel value ranges [0, 255]

Return type

numpy.ndarray

genalog.degradation.effect.blur(src, radius=5)[source]

Wrapper function for cv2.GaussianBlur

Parameters
  • src (numpy.ndarray) – source image of shape (rows, cols)

  • radius (int, optional) – size of the square kernel, MUST be an odd integer. Defaults to 5.

Returns

a copy of the source image after apply the effect

Return type

numpy.ndarray

genalog.degradation.effect.close(src, kernel)[source]

“close” morphological operation. Like morphological “dilation”, it grows the boundary of the foreground (white pixels), however, it is less destructive than dilation of the original boundary shape.

For more information see:

  1. https://docs.opencv.org/master/d9/d61/tutorial_py_morphological_ops.html

  2. http://homepages.inf.ed.ac.uk/rbf/HIPR2/close.htm

Parameters
  • src (numpy.ndarray) – source image of shape (rows, cols)

  • kernel (numpy.ndarray) – a 2D array for structuring the morphological effect

Returns

a copy of the source image after apply the effect.

Return type

numpy.ndarray

genalog.degradation.effect.create_2D_kernel(kernel_shape, kernel_type='ones')[source]

Create 2D kernel for morphological operations.

Parameters
  • kernel_shape (tuple) – shape of the kernel (rows, cols)

  • kernel_type (str, optional) – type of kernel. Defaults to “ones”.

All supported kernel types are below:

"ones": kernel is filled with all 1s in shape (rows, cols)
            [[1,1,1],
            [1,1,1],
            [1,1,1]]
"upper_triangle": upper triangular matrix filled with ones
            [[1,1,1],
            [0,1,1],
            [0,0,1]]
"lower_triangle": lower triangular matrix filled with ones
            [[1,0,0],
            [1,1,0],
            [1,1,1]]
"x": "X" shape cross
            [[1,0,1],
            [0,1,0],
            [1,0,1]]
"plus": "+" shape cross
            [[0,1,0],
            [1,1,1],
            [0,1,0]]
"ellipse": elliptical kernel
            [[0, 0, 1, 0, 0],
            [1, 1, 1, 1, 1],
            [1, 1, 1, 1, 1],
            [1, 1, 1, 1, 1],
            [0, 0, 1, 0, 0]]
Raises

ValueError – if kernel is not a 2-element tuple or kernel_type is not one of the supported values

Returns

a 2D array of shape kernel_shape.

Return type

numpy.ndarray

genalog.degradation.effect.dilate(src, kernel)[source]

“dilate” morphological operation. Grows foreground pixels (white pixels).

For more information see:

  1. https://docs.opencv.org/master/d9/d61/tutorial_py_morphological_ops.html

  2. http://homepages.inf.ed.ac.uk/rbf/HIPR2/dilate.htm

Parameters
  • src (numpy.ndarray) – source image of shape (rows, cols)

  • kernel (numpy.ndarray) – a 2D array for structuring the morphological effect

Returns

a copy of the source image after apply the effect.

Return type

numpy.ndarray

genalog.degradation.effect.erode(src, kernel)[source]

“erode” morphological operation. Erodes foreground pixels (white pixels).

For more information see:

  1. https://docs.opencv.org/master/d9/d61/tutorial_py_morphological_ops.html

  2. http://homepages.inf.ed.ac.uk/rbf/HIPR2/erode.htm

Parameters
  • src (numpy.ndarray) – source image of shape (rows, cols)

  • kernel (numpy.ndarray) – a 2D array for structuring the morphological effect

Returns

a copy of the source image after apply the effect.

Return type

numpy.ndarray

genalog.degradation.effect.morphology(src, operation='open', kernel_shape=(3, 3), kernel_type='ones')[source]

Dynamic calls different morphological operations (“open”, “close”, “dilate” and “erode”) with the given parameters

Parameters
  • src (numpy.ndarray) – source image of shape (rows, cols)

  • operation (str, optional) – name of a morphological operation: ("open", "close", "dilate", "erode") Defaults to "open".

  • kernel_shape (tuple, optional) – shape of the kernel (rows, cols). Defaults to (3,3).

  • kernel_type (str, optional) – type of kernel. ("ones", "upper_triangle", "lower_triangle", "x", "plus", "ellipse") Defaults to "ones".

Returns

a copy of the source image after apply the effect.

Return type

numpy.ndarray

genalog.degradation.effect.open(src, kernel)[source]

“open” morphological operation. Like morphological “erosion”, it removes foreground pixels (white pixels), however it is less destructive than erosion.

For more information see:

  1. https://docs.opencv.org/master/d9/d61/tutorial_py_morphological_ops.html

  2. http://homepages.inf.ed.ac.uk/rbf/HIPR2/open.htm

Parameters
  • src (numpy.ndarray) – source image of shape (rows, cols)

  • kernel (numpy.ndarray) – a 2D array for structuring the morphological effect

Returns

a copy of the source image after apply the effect.

Return type

numpy.ndarray

genalog.degradation.effect.overlay(src, background)[source]

Overlay two images together via bitwise-and:

dst[i] = src[i] & background[i]

Parameters
  • src (numpy.ndarray) – source image of shape (rows, cols)

  • background (numpy.ndarray) – background image. Must be in same shape are src

Returns

a copy of the source image after apply the effect

Return type

numpy.ndarray

genalog.degradation.effect.overlay_weighted(src, background, alpha, beta, gamma=0)[source]

overlay two images together, pixels from each image is weighted as follow

dst[i] = alpha*src[i] + beta*background[i] + gamma

Parameters
  • src (numpy.ndarray) – source image of shape (rows, cols)

  • background (numpy.ndarray) – background image. Must be in same shape are src

  • alpha (float) – transparent factor for the foreground

  • beta (float) – transparent factor for the background

  • gamma (int, optional) – luminance constant. Defaults to 0.

Returns

a copy of the source image after apply the effect

Return type

numpy.ndarray

genalog.degradation.effect.pepper(src, amount=0.05)[source]

Randomly sprinkle dark pixels on src image. Wrapper function for skimage.util.noise.random_noise(). See https://scikit-image.org/docs/stable/api/skimage.util.html#random-noise

Parameters
  • src (numpy.ndarray) – source image of shape (rows, cols)

  • amount (float, optional) – proportion of pixels in range [0, 1] to apply the effect. Defaults to 0.05.

Returns

a copy of the source image after apply the effect. Pixel value ranges [0, 255] as uint8.

Return type

numpy.ndarray

genalog.degradation.effect.pepper_then_salt(src, pepper_amount=0.05, salt_amount=0.1)[source]

Randomly add pepper then salt onto the image.

Parameters
  • src (numpy.ndarray) – source image of shape (rows, cols)

  • pepper_amount (float) – proportion of pixels in range [0, 1] to apply the pepper effect. Defaults to 0.05.

  • salt_amount (float) – proportion of pixels in range [0, 1] to apply the salt effect. Defaults to 0.1.

Returns

a copy of the source image after apply the effect. Pixel value ranges [0, 255] as uint8.

Return type

numpy.ndarray

genalog.degradation.effect.salt(src, amount=0.3)[source]

Randomly sprinkle white pixels on src image. Wrapper function for skimage.util.noise.random_noise(). See https://scikit-image.org/docs/stable/api/skimage.util.html#random-noise

Parameters
  • src (numpy.ndarray) – source image of shape (rows, cols)

  • amount (float, optional) – proportion of pixels in range [0, 1] to apply the effect. Defaults to 0.05.

Returns

a copy of the source image after apply the effect. Pixel value ranges [0, 255]

Return type

numpy.ndarray

genalog.degradation.effect.salt_then_pepper(src, salt_amount=0.1, pepper_amount=0.05)[source]

Randomly add salt then add pepper onto the image.

Parameters
  • src (numpy.ndarray) – source image of shape (rows, cols)

  • salt_amount (float) – proportion of pixels in range [0, 1] to apply the salt effect. Defaults to 0.1.

  • pepper_amount (float) – proportion of pixels in range [0, 1] to apply the pepper effect. Defaults to 0.05.

Returns

a copy of the source image after apply the effect. Pixel value ranges [0, 255] as uint8.

Return type

numpy.ndarray

genalog.degradation.effect.translation(src, offset_x, offset_y)[source]

Shift the image in x, y direction

Parameters
  • src (numpy.ndarray) – source image of shape (rows, cols)

  • offset_x (int) – pixels in the x direction. Positive value shifts right and negative shifts right.

  • offset_y (int) – pixels in the y direction. Positive value shifts down and negative shifts up.

Returns

a copy of the source image after apply the effect

Return type

numpy.ndarray