GlitchEffect#

class augraphy.augmentations.glitcheffect.GlitchEffect(glitch_direction='random', glitch_number_range=(8, 16), glitch_size_range=(5, 50), glitch_offset_range=(10, 50), p=1)[source]#

Bases: Augmentation

Create glitch effect by applying ColorShift and shifts patches of image horizontally or vertically.

Parameters:
  • glitch_direction (string, optional) – Direction of the glitch effect, select from “vertical”, “horizontal”, “all” or “random”.

  • glitch_number_range (tuple, optional) – Tuple of ints determing the number of shifted image patches.

  • glitch_size_range (tuple, optional) – Tuple of ints/floats determing the size of image patches. If the value is within the range of 0.0 to 1.0 and the value is float, the size will be scaled by image height: size (int) = image height * size (float and 0.0 - 1.0)

  • glitch_offset_range (tuple, optional) – Tuple of ints/floats determing the offset value to shift the image patches. If the value is within the range of 0.0 to 1.0 and the value is float, the size will be scaled by image width: offset (int) = image width * offset (float and 0.0 - 1.0)

  • p (float, optional) – The probability that this Augmentation will be applied.

apply_glitch(image, glitch_direction, mask, keypoints, bounding_boxes)[source]#

Apply glitch effect into the image by shifting patches of images.

Parameters:
  • image (numpy array) – Image to apply the glitch effect.

  • direction (string) – The direction of glitch effect.

  • mask (numpy array (uint8)) – The mask of labels for each pixel. Mask value should be in range of 1 to 255. Value of 0 will be assigned to the filled area after the transformation.

  • keypoints (dictionary) – A dictionary of single or multiple labels where each label is a nested list of points coordinate.

  • bounding_boxes (list) – A nested list where each nested list contains box location (x1, y1, x2, y2).

Overview#

The GlitchEffect augmentation create glitch effect by applying ColorShift and shifts patches of image horizontally or vertically. For additional input such as mask and keypoints, they are fully supported. For bounding boxes, additional boxes might be created on the shifted area. Size of some existing boxes might be pruned on the shifted area too.

Initially, a clean image with single line of text is created.

Code example:

# import libraries
import cv2
import numpy as np
from augraphy import *


# create a clean image with single line of text
image = np.full((500, 1500,3), 250, dtype="uint8")
cv2.putText(
    image,
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit",
    (80, 250),
    cv2.FONT_HERSHEY_SIMPLEX,
    1.5,
    0,
    3,
)

cv2.imshow("Input image", image)

Clean image:

../../../_images/input.png

Example 1#

In this example, a GlitchEffect augmentation instance is initialized and glitch direction is set to horizontal (“horizontal”). The number of glitch effect is set to any random number between 8 and 16 (8,16). The glitch size is set to random value in between 5 and 50 pixels (5, 50). The glitch offset is set of random value in between 5 and 10 pixels (5, 10).

Code example:

glitcheffect= GlitchEffect(glitch_direction = "horizontal",
                           glitch_number_range = (8, 16),
                           glitch_size_range = (5, 50),
                           glitch_offset_range = (5, 10)
                           )

img_glitcheffect = glitcheffect(image)

cv2.imshow("glitcheffect, img_glitcheffect)

Augmented image:

../../../_images/glitch_effect.png

Example 2#

In this example, a GlitchEffect augmentation will be applied to additional inputs such as mask, keypoints and bounding boxes. The GlitchEffect augmentation will be using the default parameters value and hence no additional parameters value will be specified.

Code example:

glitcheffect= GlitchEffect()

img_glitcheffect, mask, keypoints, bounding_boxes = glitcheffect(image=image, mask=mask, keypoints=keypoints, bounding_boxes=bounding_boxes)

cv2.imshow("glitcheffect, img_glitcheffect)

Input mask:

../../../_images/input_mask.png

Input keypoints:

../../../_images/input_keypoints.png

Input bounding boxes:

../../../_images/input_bounding_boxes.png

Augmented image:

../../../_images/glitch_effect2.png

Augmented mask:

../../../_images/glitch_effect2_mask.png

Augmented keypoints:

../../../_images/glitch_effect2_keypoints.png

Augmented bounding boxes:

../../../_images/glitch_effect2_bounding_boxes.png