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

Make sure padding size is not negative #1792

Merged
merged 5 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions mmcv/image/geometric.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,6 @@ def impad(img,
areas when padding_mode is 'constant'. Default: 0.
padding_mode (str): Type of padding. Should be: constant, edge,
reflect or symmetric. Default: constant.

- constant: pads with a constant value, this value is specified
with pad_val.
- edge: pads with the last value at the edge of the image.
Expand All @@ -479,7 +478,9 @@ def impad(img,

assert (shape is not None) ^ (padding 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
if isinstance(pad_val, tuple):
Expand Down
4 changes: 4 additions & 0 deletions tests/test_image/test_geometric.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,10 @@ def test_impad(self):
with pytest.raises(AssertionError):
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):
img = np.random.rand(11, 14, 3).astype(np.float32)
padded_img = mmcv.impad_to_multiple(img, 4)
Expand Down