Skip to content

Commit 2ce972e

Browse files
committed
New RandomSaltAndPepper
1 parent e5209e4 commit 2ce972e

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

mltu/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
__version__ = "1.0.7"
1+
__version__ = "1.0.8"
22

33
from .annotations.image import Image

mltu/augmentors.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- RandomErodeDilate
1212
- RandomSharpen
1313
- RandomGaussianBlur
14+
- RandomSaltAndPepper
1415
"""
1516

1617
def randomness_decorator(func):
@@ -319,4 +320,60 @@ def __call__(self, image: Image, annotation: typing.Any) -> typing.Tuple[Image,
319320

320321
image.update(img)
321322

323+
return image, annotation
324+
325+
326+
class RandomSaltAndPepper(Augmentor):
327+
""" Randomly add Salt and Pepper noise to image"""
328+
def __init__(
329+
self,
330+
random_chance: float = 0.5,
331+
log_level: int = logging.INFO,
332+
salt_vs_pepper: float = 0.5,
333+
amount: float = 0.1,
334+
) -> None:
335+
""" Randomly add Salt and Pepper noise to image
336+
337+
Args:
338+
random_chance (float): Float between 0.0 and 1.0 setting bounds for random probability. Defaults to 0.5.
339+
log_level (int): Log level for the augmentor. Defaults to logging.INFO.
340+
salt_vs_pepper (float): ratio of salt vs pepper. Defaults to 0.5.
341+
amount (float): proportion of the image to be salted and peppered. Defaults to 0.1.
342+
"""
343+
super(RandomSaltAndPepper, self).__init__(random_chance, log_level)
344+
self.salt_vs_pepper = salt_vs_pepper
345+
self.amount = amount
346+
347+
assert 0 <= salt_vs_pepper <= 1.0, "salt_vs_pepper must be between 0.0 and 1.0"
348+
assert 0 <= amount <= 1.0, "amount must be between 0.0 and 1.0"
349+
350+
@randomness_decorator
351+
def __call__(self, image: Image, annotation: typing.Any) -> typing.Tuple[Image, typing.Any]:
352+
""" Randomly add salt and pepper noise to an image
353+
354+
Args:
355+
image (Image): Image to be noised
356+
annotation (typing.Any): Annotation to be noised
357+
358+
Returns:
359+
image (Image): Noised image
360+
annotation (typing.Any): Noised annotation if necessary
361+
"""
362+
img = image.numpy()
363+
height, width, channels = img.shape
364+
365+
# Salt mode
366+
num_salt = int(self.amount * height * width * self.salt_vs_pepper)
367+
row_coords = np.random.randint(0, height, size=num_salt)
368+
col_coords = np.random.randint(0, width, size=num_salt)
369+
img[row_coords, col_coords, :] = [255, 255, channels]
370+
371+
# Pepper mode
372+
num_pepper = int(self.amount * height * width * (1.0 - self.salt_vs_pepper))
373+
row_coords = np.random.randint(0, height, size=num_pepper)
374+
col_coords = np.random.randint(0, width, size=num_pepper)
375+
img[row_coords, col_coords, :] = [0, 0, channels]
376+
377+
image.update(img)
378+
322379
return image, annotation

0 commit comments

Comments
 (0)