LinesDegradation#

class augraphy.augmentations.linesdegradation.LinesDegradation(line_roi=(0.0, 0.0, 1.0, 1.0), line_gradient_range=(32, 255), line_gradient_direction=(0, 2), line_split_probability=(0.2, 0.4), line_replacement_value=(250, 255), line_min_length=(30, 40), line_long_to_short_ratio=(5, 7), line_replacement_probability=(0.4, 0.5), line_replacement_thickness=(1, 3), p=1)[source]#

Bases: Augmentation

Degrades lines by replacing lines formed by image gradients with a different value.

Parameters:
  • line_roi (tuple, optional) – Tuple of 4 (x0, y0, xn, yn) to determine the region of interest of the augmentation effect. 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)

  • line_gradient_range (tuple, optional) – Pair of ints determining range of gradient values (low, high) in detecting the lines.

  • line_gradient_direction (tuple, optional) – Set value to 0 for horizontal gradients, 1 for vertical gradients and 2 for both.

  • line_split_probability (tuple, optional) – Pair of floats determining the probability to split long line into shorter lines.

  • line_replacement_value (tuple, optional) – Pair of ints determining the new value of the detected lines.

  • line_min_length (tuple, optional) – Pair of ints determining the minimum length of detected lines.

  • line_long_to_short_ratio (tuple, optional) – Pair of ints determining the threshold ratio of major axis to minor axis of the detected lines.

  • ine_replacement_probability (tuple, optional) – Pair of floats determining the probability to replace the detected lines with new value.

  • line_replacement_thickness (tuple, optional) – Pair of ints determining the thickness of replaced lines.

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

Overview#

The LinesDegradation degrades lines by replacing lines formed by image gradients with a different value.

Initially, a clean image with single line of text and multiple lines 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")

# add lines
for i in range(15):
    cv2.line(image, (i*100,0), (i*100, 500), (30,30,30), 2**(i%2))
for i in range(5):
    cv2.line(image, (0,i*100), (1500, i*100), (30,30,30), 2**(i%2))

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_lines.png

Example 1#

In this example, a LinesDegradation augmentation instance is initialized and the line_gradient_direction is set to (1,1) so that it affects only the vertical line. The line_replacement_value is set to high value (250, 250) so that it creates a lines thinning effect.

Code example:

lines_degradation = LinesDegradation(line_roi = (0.0, 0.0, 1.0, 1.0),
                                     line_gradient_range=(32, 255),
                                     line_gradient_direction= (1,1),
                                     line_split_probability=(0.2, 0.3),
                                     line_replacement_value=(250, 250),
                                     line_min_length=(15, 15),
                                     line_long_to_short_ratio = (3,3),
                                     line_replacement_probability = (0.5, 0.5),
                                     line_replacement_thickness = (1, 2)
                                     )

img_lines_degradation = lines_degradation(image)
cv2.imshow("Vertical degraded lines - Thinning", img_lines_degradation)

Augmented image:

../../../_images/linesdegradation_thinning.png

Example 2#

In this example, a LinesDegradation augmentation instance is initialized and the line_roi (region of interest) is set at the left side of the image (0.0, 0.0, 0.5, 1.0). The line_replacement_value is set to low value (0, 25) so that it creates a lines thickening effect.

Code example:

lines_degradation = LinesDegradation(line_roi = (0.0, 0.0, 0.5, 1.0),
                                     line_gradient_range=(32, 255),
                                     line_gradient_direction= (2,2),
                                     line_split_probability=(0.2, 0.3),
                                     line_replacement_value=(0, 25),
                                     line_min_length=(15, 15),
                                     line_long_to_short_ratio = (3,3),
                                     line_replacement_probability = (1.0, 1.0),
                                     line_replacement_thickness = (2, 2)
                                     )

img_lines_degradation = lines_degradation(image)
cv2.imshow("Degraded lines in ROI - Thickening", img_lines_degradation)

Augmented image:

../../../_images/linesdegradation_thickening.png