BadPhotoCopy#

class augraphy.augmentations.badphotocopy.BadPhotoCopy(noise_mask=None, noise_type=-1, noise_side='random', noise_iteration=(1, 2), noise_size=(1, 3), noise_value=(32, 128), noise_sparsity=(0.1, 0.9), noise_concentration=(0.1, 0.7), blur_noise=-1, blur_noise_kernel=(5, 5), wave_pattern=-1, edge_effect=-1, numba_jit=1, p=1)[source]#

Bases: Augmentation

Uses added noise to generate an effect of dirty copier.

Parameters:
  • noise_mask (uint8, optional) – Mask of noise to generate badphotocopy effect.

  • noise_type (int, optional) – Types of noises to generate different mask patterns. Use -1 to select randomly. 1 = sklearn.datasets’ make_blobs noise 2 = gaussian noise 3 = perlin noise 4 = worley noise 5 = rectangular pattern noise

  • noise_side (string, optional) – Location of noise. Select from: “random”, “left”, “top”, “right”, “bottom”, “top_left”, “top_right”, “bottom_left”, “bottom_right”, “none”, “all”.

  • noise_iteration (tuple, optional) – Pair of ints to determine number of iterations to apply noise in the mask.

  • noise_size (tuple, optional) – Pair of ints to determine scale of noise in the mask.

  • noise_value (tuple, optional) – Intensity range of noise, lower value get darker effect.

  • noise_sparsity (tuple, optional) – Pair of floats determining sparseness of noise.

  • noise_concentration (tuple, optional) – Pair of floats determining concentration of noise.

  • blur_noise (int, optional) – Flag to enable blur in noise mask. Use -1 to select randomly.

  • blur_noise_kernel (tuple, optional) – Kernel to blur noise mask.

  • wave_pattern (int, optional) – To enable wave pattern in noise. Use -1 to select randomly.

  • edge_effect (int, optional) – To add sobel edge effect into the noise mask. Use -1 to select randomly.

  • numba_jit (int, optional) – The flag to enable numba jit to speed up the processing in the augmentation.

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

apply_augmentation(image, mask, keypoints, bounding_boxes)[source]#

applies augmentation to the input image.

Parameters:
  • image (numpy.array (numpy.uint8)) – The image to apply the augmentation.

  • mask (numpy array (uint8), optional) – The mask of labels for each pixel. Mask value should be in range of 0 to 255.

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

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

apply_wave(mask, noise_side)[source]#

applies wavy pattern mask to input mask.

Parameters:
  • mask (numpy.array (numpy.uint8)) – The image to apply the function.

  • noise_side (string) – The side of the image to apply the wave effect.

static fill_wave_mask(img_wave, smooth_points)[source]#

Fill wavy pattern mask with value.

Parameters:
  • img_wave (numpy.array) – The image of wavy pattern mask.

  • smooth_points (numpy.array) – Coordinates of wavy pattern.

Overview#

There are 5 main types of noise in BadPhotoCopy augmentation:

Noise type 1 creates sklearn.datasets’ make_blobs noise. Noise type 2 creases Gaussian noise. Noise type 3 creates Perlin noise. Noise type 4 creates Worley noise. Noise type 5 creates rectangular pattern noise.

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 BadPhotoCopy augmentation instance is initialized with sklearn.datasets’ make_blobs noise (noise_type=1) and the noise is set at the left side of the image. Noise concentation is set to low intensity (0.2, 0.3) and noise sparsity is set to a low value (0.15, 0.15). Augmentation is set to run with 2 to 3 iterations and each time the noise size is randomly set from 2 to 3. Flag to blur noise is set to random (-1).

Code example:

BadPhotoCopy_type_1 = BadPhotoCopy(noise_type=1,
                                   noise_side="left",
                                   noise_iteration=(2,3),
                                   noise_size=(2,3),
                                   noise_sparsity=(0.15,0.15),
                                   noise_concentration=(0.3,0.3),
                                   blur_noise=-1,
                                   blur_noise_kernel=(5, 5),
                                   wave_pattern=0,
                                   edge_effect=0)
img_BadPhotoCopy_type_1 = BadPhotoCopy_type_1(image)
cv2.imshow("type1",img_BadPhotoCopy_type_1)

Augmented image:

../../../_images/type1.png

Example 2#

In this example, a BadPhotoCopy augmentation instance is initialized with Gaissian noise (noise_type=2) and the noise is set at the right side of the image. Noise concentation is set to low intensity (0.2, 0.2) and noise sparsity is set to a medium value (0.4, 0.5). Augmentation is set to run with single iteration (1, 1) and the noise size is set to 1. Flags to blur noise and edge effect in noise are enabled.

Code example:

BadPhotoCopy_type_2 = BadPhotoCopy(noise_type=2,
                                   noise_side="right",
                                   noise_iteration=(1,1),
                                   noise_size=(1,1),
                                   noise_sparsity=(0.4,0.5),
                                   noise_concentration=(0.2,0.2),
                                   blur_noise=1,
                                   blur_noise_kernel=(5, 5),
                                   wave_pattern=0,
                                   edge_effect=1)
img_BadPhotoCopy_type_2 = BadPhotoCopy_type_2(image)
cv2.imshow("type2",img_BadPhotoCopy_type_2)

Augmented image:

../../../_images/type2.png

Example 3#

In this example, a BadPhotoCopy augmentation instance is initialized with Perlin noise(noise_type=3). Noise is apply evenly to the whole image by setting noise side = “none”. Noise value is set to random value in between 128 and 255 (128, 255). Noise concentation is set to medium intensity (0.5, 0.5) and noise sparsity is set to a low value(0.2, 0.3). Augmentation is set to run with single iteration (1, 1) and noise size is randomly set from 1 to 3 (1, 3). Flag to blur noise is enabled and flag for edge effet is set to random (-1).

Code example:

BadPhotoCopy_type_3 = BadPhotoCopy(noise_type=3,
                                   noise_side="none",
                                   noise_iteration=(1,1),
                                   noise_size=(1,3),
                                   noise_value=(128, 255),
                                   noise_sparsity=(0.2,0.3),
                                   noise_concentration=(0.5,0.5),
                                   blur_noise=1,
                                   blur_noise_kernel=(5, 5),
                                   wave_pattern=0,
                                   edge_effect=1)
img_BadPhotoCopy_type_3 = BadPhotoCopy_type_3(image)
cv2.imshow("type3",img_BadPhotoCopy_type_3)

Augmented image:

../../../_images/type3.png

Example 4#

In this example, a BadPhotoCopy augmentation instance is initialized with Worley noise (noise_type=4). Noise is apply evenly to the whole image by setting noise side = “none”. Noise value is set to random value in between 32 and 255 (32, 255). Noise concentation is set to high intensity (0.99, 0.99) and noise sparsity is set to a moderate value(0.5, 0.5). Augmentation is set to run with single iteration (1,1) and the noise size is randomly set from 1 to 3. Flag to blur noise is enabled and flag for edge effet is set to random (-1).

Code example:

BadPhotoCopy_type_4 = BadPhotoCopy(noise_type=4,
                                   noise_side="none",
                                   noise_iteration=(1,1),
                                   noise_size=(1,3),
                                   noise_value=(32, 255),
                                   noise_sparsity=(0.5,0.5),
                                   noise_concentration=(0.99,0.99),
                                   blur_noise=0,
                                   wave_pattern=0,
                                   edge_effect=0)
img_BadPhotoCopy_type_4 = BadPhotoCopy_type_4(image)
cv2.imshow("type4",img_BadPhotoCopy_type_4)

Augmented image:

../../../_images/type4.png

Example 5#

In this example, a BadPhotoCopy augmentation instance is initialized with rectangular pattern noise (noise_type=5). Noise is apply to all borders of the image by setting noise side = “all”. Noise value is set to random value in between 32 and 128 (32, 128). Noise concentation is set to medium intensity (0.5, 0.5) and noise sparsity is set to a low value(0.3, 0.3). Augmentation is set to run with single iteration (1,1) and the noise size is randomly set from 1 to 3. Flag to blur noise is enabled and flag for edge effet is set to random (-1).

Code example:

BadPhotoCopy_type_5 = BadPhotoCopy(noise_type=5,
                                   noise_side="all",
                                   noise_iteration=(1,1),
                                   noise_size=(1,3),
                                   noise_value=(32, 128),
                                   noise_sparsity=(0.3,0.3),
                                   noise_concentration=(0.5,0.5),
                                   blur_noise=0,
                                   wave_pattern=0,
                                   edge_effect=0)
img_BadPhotoCopy_type_5 = BadPhotoCopy_type_5(image)
cv2.imshow("type5",img_BadPhotoCopy_type_5)

Augmented image:

../../../_images/type5.png