Geometric#

class augraphy.augmentations.geometric.Geometric(scale=(1, 1), translation=(0, 0), fliplr=0, flipud=0, crop=(), rotate_range=(0, 0), padding=[0, 0, 0, 0], padding_type='fill', padding_value=(255, 255, 255), randomize=0, p=1)[source]#

Bases: Augmentation

Applies basic geometric transformations such as resizing, flips and rotation.

Parameters:
  • scale (tuple, optional) – Pair of floats determining new scale of image.

  • translation (tuple, optional) – Pair of values determining x and y translation value. The translation value will be in percentage of the image size if the value is float and in between -1.0 - 1.0: x (int) = image width * x (float and -1.0 - 1.0); y (int) = image height * y (float and -1.0 - 1.0)

  • fliplr (int, optional) – Flag to flip image in left right direction.

  • flipud (int, optional) – Flag to flip image in up down direction.

  • crop (tuple, optional) – Tuple of 4 (x0, y0, xn, yn) to crop section of image. The value will be in percentage of the image size if the value is float and in between 0.0 - 1.0: x0 (int) = image width * x0 (float and 0.0 - 1.0); y0 (int) = image height * y0 (float and 0.0 - 1.0); xn (int) = image width * xn (float and 0.0 - 1.0); yn (int) = image height * yn (float and 0.0 - 1.0)

  • rotate_range (tuple, optional) – Pair of ints determining the range from which to sample the image rotation.

  • randomize (int, optional) – Flag to apply random geometric transformations.

  • padding (list, optional) – Padding amount on each (left, right, top, bottom) side. The padding amount will be in percentage of the image size if the value is float and in between 0.0 - 1.0: left (int) = image width * left (float and 0.0 - 1.0); right (int) = image height * right (float and 0.0 - 1.0); top (int) = image width * top (float and 0.0 - 1.0); bottom (int) = image height * bottom (float and 0.0 - 1.0)

  • padding_type – Padding methods, select from fill,duplicate and mirror.

  • padding_value – Padding value (in BGR) for fill padding method.

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

randomize_parameters(image)[source]#

Randomize parameters for random geometrical effect.

Parameters:

image (numpy array) – The input image.

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

Crop image based on the input cropping box.

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

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

Flip image left-right or up-down based on the input flipping flags.

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

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

Apply padding to image based on the input padding value.

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

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

Rotate image based on the input rotation angle.

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

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

Scale image size based on the input scaling ratio.

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

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

Translate image based on the input translation value.

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

Overview#

The geometric augmentation applies basic geometric transformations such as resizing, flips and rotation.

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 Geometric augmentation instance is initialized and the image size is scaled randomly between 0.5 to 1.5 times (0.5, 1.5) of original size. Horizontal translation is set to 50 pixels to the right and vertical translation is set to 50 pixels to the top of the image (50, -50). Flags for both flip left right and flip up down is enabled. It is set to rotate randomly from 3 to 5 degree (3,5).

Code example:

geometric = Geometric(scale=(0.5, 1.5),
                      translation=(50, -50),
                      fliplr=1,
                      flipud=1,
                      crop=(),
                      rotate_range=(3, 5)
                      )

img_geometric_transform = geometric(image)

cv2.imshow("geometric_transform", img_geometric_transform)

Augmented image:

../../../_images/geometric_transform.png

Example 2#

In this example, a Geometric augmentation will be applied to additional inputs such as mask, keypoints and bounding boxes. The scale of the augmentation is set to random value in between 1.5 and 2.0.

Code example:

geometric = Geometric(scale=(1.5, 2.0))

img_geometric_transform, mask, keypoints, bounding_boxes = geometric(image, mask, keypoints, bounding_boxes)

cv2.imshow("geometric_transform", img_geometric_transform)

Input mask:

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

Input keypoints:

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

Input bounding boxes:

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

Augmented image:

../../../_images/geometric_transform2.png

Augmented mask:

../../../_images/geometric_transform2_mask.png

Augmented keypoints:

../../../_images/geometric_transform2_keypoints.png

Augmented bounding boxes:

../../../_images/geometric_transform2_bounding_boxes.png

Example 3#

In this example, a Geometric augmentation will be applied to additional inputs such as mask, keypoints and bounding boxes. The translation of the augmentation is set to translate 100 pixels to the right and 100 pixels to the top section of the image.

Code example:

geometric = Geometric(translation=(100, -100))

img_geometric_transform, mask, keypoints, bounding_boxes = geometric(image, mask, keypoints, bounding_boxes)

cv2.imshow("geometric_transform", img_geometric_transform)

Augmented image:

../../../_images/geometric_transform3.png

Augmented mask:

../../../_images/geometric_transform3_mask.png

Augmented keypoints:

../../../_images/geometric_transform3_keypoints.png

Augmented bounding boxes:

../../../_images/geometric_transform3_bounding_boxes.png

Example 4#

In this example, a Geometric augmentation will be applied to additional inputs such as mask, keypoints and bounding boxes. The augmentation is set to flip image in left-right and up-down direction.

Code example:

geometric = Geometric(fliplr=1, flipud=1)

img_geometric_transform, mask, keypoints, bounding_boxes = geometric(image, mask, keypoints, bounding_boxes)

cv2.imshow("geometric_transform", img_geometric_transform)

Augmented image:

../../../_images/geometric_transform4.png

Augmented mask:

../../../_images/geometric_transform4_mask.png

Augmented keypoints:

../../../_images/geometric_transform4_keypoints.png

Augmented bounding boxes:

../../../_images/geometric_transform4_bounding_boxes.png

Example 5#

In this example, a Geometric augmentation will be applied to additional inputs such as mask, keypoints and bounding boxes. The augmentation is set to crop 60% of image, removing 20% of all image edges.

Code example:

geometric = Geometric(crop=(0.2, 0.2, 0.8, 0.8))

img_geometric_transform, mask, keypoints, bounding_boxes = geometric(image, mask, keypoints, bounding_boxes)

cv2.imshow("geometric_transform", img_geometric_transform)

Augmented image:

../../../_images/geometric_transform5.png

Augmented mask:

../../../_images/geometric_transform5_mask.png

Augmented keypoints:

../../../_images/geometric_transform5_keypoints.png

Augmented bounding boxes:

../../../_images/geometric_transform5_bounding_boxes.png

Example 6#

In this example, a Geometric augmentation will be applied to additional inputs such as mask, keypoints and bounding boxes. The augmentation is set to rotate image randomly between -60 degree and 60 degree.

Code example:

geometric = Geometric(rotate_range=(-60,60))

img_geometric_transform, mask, keypoints, bounding_boxes = geometric(image, mask, keypoints, bounding_boxes)

cv2.imshow("geometric_transform", img_geometric_transform)

Augmented image:

../../../_images/geometric_transform6.png

Augmented mask:

../../../_images/geometric_transform6_mask.png

Augmented keypoints:

../../../_images/geometric_transform6_keypoints.png

Augmented bounding boxes:

../../../_images/geometric_transform6_bounding_boxes.png

Example 7#

In this example, a Geometric augmentation will be applied to additional inputs such as mask, keypoints and bounding boxes. The augmentation is set to pad image in all edges by 10%.

Code example:

geometric = Geometric(padding=[0.1,0.1,0.1,0.1])

img_geometric_transform, mask, keypoints, bounding_boxes = geometric(image, mask, keypoints, bounding_boxes)

cv2.imshow("geometric_transform", img_geometric_transform)

Augmented image:

../../../_images/geometric_transform7.png

Augmented mask:

../../../_images/geometric_transform7_mask.png

Augmented keypoints:

../../../_images/geometric_transform7_keypoints.png

Augmented bounding boxes:

../../../_images/geometric_transform7_bounding_boxes.png