Skip to content

Commit d8a212d

Browse files
committed
New image blurring Augmentor
1 parent 63da908 commit d8a212d

File tree

4 files changed

+60
-9
lines changed

4 files changed

+60
-9
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1+
## [1.0.6] - 2022-04-13
2+
### Changed
3+
- Fixed bug in `mltu.dataProvider.DataProvider` object to work without `data_preprocessors` when feeding loaded data in memory
4+
5+
### Added
6+
- Added `RandomGaussianBlur` augmentor into `mltu.augmentors`
7+
8+
19
## [1.0.4] - 2022-03-22
210
### Changed
311
- Fix `ImageReader` to work either with image path or `np.ndarray`
412
- Added `metadata` support to `callbacks/tf2onnx` when converting to onnx format
513

14+
### Added
15+
-
616

717
## [1.0.3] - 2022-03-20
818
### 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.5"
1+
__version__ = "1.0.6"
22

33
from .annotations.image import Image

mltu/augmentors.py

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- RandomRotate
1111
- RandomErodeDilate
1212
- RandomSharpen
13+
- RandomGaussianBlur
1314
"""
1415

1516
def randomness_decorator(func):
@@ -92,7 +93,7 @@ def __call__(self, image: Image, annotation: typing.Any) -> typing.Tuple[Image,
9293
9394
Returns:
9495
image (Image): Adjusted image
95-
annotation (typing.Any): Adjusted annotation
96+
annotation (typing.Any): Adjusted annotation if necessary
9697
"""
9798
value = 1 + np.random.uniform(-self._delta, self._delta) / 255
9899

@@ -202,10 +203,12 @@ def __call__(self, image: Image, annotation: typing.Any) -> typing.Tuple[Image,
202203
""" Randomly erode and dilate image
203204
204205
Args:
205-
image (np.ndarray): Image to be eroded and dilated
206+
image (Image): Image to be eroded and dilated
207+
annotation (typing.Any): Annotation to be adjusted
206208
207209
Returns:
208-
image (np.ndarray): Eroded and dilated image
210+
image (Image): Eroded and dilated image
211+
annotation (typing.Any): Adjusted annotation if necessary
209212
"""
210213
kernel = np.ones(self._kernel_size, np.uint8)
211214

@@ -218,6 +221,7 @@ def __call__(self, image: Image, annotation: typing.Any) -> typing.Tuple[Image,
218221

219222
return image, annotation
220223

224+
221225
class RandomSharpen(Augmentor):
222226
""" Randomly sharpen image"""
223227
def __init__(
@@ -255,12 +259,12 @@ def __call__(self, image: Image, annotation: typing.Any) -> typing.Tuple[Image,
255259
""" Randomly sharpen image
256260
257261
Args:
258-
image (np.ndarray): Image to be sharpened
259-
annotation (typing.Any): Annotation to be sharpened
262+
image (Image): Image to be sharpened
263+
annotation (typing.Any): Annotation to be adjusted
260264
261265
Returns:
262-
image (np.ndarray): Sharpened image
263-
annotation (typing.Any): Sharpened annotation
266+
image (Image): Sharpened image
267+
annotation (typing.Any): Adjusted annotation if necessary
264268
"""
265269
lightness = np.random.uniform(*self._ligtness_range)
266270
alpha = np.random.uniform(*self._alpha_range)
@@ -278,4 +282,41 @@ def __call__(self, image: Image, annotation: typing.Any) -> typing.Tuple[Image,
278282
# Merge the sharpened channels back into the original image
279283
image.update(cv2.merge([r_sharp, g_sharp, b_sharp]))
280284

285+
return image, annotation
286+
287+
288+
class RandomGaussianBlur(Augmentor):
289+
""" Randomly erode and dilate image"""
290+
def __init__(
291+
self,
292+
random_chance: float = 0.5,
293+
log_level: int = logging.INFO,
294+
sigma: typing.Union[int, float] = 0.5,
295+
) -> None:
296+
""" Randomly erode and dilate image
297+
298+
Args:
299+
random_chance (float): Float between 0.0 and 1.0 setting bounds for random probability. Defaults to 0.5.
300+
log_level (int): Log level for the augmentor. Defaults to logging.INFO.
301+
sigma (int, float): standard deviation of the Gaussian kernel
302+
"""
303+
super(RandomGaussianBlur, self).__init__(random_chance, log_level)
304+
self.sigma = sigma
305+
306+
@randomness_decorator
307+
def __call__(self, image: Image, annotation: typing.Any) -> typing.Tuple[Image, typing.Any]:
308+
""" Randomly blurs an image with a Gaussian filter
309+
310+
Args:
311+
image (Image): Image to be blurred
312+
annotation (typing.Any): Annotation to be blurred
313+
314+
Returns:
315+
image (Image): Blurred image
316+
annotation (typing.Any): Blurred annotation if necessary
317+
"""
318+
img = cv2.GaussianBlur(image.numpy(), (0, 0), self.sigma)
319+
320+
image.update(img)
321+
281322
return image, annotation

mltu/dataProvider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def __init__(
4242
log_level (int, optional): The log level. Defaults to logging.INFO.
4343
"""
4444
self._dataset = dataset
45-
self._data_preprocessors = data_preprocessors
45+
self._data_preprocessors = [] if data_preprocessors is None else data_preprocessors
4646
self._batch_size = batch_size
4747
self._shuffle = shuffle
4848
self._epoch = initial_epoch

0 commit comments

Comments
 (0)