Skip to content

Commit

Permalink
Merge pull request #342 from kwcckw/dev
Browse files Browse the repository at this point in the history
Updated NoiseGenerator.
  • Loading branch information
kwcckw authored Aug 10, 2023
2 parents 9b9b117 + c781015 commit 19c4b4d
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 10 deletions.
6 changes: 3 additions & 3 deletions augraphy/augmentations/badphotocopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ def __init__(
mask=None,
noise_type=-1,
noise_side="random",
noise_iteration=(2, 5),
noise_iteration=(1, 2),
noise_size=(1, 3),
noise_value=(0, 64),
noise_value=(32, 128),
noise_sparsity=(0.1, 0.9),
noise_concentration=(0.1, 0.6),
noise_concentration=(0.1, 0.7),
blur_noise=-1,
blur_noise_kernel=(5, 5),
wave_pattern=-1,
Expand Down
180 changes: 173 additions & 7 deletions augraphy/utilities/noisegenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,14 @@ def generate_mask_main(

if noise_type not in [1, 2, 3, 4]:

# generate sparsity
sparsity = random.uniform(noise_sparsity[0], noise_sparsity[1])

# gaussian noise
if noise_type == 5:
img_mask = np.full((ysize, xsize), fill_value=255, dtype="float")

iterations = random.randint(3, 8)
iterations = random.randint(2, 3)
for _ in range(iterations):
# random parameters for gaussian noise
mean = random.randint(0, 255)
Expand Down Expand Up @@ -455,6 +458,172 @@ def generate_mask_main(
img_mask = (img_mask - np.min(img_mask)) / (np.max(img_mask) - np.min(img_mask))
img_mask = (img_mask * 255).astype("uint8")

# sklearn.datasets' make_blobs noise
elif noise_type == 8:
# get max of y or x size
max_size = max(xsize, ysize)

n_clusters = (
int((noise_concentration[0]) * max_size),
int((noise_concentration[1]) * max_size),
)
n_samples = (
int((noise_concentration[0]) * max_size),
int((noise_concentration[1]) * max_size),
)

# prevent 0 cluster or 0 sample
n_clusters = (max(1, n_clusters[0]), max(1, n_clusters[1]))
n_samples = (max(10, n_samples[0]), max(10, n_samples[1]))

# generate array of samples
n_samples_array = [
random.randint(n_samples[0], n_samples[1])
for _ in range(random.randint(n_clusters[0], n_clusters[1]))
]

# generate std for clusters
std_range = (
int((noise_sparsity[0]) * (max_size)),
int((noise_sparsity[1]) * (max_size)),
)
std = max(50, int(random.randint(std_range[0], std_range[1]) / 5))

center_x = (-std, xsize + std)
center_y = (-std, ysize + std)

# generate coordinates for clusters of blobs
generated_points_x, generated_points_y = self.generate_points(
n_samples_array,
std,
center_x,
center_y,
xsize,
ysize,
)

# generate mask
img_mask = self.generate_mask(
noise_background,
noise_value,
generated_points_x,
generated_points_y,
xsize,
ysize,
)

# noise with consistent square pattern
elif noise_type == 9:

end_y = int(ysize * sparsity)
end_x = xsize

# get max of y or x size
max_size = max(xsize, end_y)

std_range = (
int((noise_sparsity[0]) * (max_size / 5)),
int((noise_sparsity[1]) * (max_size / 5)),
)
std = int(random.randint(std_range[0], std_range[1]) / 25)

center_x = (-std, xsize + std)
center_y = (-std, end_y + std)

# generate number of clusters and number of samples in each cluster
n_clusters = (
int((noise_concentration[0]) * max_size),
int((noise_concentration[1]) * max_size),
)
n_samples = (
int((noise_concentration[0]) * max_size),
int((noise_concentration[1]) * max_size),
)

# prevent 0 cluster or 0 sample
n_clusters = (max(1, n_clusters[0]), max(1, n_clusters[1]))
n_samples = (max(10, n_samples[0]), max(10, n_samples[1]))

# generate array of samples
n_samples_array = [
random.randint(n_samples[0], n_samples[1])
for _ in range(random.randint(n_clusters[0], n_clusters[1]))
]

n_step_x = int(xsize / random.randint(3, 6))
n_step_y = int(end_y / random.randint(8, 16))

# initialize points array
generated_points_x = np.array([[-1]], dtype="int")
generated_points_y = np.array([[-1]], dtype="int")

# initial noise location
ccenter_y = (0, 0)
ccenter_x = (0, 0)

# reduction ratio to determine the density of noise
samples_index = int(len(n_samples_array) * 0.05)

while ccenter_y[0] < end_y:

n_samples_array = n_samples_array[:samples_index]
samples_index = max(1, int(samples_index * 0.8))

# varying y
ccenter_y = (ccenter_y[1], ccenter_y[1] + n_step_y)
ccenter_x = (0, 0)

while ccenter_x[0] < end_x:
# varying x
ccenter_x = (ccenter_x[0], ccenter_x[1] + n_step_x)

# generate coordinates for clusters of blobs
cgenerated_points_x, cgenerated_points_y = self.generate_points(
n_samples_array,
std,
ccenter_x,
ccenter_y,
xsize,
ysize,
)

# combine coordinates
generated_points_x = np.concatenate(
[generated_points_x, cgenerated_points_x],
)
generated_points_y = np.concatenate(
[generated_points_y, cgenerated_points_y],
)

# space between next noise patch
add_space = random.randint(5, 10)
ccenter_x = (
ccenter_x[0] + n_step_x + add_space,
ccenter_x[1] + n_step_x + add_space,
)

# space between next noise patch
add_space = random.randint(5, 15)
ccenter_y = (ccenter_y[1] + add_space, ccenter_y[1] + add_space)

# generate mask
img_mask = self.generate_mask(
noise_background,
noise_value,
generated_points_x,
generated_points_y,
xsize,
ysize,
)

if noise_side == "left":
img_mask = np.rot90(img_mask, 1)
elif noise_side == "bottom" or noise_side == "bottom_left" or noise_side == "bottom_right":
img_mask = np.rot90(img_mask, 2)
elif noise_side == "right":
img_mask = np.rot90(img_mask, 3)
img_mask = cv2.resize(img_mask, (xsize, ysize), interpolation=cv2.INTER_LINEAR)

# threshold to increase or decrease the noise concentration
if noise_type == 5:
noise_threshold = int(random.uniform(noise_concentration[0], noise_concentration[1]) * 255)
Expand All @@ -468,7 +637,7 @@ def generate_mask_main(
size=(ysize, xsize),
dtype="uint8",
)
indices_background = img_mask > noise_threshold
indices_background = img_mask >= noise_threshold

# temporary assignment
min_value = np.min(img_mask)
Expand All @@ -486,9 +655,6 @@ def generate_mask_main(
# update background value
img_mask[indices_background] = img_background[indices_background]

# generate sparsity
sparsity = random.uniform(noise_sparsity[0], noise_sparsity[1])

# reduce noise area based on sparsity value
img_sparsity_value = np.arange(-0.3, 1, 1 / int(ysize * sparsity), dtype="float")
length = len(img_sparsity_value)
Expand Down Expand Up @@ -770,8 +936,8 @@ def generate_noise(
img_mask = np.full((ysize, xsize), fill_value=background_value, dtype="int")

# any invalid noise type will reset noise type to 0
if self.noise_type not in [1, 2, 3, 4, 5, 6, 7]:
noise_type = random.randint(1, 7)
if self.noise_type not in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
noise_type = random.randint(1, 9)
else:
noise_type = self.noise_type

Expand Down

0 comments on commit 19c4b4d

Please sign in to comment.