diff --git a/augraphy/augmentations/noisetexturize.py b/augraphy/augmentations/noisetexturize.py index e6e0b139..4d6200bd 100644 --- a/augraphy/augmentations/noisetexturize.py +++ b/augraphy/augmentations/noisetexturize.py @@ -7,7 +7,7 @@ class NoiseTexturize(Augmentation): - """Creates a random noise based texture pattern to emulate paper textures. + """Creates a random noise pattern to emulate paper textures. Consequently applies noise patterns to the original image from big to small. :param sigma_range: Defines bounds of noise fluctuations. diff --git a/augraphy/augmentations/quasicrystal.py b/augraphy/augmentations/quasicrystal.py index cd316f35..5a5041b0 100644 --- a/augraphy/augmentations/quasicrystal.py +++ b/augraphy/augmentations/quasicrystal.py @@ -23,7 +23,6 @@ """ import math -import os import random import warnings @@ -39,34 +38,46 @@ class PatternGenerator(Augmentation): - def __init__(self, imgx=512, imgy=512, n_rotation_range=(10, 15), p=1.0): - """ - In this implementation we take a geometric plane and every point in the plane is shaded according - to its position,(x,y) coordinate. We take the pattern and perform a bitwise not operation so that it can - be added as an background to an image.This code is a python implementation of a QuasiPattern Distortion augmentation techniques - using PIL and the OpenCV libraries. This augmentation creates a new pattern image and superimposes it onto an input image. - - To make the pattern more prominent - a. Increase the 'frequency' parameter: Increasing the frequency of the pattern will the it tightly populated and more prominent. - b. Decrease the 'n_rotation' parameter: Decreasing the number of rotations will make the pattern less symmetrical. - - :param imgx: width of the pattern image. default is 512 - :type imgx: int - :param imgy: height of the pattern image, default is 512 - :type imgy: int - :param n_rotation: is the number of rotations applied to the pattern, default value lies - between 10 and 15. - :type n_rotation: tuple (int) - - - """ + """In this implementation we take a geometric plane and every point in the plane is shaded according + to its position,(x,y) coordinate. We take the pattern and perform a bitwise not operation so that it can + be added as an background to an image.This code is a python implementation of a QuasiPattern Distortion augmentation techniques + using PIL and the OpenCV libraries. This augmentation creates a new pattern image and superimposes it onto an input image. + To make the pattern more prominent + a. Increase the 'frequency' parameter: Increasing the frequency of the pattern will the it tightly populated and more prominent. + b. Decrease the 'n_rotation' parameter: Decreasing the number of rotations will make the pattern less symmetrical. + + :param imgx: width of the pattern image. default is 512 + :type imgx: int, optional + :param imgy: height of the pattern image, default is 512 + :type imgy: int, optional + :param n_rotation: is the number of rotations applied to the pattern, default value lies + between 10 and 15. + :type n_rotation: tuple (int) , optional + :param color: Color of the pattern in BGR format. Use "random" for random color effect. + :type color: tuple (int), optional + :param alpha_range: Tuple of floats determining the alpha value of the patterns. + :type alpha_range: tuple (float), optional + """ + + def __init__( + self, + imgx=512, + imgy=512, + n_rotation_range=(10, 15), + color="random", + alpha_range=(0.25, 0.5), + p=1.0, + ): + """Constructor method""" super().__init__(p=p) self.imgx = imgx # width of the image self.imgy = imgy # hieght of the image self.n_rotation_range = n_rotation_range # number of rotation to be applied to the pattern + self.color = color + self.alpha_range = alpha_range def __repr__(self): - # return f"QuasiPattern Distortion: width = {self.imgx} , height = {self.imgy}, n_rotation = {self.n_rotation}" + # return f"QuasiPattern Distortion: width = {self.imgx} , height = {self.imgy}, n_rotation = {self.n_rotation}, color = {self.color}, alpha_range = {self.alpha_range}" return f"QuasiPattern Distortion: n_rotation_range = {self.n_rotation_range}" @staticmethod @@ -97,6 +108,7 @@ def __call__(self, image, layer=None, force=False): if force or self.should_run(): result = image.copy() h, w = result.shape[:2] + self.n_rotation = random.randint(self.n_rotation_range[0], self.n_rotation_range[1]) pattern_image = np.zeros((self.imgy, self.imgx, 3), dtype=np.uint8) frequency = random.random() * 100 + 18 # determines the frequency of pattern @@ -109,6 +121,19 @@ def __call__(self, image, layer=None, force=False): invert = cv2.cvtColor(invert, cv2.COLOR_RGB2GRAY) elif len(image.shape) == 3 and image.shape[2] == 1: invert = cv2.cvtColor(invert, cv2.COLOR_RGB2GRAY) - sw = PatternMaker(alpha=0.15) + sw = PatternMaker(alpha=random.uniform(self.alpha_range[0], self.alpha_range[1])) + + # apply color into pattern + if self.color == "random": + color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) + else: + color = self.color + if len(invert.shape) > 2: + color_mask = np.full_like(invert, fill_value=color, dtype="uint8") + else: + color_mask = np.full_like(invert, fill_value=np.mean(color), dtype="uint8") + invert = cv2.multiply(invert, color_mask, scale=1 / 255) + + # overlay pattern into image result = sw.superimpose(result, invert) return result diff --git a/doc/source/augmentations/augmentations/noisetexturize/noise_texturize.png b/doc/source/augmentations/augmentations/noisetexturize/noise_texturize.png index 5a42f179..a6e30b97 100644 Binary files a/doc/source/augmentations/augmentations/noisetexturize/noise_texturize.png and b/doc/source/augmentations/augmentations/noisetexturize/noise_texturize.png differ diff --git a/doc/source/augmentations/noisetexturize.rst b/doc/source/augmentations/noisetexturize.rst index c4829f18..74f80915 100644 --- a/doc/source/augmentations/noisetexturize.rst +++ b/doc/source/augmentations/noisetexturize.rst @@ -10,7 +10,7 @@ NoiseTexturize -------- Overview -------- -The Noise Texturize augmentation creates a random noise based texture pattern to emulate paper textures. +The Noise Texturize augmentation creates a random noise pattern to emulate paper textures. Initially, a clean image with single line of text is created. @@ -45,15 +45,15 @@ Clean image: --------- Example 1 --------- -In this example, a NoiseTexturize augmentation instance is initialized and the sigma value that define noise fluctuatiosn is set in between 12 and 15 (12, 15). -The noise turbulence range is set in between 3 and 5. +In this example, a NoiseTexturize augmentation instance is initialized and the sigma value that define noise fluctuatiosn is set in between 3 and 10 (3, 10). +The noise turbulence range is set in between 2 and 5. Code example: :: - noise_texturize = NoiseTexturize(sigma_range=(12, 15), - turbulence_range=(3, 5), + noise_texturize = NoiseTexturize(sigma_range=(3, 10), + turbulence_range=(2, 5), ) img_noise_texturize = noise_texturize(image)