Skip to content
This repository was archived by the owner on Jul 2, 2021. It is now read-only.

Fix resize_contain with odd margin #404

Merged
merged 4 commits into from
Aug 20, 2017
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
29 changes: 11 additions & 18 deletions chainercv/transforms/image/resize_contain.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import division
import numpy as np

from chainercv.transforms import resize
Expand Down Expand Up @@ -47,11 +48,11 @@ def resize_contain(img, size, fill=0, return_param=False):
"""
C, H, W = img.shape
out_H, out_W = size
scale_h = out_H / float(H)
scale_w = out_W / float(W)
scale = min(min(scale_h, scale_w), 1.)
scale_h = out_H / H
scale_w = out_W / W
scale = min(min(scale_h, scale_w), 1)
scaled_size = (int(H * scale), int(W * scale))
if scale < 1.:
if scale < 1:
img = resize(img, scaled_size)
y_slice, x_slice = _get_pad_slice(img, size=size)
out_img = np.empty((C, out_H, out_W), dtype=img.dtype)
Expand All @@ -76,23 +77,15 @@ def _get_pad_slice(img, size):
_, H, W = img.shape

if H < size[0]:
diff_y = size[0] - H
margin_y = diff_y / 2
if diff_y % 2 == 0:
y_slice = slice(int(margin_y), int(size[0] - margin_y))
else:
y_slice = slice(int(margin_y), int(size[0] - margin_y - 1))
margin_y = (size[0] - H) // 2
else:
y_slice = slice(0, int(size[0]))
margin_y = 0
y_slice = slice(margin_y, margin_y + H)

if W < size[1]:
diff_x = size[1] - W
margin_x = diff_x / 2
if diff_x % 2 == 0:
x_slice = slice(int(margin_x), int(size[1] - margin_x))
else:
x_slice = slice(int(margin_x), int(size[1] - margin_x - 1))
margin_x = (size[1] - W) // 2
else:
x_slice = slice(0, int(size[1]))
margin_x = 0
x_slice = slice(margin_x, margin_x + W)

return y_slice, x_slice
77 changes: 39 additions & 38 deletions tests/transforms_tests/image_tests/test_resize_contain.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,50 @@
from chainercv.transforms import resize_contain


@testing.parameterize(
{'fill': 128},
{'fill': (104, 117, 123)},
{'fill': np.random.uniform(255, size=(3, 1, 1))},
)
@testing.parameterize(*testing.product_dict(
[
{'size': (48, 96), 'scaled_size': (32, 64),
'y_offset': 8, 'x_offset': 16},
{'size': (16, 68), 'scaled_size': (16, 32),
'y_offset': 0, 'x_offset': 18},
{'size': (24, 16), 'scaled_size': (8, 16),
'y_offset': 8, 'x_offset': 0},
{'size': (47, 97), 'scaled_size': (32, 64),
'y_offset': 7, 'x_offset': 16},
],
[
{'fill': 128},
{'fill': (104, 117, 123)},
{'fill': np.random.uniform(255, size=(3, 1, 1))},
]
))
class TestResizeContain(unittest.TestCase):

def test_resize_contain(self):
img = np.random.uniform(size=(3, 32, 64))
H, W = 32, 64
img = np.random.uniform(255, size=(3, H, W))

out, param = resize_contain(
img, (48, 96), fill=self.fill, return_param=True)

np.testing.assert_array_equal(img, out[:, 8:40, 16:80])

if isinstance(self.fill, int):
fill = (self.fill,) * 3
else:
fill = self.fill
np.testing.assert_array_equal(
out[:, 0, 0], np.array(fill).flatten())

self.assertEqual(param['scaled_size'], (32, 64))
self.assertEqual(param['y_offset'], 8)
self.assertEqual(param['x_offset'], 16)

def test_resize_contain_canvas_small_x(self):
img = np.random.uniform(size=(3, 32, 64))

out, param = resize_contain(
img, (16, 68), fill=self.fill, return_param=True)
self.assertEqual(param['scaled_size'], (16, 32))
self.assertEqual(param['y_offset'], 0)
self.assertEqual(param['x_offset'], 18)

def test_resize_contain_canvas_small_y(self):
img = np.random.uniform(size=(3, 32, 64))

out, param = resize_contain(
img, (24, 16), fill=self.fill, return_param=True)
self.assertEqual(param['scaled_size'], (8, 16))
self.assertEqual(param['y_offset'], 8)
self.assertEqual(param['x_offset'], 0)
img, self.size, fill=self.fill, return_param=True)

self.assertEqual(param['scaled_size'], self.scaled_size)
self.assertEqual(param['y_offset'], self.y_offset)
self.assertEqual(param['x_offset'], self.x_offset)

if self.scaled_size == (H, W):
np.testing.assert_array_equal(
out[:,
self.y_offset:self.y_offset + H,
self.x_offset:self.x_offset + W],
img)

if self.y_offset > 0 or self.x_offset > 0:
if isinstance(self.fill, int):
fill = (self.fill,) * 3
else:
fill = self.fill
np.testing.assert_array_equal(
out[:, 0, 0], np.array(fill).flatten())


testing.run_module(__name__, __file__)