Faxify#
- class augraphy.augmentations.faxify.Faxify(scale_range=(1.0, 1.25), monochrome=-1, monochrome_method='random', monochrome_arguments={}, halftone=-1, invert=1, half_kernel_size=(1, 1), angle=(0, 360), sigma=(1, 3), numba_jit=1, p=1)[source]#
Bases:
Augmentation
Emulates faxify effect in the image.
- Parameters:
scale_range (tuple, optional) – Pair of floats determining the range from which to divide the resolution by.
monochrome (int, optional) – Flag to enable monochrome effect.
monochrome_method (string, optional) – Monochrome thresholding method.
monochrome_arguments (dict, optional) – A dictionary contains argument to monochrome thresholding method.
halftone (int, optional) – Flag to enable halftone effect.
invert (int, optional) – Flag to invert grayscale value in halftone effect.
half_kernel_size (tuple, optional) – Pair of ints to determine half size of gaussian kernel for halftone effect.
angle (tuple, optional) – Pair of ints to determine angle of halftone effect.
sigma (tuple, optional) – Pair of ints to determine sigma value of gaussian kernel in halftone effect.
numba_jit (int, optional) – The flag to enable numba jit to speed up the processing in the augmentation.
p (float, optional) – The probability that this Augmentation will be applied.
- static apply_halftone(image_halftone, gaussian_kernel, rotated, ysize, xsize, kernel_size)[source]#
Run loops and apply halftone effect in the input image.
- Parameters:
image_halftone (numpy.array (numpy.uint8)) – The image with halftone effect.
gaussian_kernel (numpy.array (numpy.uint8)) – Gaussian kernel to generate the halftone effect.
rotated (numpy.array (numpy.uint8)) – The rotated input image
ysize (int) – Row numbers of the input image.
xsize (int) – Column numbers of the input image.
kernel_size (int) – Kernel size to generate the halftone effect.
- complement_rgb_to_gray(img, invert=1, gray_level=255, max_value=255)[source]#
Convert RGB/BGR image to single channel grayscale image.
- Parameters:
img (numpy.array (numpy.uint8)) – The image to apply the function.
invert (int) – Flag to invert the generated grayscale value.
gray_level (int) – The selected gray value.
max_value (int) – Maximum value of gray value.
- cv_rotate(image, angle)[source]#
Rotate image based on the input angle.
- Parameters:
image (numpy.array (numpy.uint8)) – The image to apply the function.
angle (int) – The angle of the rotation.
- downscale(image)[source]#
Downscale image based on the user input scale value.
- Parameters:
image (numpy.array (numpy.uint8)) – The image to apply the function.
- generate_halftone(image, half_kernel_size=2, angle=45, sigma=2)[source]#
Generate halftone effect in the input image.
- Parameters:
image (numpy.array (numpy.uint8)) – The image to apply the function.
half_kernel_size (int) – Half value of kernel size to generate halftone effect.
angle (int) – The angle of the halftone effect.
sigma (int) – Sigma value of gaussian kernel for halftone effect.
Overview#
The Faxify augmentation emulates the artifacts created by faxing the document.
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:
Example 1#
In this example, a Faxify augmentation instance is initialized and it is set to opencv thresholding method (“cv2.threshold”) for monochrome effect. Halftone effect is enable (1) and halftone half kernel size is set to 2 (2,2). Halftone angle is set randomly between 0 to 360 degree (0, 360) and the sigma value of Gaussian blur is to random value between 1 and 3 (1,3).
Code example:
faxify = Faxify(scale_range = (1,2),
monochrome = 1,
monochrome_method = "cv2.threshold",
monochrome_arguments = {"thresh":128, "maxval":128, "type":cv2.THRESH_BINARY},
halftone = 1,
invert = 1,
half_kernel_size = (2,2),
angle = (0, 360),
sigma = (1,3))
img_faxify = faxify(image)
cv2.imshow("faxify", img_faxify)
Augmented image: