Skip to content

Commit

Permalink
Merge pull request #331 from kwcckw/dev
Browse files Browse the repository at this point in the history
Updated PatternGenerator.
  • Loading branch information
kwcckw authored Jul 28, 2023
2 parents f783430 + cc38d1a commit f2c01c7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 30 deletions.
2 changes: 1 addition & 1 deletion augraphy/augmentations/noisetexturize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
73 changes: 49 additions & 24 deletions augraphy/augmentations/quasicrystal.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"""
import math
import os
import random
import warnings

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions doc/source/augmentations/noisetexturize.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f2c01c7

Please sign in to comment.