Skip to content

Commit cede0ae

Browse files
Merge pull request #340 from oarriaga/images
Resize the background images
2 parents 1d00695 + dd20438 commit cede0ae

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

paz/processors/image.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from ..backend.image import solarize
3333
from ..backend.image import cutout
3434
from ..backend.image import add_gaussian_noise
35-
from ..backend.image import BILINEAR
35+
from ..backend.image import BILINEAR, CUBIC
3636
from ..backend.image.tensorflow_image import imagenet_preprocess_input
3737
from ..backend.image.opencv_image import convolve_image
3838

@@ -383,16 +383,35 @@ def __init__(self, background_paths):
383383
raise ValueError('No paths given in ``background_paths``')
384384
self.background_paths = background_paths
385385

386+
def _random_shape_crop(self, image, shape, buffer=200):
387+
"""Randomly crops an image of the given ``shape``.
388+
389+
# Arguments
390+
image: Numpy array.
391+
shape: List of two ints ''(H, W)''.
392+
393+
# Returns
394+
Numpy array of cropped image.
395+
"""
396+
H, W = image.shape[:2]
397+
image_copy = image.copy()
398+
if (shape[0] >= H) or (shape[1] >= W):
399+
image = resize_image(image_copy, (shape[0] + buffer,
400+
shape[1] + buffer),
401+
method=CUBIC)
402+
H, W = image.shape[:2]
403+
x_min = np.random.randint(0, W - shape[1])
404+
y_min = np.random.randint(0, H - shape[0])
405+
x_max = int(x_min + shape[1])
406+
y_max = int(y_min + shape[0])
407+
cropped_image = image[y_min:y_max, x_min:x_max]
408+
return cropped_image
409+
386410
def call(self, image):
387411
random_arg = np.random.randint(0, len(self.background_paths))
388412
background_path = self.background_paths[random_arg]
389413
background = load_image(background_path)
390-
background = random_shape_crop(background, image.shape[:2])
391-
if background is None:
392-
H, W, num_channels = image.shape
393-
# background contains always a channel less
394-
num_channels = num_channels - 1
395-
background = make_random_plain_image((H, W, num_channels))
414+
background = self._random_shape_crop(background, image.shape[:2])
396415
return blend_alpha_channel(image, background)
397416

398417

0 commit comments

Comments
 (0)