Folding#

class augraphy.augmentations.folding.Folding(fold_x=None, fold_deviation=(0, 0), fold_count=2, fold_noise=0.01, fold_angle_range=(0, 0), gradient_width=(0.1, 0.2), gradient_height=(0.01, 0.02), backdrop_color=(0, 0, 0), p=1)[source]#

Bases: Augmentation

Emulates folding effect from perspective transformation

Parameters:
  • fold_x (int, optional) – X coordinate of the folding effect.

  • fold_deviation (tuple, optional) – Deviation (in pixels) of provided X coordinate location.

  • count (fold) – Number of applied foldings

  • fold_noise (float, optional) – Level of noise added to folding area. Range from value of 0 to 1.

  • fold_angle_range (tuple, optional) – Tuple of ints determining the angle to rotate the image before applying a varying angle folding effect.

  • gradient_width (tuple, optional) – Tuple (min, max) Measure of the space affected by fold prior to being warped (in units of percentage of width of page).

  • gradient_height (tuple, optional) – Tuple (min, max) Measure of depth of fold (unit measured as percentage page height)

  • backdrop_color (tuple, optional) – The backdrop color (BGR) of the folding effect.

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

apply_folding(img, keypoints, bounding_boxes, ysize, xsize, fold_x, fold_width_one_side, fold_y_shift, fold_noise, fmask)[source]#

Apply perspective transform twice to get single folding effect.

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

  • 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).

  • ysize (int) – Height of the image.

  • xsize (int) – Width of the image.

  • gradient_width (int) – Measure of the space affected by fold prior to being warped (in units of percentage of width of page).

  • gradient_height (int) – Measure of depth of fold (unit measured as percentage page height).

  • fold_noise (float) – Level of noise added to folding area.

  • fmask (int) – Flag to identify if the input is mask instead of image.

apply_rotate_and_folding(image_fold, fold_angle, fold_x=None, fold_width_one_side=None, fold_y_shift=None, keypoints=None, bounding_boxes=None, fmask=0)[source]#

Apply rotation and folding effect.

Parameters:
  • image_fold – The image to apply the function.

  • fold_angle (int) – The angle of rotation.

  • fold_x (int) – The folding center x coordinate

  • fold_width_one_side (int) – The warped width of folding effect from the fold_x.

  • fold_y_shift (int) – Depth of the folding effect.

  • fmask (int) – Flag to identify if the input image is mask isntead of image.

Overview#

The Folding augmentation emulates folded paper being scanned, with a visible warp effect around the fold line. For additional input such as mask and keypoints, they are fully supported. For bounding boxes, only the start point or end point of the box will be affected.

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 Folding augmentation instance is initialized and the folding count is set to 10 (fold_count=10). There will be no noise at the folding area (fold_noise=0.0) and the angle of folding is set to random value in between -360 to 360 degree (-360, 360). Each folding gradient width is set to low value (0.2, 0.2) and folding gradient height is to very low value (0.02, 0.02). The backdrop color on the folding effect is set to black color (0, 0, 0).

Code example:

folding = Folding(fold_count=10,
                  fold_noise=0.0,
                  fold_angle_range = (-360,360),
                  gradient_width=(0.1, 0.2),
                  gradient_height=(0.01, 0.1),
                  backdrop_color = (0,0,0),
                  )

img_folded= folding(image)
cv2.imshow("folding", img_folded)

Augmented image:

../../../_images/folding.png

Example 2#

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

Code example:

folding = Folding()

img_folded, mask, keypoints, bounding_boxes = folding(image=image, mask=mask, keypoints=keypoints, bounding_boxes=bounding_boxes)
cv2.imshow("folding", img_folded)

Input mask:

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

Input keypoints:

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

Input bounding boxes:

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

Augmented image:

../../../_images/folding2.png

Augmented mask:

../../../_images/folding2_mask.png

Augmented keypoints:

../../../_images/folding2_keypoints.png

Augmented bounding boxes:

../../../_images/folding2_bounding_boxes.png