SectionShift#

class augraphy.augmentations.sectionshift.SectionShift(section_shift_number_range=(3, 5), section_shift_locations='random', section_shift_x_range=(-10, 10), section_shift_y_range=(-10, 10), section_shift_fill_value=-1, p=1)[source]#

Bases: Augmentation

Shift single or multiple sections of image in horizontal, vertical or both directions to create an effect of shifted image sections.

Parameters:
  • section_shift_number_range (tuple, optional) – Tuple of ints determing the number of section shift operation.

  • section_shift_locations (list, optional) – A nested list contains list of shifting boxes. Each box should be in format of [x0, y0, xn, yn]. Use “random” for random location.

  • section_shift_x_range (tuple, optional) – Tuple of ints determing the shifting value in horizontal direction. The shifting value will be in percentage of the image width if the value is float and in between -1.0 - 1.0: shifting_x (int) = image width * shifting_x (float and -1.0 - 1.0)

  • section_shift_y_range (tuple, optional) – Tuple of ints determing the shifting value in vertical direction. The shifting value will be in percentage of the image height if the value is float and in between -1.0 - 1.0: shifting_y (int) = image height * shifting_y (float and -1.0 - 1.0)

  • section_shift_fill_value (tuple, optional) – Tuple of values in BGR to fill in the shifted area. Use “-1” to not fill any value and the image default value will be used instead. Use “random” to fill random color.

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

apply_shift(image, mask, keypoints, bounding_boxes, shift_box, section_shift_x, section_shift_y)[source]#

Core function to shift section of image based on the input box and shifting values.

Parameters:
  • image (numpy array) – The input image.

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

  • shift_box (tuple) – Tuple contains the box of the shifting location in format of x0, y0, xn, yn.

  • section_shift_x (int) – The shifting value in horizontal direction.

  • section_shift_y (int) – The shifting value in vertical direction.

Overview#

The SectionShift augmentation shifts single or multiple sections of image in horizontal, vertical or both directions, creating an effect of shifted image sections. For additional input such as mask and keypoints, they are fully supported. For bounding boxes, they will be affected only if the starting point or ending point of the box in the shifting area.

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 SectionShift augmentation instance is initialized and the number of shifting sections is set to 5 (5,5). The shifting location is set of random coordinates (“random”). The shifting value in both horizontal and vertical direction is set to 20 (20,20). The new value in the shifted area is filled with white color (255,255,255).

Code example:

sectionshift = SectionShift(section_shift_number_range = (5,5),
                            section_shift_locations = "random",
                            section_shift_x_range = (20,20),
                            section_shift_y_range = (20,20),
                            section_shift_fill_value = (255,255,255)
                            )

img_sectionshift = sectionshift(image)
cv2.imshow("sectionshift", img_sectionshift)

Augmented image:

../../../_images/section_shift.png

Example 2#

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

Code example:

sectionshift = SectionShift()

img_sectionshift, mask, keypoints, bounding_boxes = sectionshift(image=image, mask=mask, keypoints=keypoints, bounding_boxes=bounding_boxes)

cv2.imshow("sectionshift", img_sectionshift)

Input mask:

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

Input keypoints:

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

Input bounding boxes:

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

Augmented image:

../../../_images/section_shift2.png

Augmented mask:

../../../_images/section_shift2_mask.png

Augmented keypoints:

../../../_images/section_shift2_keypoints.png

Augmented bounding boxes:

../../../_images/section_shift2_bounding_boxes.png