mirror of
https://github.com/open-mmlab/mmcv.git
synced 2025-06-03 21:54:52 +08:00
Make sure padding size is not negative (#1792)
* Make sure padding size is not negative If Resize maintains aspect ratio, could be the case that image shape is bigger than resize value. * Test pad shape smaller than image shape * Fix lint * Remove whitespaces
This commit is contained in:
parent
74031cc508
commit
7982dd1a06
@ -460,7 +460,6 @@ def impad(img,
|
|||||||
areas when padding_mode is 'constant'. Default: 0.
|
areas when padding_mode is 'constant'. Default: 0.
|
||||||
padding_mode (str): Type of padding. Should be: constant, edge,
|
padding_mode (str): Type of padding. Should be: constant, edge,
|
||||||
reflect or symmetric. Default: constant.
|
reflect or symmetric. Default: constant.
|
||||||
|
|
||||||
- constant: pads with a constant value, this value is specified
|
- constant: pads with a constant value, this value is specified
|
||||||
with pad_val.
|
with pad_val.
|
||||||
- edge: pads with the last value at the edge of the image.
|
- edge: pads with the last value at the edge of the image.
|
||||||
@ -479,7 +478,9 @@ def impad(img,
|
|||||||
|
|
||||||
assert (shape is not None) ^ (padding is not None)
|
assert (shape is not None) ^ (padding is not None)
|
||||||
if shape is not None:
|
if shape is not None:
|
||||||
padding = (0, 0, shape[1] - img.shape[1], shape[0] - img.shape[0])
|
width = max(shape[1] - img.shape[1], 0)
|
||||||
|
height = max(shape[0] - img.shape[0], 0)
|
||||||
|
padding = (0, 0, width, height)
|
||||||
|
|
||||||
# check pad_val
|
# check pad_val
|
||||||
if isinstance(pad_val, tuple):
|
if isinstance(pad_val, tuple):
|
||||||
|
@ -461,6 +461,10 @@ class TestGeometric:
|
|||||||
with pytest.raises(AssertionError):
|
with pytest.raises(AssertionError):
|
||||||
mmcv.impad(img, shape=(12, 15), padding=(0, 0, 5, 2))
|
mmcv.impad(img, shape=(12, 15), padding=(0, 0, 5, 2))
|
||||||
|
|
||||||
|
# Pad shape smaller than image shape
|
||||||
|
padded_img = mmcv.impad(img, shape=(8, 8))
|
||||||
|
assert padded_img.shape == (10, 10, 3)
|
||||||
|
|
||||||
def test_impad_to_multiple(self):
|
def test_impad_to_multiple(self):
|
||||||
img = np.random.rand(11, 14, 3).astype(np.float32)
|
img = np.random.rand(11, 14, 3).astype(np.float32)
|
||||||
padded_img = mmcv.impad_to_multiple(img, 4)
|
padded_img = mmcv.impad_to_multiple(img, 4)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user