Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add cutout and lighting #909

Merged
merged 3 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add cutout
  • Loading branch information
LXXXXR committed Mar 24, 2021
commit 29b3f856cc0e65a3e2ae828f5036f88e4be0f72d
8 changes: 4 additions & 4 deletions mmcv/image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from .colorspace import (bgr2gray, bgr2hls, bgr2hsv, bgr2rgb, bgr2ycbcr,
gray2bgr, gray2rgb, hls2bgr, hsv2bgr, imconvert,
rgb2bgr, rgb2gray, rgb2ycbcr, ycbcr2bgr, ycbcr2rgb)
from .geometric import (imcrop, imflip, imflip_, impad, impad_to_multiple,
imrescale, imresize, imresize_like, imrotate, imshear,
imtranslate, rescale_size)
from .geometric import (imcrop, imcutout, imflip, imflip_, impad,
impad_to_multiple, imrescale, imresize, imresize_like,
imrotate, imshear, imtranslate, rescale_size)
from .io import imfrombytes, imread, imwrite, supported_backends, use_backend
from .misc import tensor2imgs
from .photometric import (adjust_brightness, adjust_color, adjust_contrast,
Expand All @@ -22,5 +22,5 @@
'rgb2ycbcr', 'bgr2ycbcr', 'ycbcr2rgb', 'ycbcr2bgr', 'tensor2imgs',
'imshear', 'imtranslate', 'adjust_color', 'imequalize',
'adjust_brightness', 'adjust_contrast', 'lut_transform', 'clahe',
'adjust_sharpness', 'auto_contrast'
'adjust_sharpness', 'auto_contrast', 'imcutout'
]
52 changes: 52 additions & 0 deletions mmcv/image/geometric.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,58 @@ def impad_to_multiple(img, divisor, pad_val=0):
return impad(img, shape=(pad_h, pad_w), pad_val=pad_val)


def imcutout(img, shape, pad_val=0):
"""Randomly cut out a rectangle from the original img.

Args:
img (ndarray): Image to be cutout.
shape (int | tuple[int]): Expected cutout shape (h, w). If given as a
int, the value will be used for both h and w.
pad_val (int | float | tuple[int | float]): Values to be filled in the
cut area. Defaults to 0.
"""

channels = 1 if img.ndim == 2 else img.shape[2]
if isinstance(shape, int):
cut_h, cut_w = shape, shape
else:
assert isinstance(shape, tuple) and len(shape) == 2, \
f'shape must be a int or a tuple with length 2, but got type ' \
f'{type(shape)} instead.'
cut_h, cut_w = shape
if isinstance(pad_val, (int, float)):
pad_val = tuple([pad_val] * channels)
elif isinstance(pad_val, tuple):
assert len(pad_val) == channels, \
'Expected the num of elements in tuple equals the channels' \
'of input image. Found {} vs {}'.format(
len(pad_val), channels)
else:
raise TypeError(f'Invalid type {type(pad_val)} for `pad_val`')

img_h, img_w = img.shape[:2]
y0 = np.random.uniform(img_h)
x0 = np.random.uniform(img_w)

y1 = int(max(0, y0 - cut_h / 2.))
x1 = int(max(0, x0 - cut_w / 2.))
y2 = min(img_h, y1 + cut_h)
x2 = min(img_w, x1 + cut_w)

if img.ndim == 2:
patch_shape = (y2 - y1, x2 - x1)
else:
patch_shape = (y2 - y1, x2 - x1, channels)

img_cutout = img.copy()
patch = np.array(
pad_val, dtype=img.dtype) * np.ones(
patch_shape, dtype=img.dtype)
img_cutout[y1:y2, x1:x2, ...] = patch

return img_cutout


def _get_shear_matrix(magnitude, direction='horizontal'):
"""Generate the shear matrix for transformation.

Expand Down
34 changes: 34 additions & 0 deletions tests/test_image/test_geometric.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,40 @@ def test_impad_to_multiple(self):
padded_img = mmcv.impad_to_multiple(img, 2)
assert padded_img.shape == (20, 12)

def test_imcutout(self):
img = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).astype(np.uint8)

# shape must be int or tuple
with pytest.raises(AssertionError):
mmcv.imcutout(img, 2.5)
# pad_val must be int or float or tuple with the same length
# of img channels
with pytest.raises(AssertionError):
mmcv.imcutout(img, 1, (1, 2, 3))
with pytest.raises(TypeError):
mmcv.imcutout(img, 1, None)

# test cutout the whole img
assert_array_equal(mmcv.imcutout(img, 6), np.zeros_like(img))
# test not cutout
assert_array_equal(mmcv.imcutout(img, 0), img)
# test cutout when shape is int
np.random.seed(0)
img_cutout = np.array([[1, 2, 3], [4, 0, 6], [7, 8,
9]]).astype(np.uint8)
assert_array_equal(mmcv.imcutout(img, 1), img_cutout)
img_cutout = np.array([[1, 2, 3], [4, 10, 6], [7, 8,
9]]).astype(np.uint8)
assert_array_equal(mmcv.imcutout(img, 1, pad_val=10), img_cutout)
# test cutout when shape is tuple
np.random.seed(0)
img_cutout = np.array([[1, 2, 3], [0, 0, 6], [7, 8,
9]]).astype(np.uint8)
assert_array_equal(mmcv.imcutout(img, (1, 2)), img_cutout)
img_cutout = np.array([[1, 2, 3], [10, 10, 6], [7, 8,
9]]).astype(np.uint8)
assert_array_equal(mmcv.imcutout(img, (1, 2), pad_val=10), img_cutout)

def test_imrotate(self):
img = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).astype(np.uint8)
assert_array_equal(mmcv.imrotate(img, 0), img)
Expand Down