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

Commit

Permalink
fix transforms/bbox
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyu2172 committed Jun 6, 2017
1 parent a6d3d03 commit eeaf69c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 40 deletions.
26 changes: 13 additions & 13 deletions chainercv/transforms/bbox/flip_bbox.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
def flip_bbox(bbox, size, x_flip=False, y_flip=False):
def flip_bbox(bbox, size, y_flip=False, x_flip=False):
"""Flip bounding boxes accordingly.
The bounding boxes are expected to be packed into a two dimensional
tensor of shape :math:`(R, 4)`, where :math:`R` is the number of
bounding boxes in the image. The second axis represents attributes of
the bounding box. They are :obj:`(x_min, y_min, x_max, y_max)`,
the bounding box. They are :obj:`(y_min, x_min, y_max, x_max)`,
where the four attributes are coordinates of the bottom left and the
top right vertices.
Expand All @@ -13,10 +13,10 @@ def flip_bbox(bbox, size, x_flip=False, y_flip=False):
:math:`R` is the number of bounding boxes.
size (tuple): A tuple of length 2. The height and the width
of the image before resized.
x_flip (bool): Flip bounding box according to a horizontal flip of
an image.
y_flip (bool): Flip bounding box according to a vertical flip of
an image.
x_flip (bool): Flip bounding box according to a horizontal flip of
an image.
Returns:
~numpy.ndarray:
Expand All @@ -25,14 +25,14 @@ def flip_bbox(bbox, size, x_flip=False, y_flip=False):
"""
H, W = size
bbox = bbox.copy()
if x_flip:
x_max = W - 1 - bbox[:, 0]
x_min = W - 1 - bbox[:, 2]
bbox[:, 0] = x_min
bbox[:, 2] = x_max
if y_flip:
y_max = H - 1 - bbox[:, 1]
y_min = H - 1 - bbox[:, 3]
bbox[:, 1] = y_min
bbox[:, 3] = y_max
y_max = H - 1 - bbox[:, 0]
y_min = H - 1 - bbox[:, 2]
bbox[:, 0] = y_min
bbox[:, 2] = y_max
if x_flip:
x_max = W - 1 - bbox[:, 1]
x_min = W - 1 - bbox[:, 3]
bbox[:, 1] = x_min
bbox[:, 3] = x_max
return bbox
12 changes: 6 additions & 6 deletions chainercv/transforms/bbox/resize_bbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def resize_bbox(bbox, in_size, out_size):
The bounding boxes are expected to be packed into a two dimensional
tensor of shape :math:`(R, 4)`, where :math:`R` is the number of
bounding boxes in the image. The second axis represents attributes of
the bounding box. They are :obj:`(x_min, y_min, x_max, y_max)`,
the bounding box. They are :obj:`(y_min, x_min, y_max, x_max)`,
where the four attributes are coordinates of the bottom left and the
top right vertices.
Expand All @@ -22,10 +22,10 @@ def resize_bbox(bbox, in_size, out_size):
"""
bbox = bbox.copy()
x_scale = float(out_size[1]) / in_size[1]
y_scale = float(out_size[0]) / in_size[0]
bbox[:, 0] = x_scale * bbox[:, 0]
bbox[:, 2] = x_scale * bbox[:, 2]
bbox[:, 1] = y_scale * bbox[:, 1]
bbox[:, 3] = y_scale * bbox[:, 3]
x_scale = float(out_size[1]) / in_size[1]
bbox[:, 0] = y_scale * bbox[:, 0]
bbox[:, 2] = y_scale * bbox[:, 2]
bbox[:, 1] = x_scale * bbox[:, 1]
bbox[:, 3] = x_scale * bbox[:, 3]
return bbox
13 changes: 7 additions & 6 deletions chainercv/transforms/bbox/translate_bbox.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
def translate_bbox(bbox, x_offset=0, y_offset=0):
def translate_bbox(bbox, y_offset=0, x_offset=0):
"""Translate bounding boxes.
This method is mainly used together with image transforms, such as padding
and cropping, which translates the left top point of the image from
coordinate :math:`(0, 0)` to coordinate :math:`(x\_offset, y\_offset)`.
coordinate :math:`(0, 0)` to coordinate
:math:`(y, x) = (y\_offset, x\_offset)`.
The bounding boxes are expected to be packed into a two dimensional
tensor of shape :math:`(R, 4)`, where :math:`R` is the number of
bounding boxes in the image. The second axis represents attributes of
the bounding box. They are :obj:`(x_min, y_min, x_max, y_max)`,
the bounding box. They are :obj:`(y_min, x_min, y_max, x_max)`,
where the four attributes are coordinates of the bottom left and the
top right vertices.
Args:
bbox (~numpy.ndarray): Bounding boxes to be transformed. The shape is
:math:`(R, 4)`. :math:`R` is the number of bounding boxes.
x_offset (int or float): The offset along x axis.
y_offset (int or float): The offset along y axis.
x_offset (int or float): The offset along x axis.
Returns:
~numpy.ndarray:
Expand All @@ -25,7 +26,7 @@ def translate_bbox(bbox, x_offset=0, y_offset=0):
"""

out_bbox = bbox.copy()
out_bbox[:, :2] += (x_offset, y_offset)
out_bbox[:, 2:] += (x_offset, y_offset)
out_bbox[:, :2] += (y_offset, x_offset)
out_bbox[:, 2:] += (y_offset, x_offset)

return out_bbox
12 changes: 6 additions & 6 deletions tests/transforms_tests/bbox_tests/test_flip_bbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ def test_flip_bbox(self):
bbox = np.random.uniform(
low=0., high=32., size=(10, 4))

out = flip_bbox(bbox, size=(34, 32), x_flip=True)
out = flip_bbox(bbox, size=(34, 32), y_flip=True)
bbox_expected = bbox.copy()
bbox_expected[:, 0] = 31 - bbox[:, 2]
bbox_expected[:, 2] = 31 - bbox[:, 0]
bbox_expected[:, 0] = 33 - bbox[:, 2]
bbox_expected[:, 2] = 33 - bbox[:, 0]
np.testing.assert_equal(out, bbox_expected)

out = flip_bbox(bbox, size=(34, 32), y_flip=True)
out = flip_bbox(bbox, size=(34, 32), x_flip=True)
bbox_expected = bbox.copy()
bbox_expected[:, 1] = 33 - bbox[:, 3]
bbox_expected[:, 3] = 33 - bbox[:, 1]
bbox_expected[:, 1] = 31 - bbox[:, 3]
bbox_expected[:, 3] = 31 - bbox[:, 1]
np.testing.assert_equal(out, bbox_expected)


Expand Down
8 changes: 4 additions & 4 deletions tests/transforms_tests/bbox_tests/test_resize_bbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def test_resize_bbox(self):

out = resize_bbox(bbox, in_size=(32, 32), out_size=(64, 128))
bbox_expected = bbox.copy()
bbox_expected[:, 0] = bbox[:, 0] * 4
bbox_expected[:, 1] = bbox[:, 1] * 2
bbox_expected[:, 2] = bbox[:, 2] * 4
bbox_expected[:, 3] = bbox[:, 3] * 2
bbox_expected[:, 0] = bbox[:, 0] * 2
bbox_expected[:, 1] = bbox[:, 1] * 4
bbox_expected[:, 2] = bbox[:, 2] * 2
bbox_expected[:, 3] = bbox[:, 3] * 4
np.testing.assert_equal(out, bbox_expected)


Expand Down
10 changes: 5 additions & 5 deletions tests/transforms_tests/bbox_tests/test_translate_bbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ def test_translate_bbox(self):
bbox = np.random.uniform(
low=0., high=32., size=(10, 4))

out = translate_bbox(bbox, x_offset=3, y_offset=5)
out = translate_bbox(bbox, y_offset=5, x_offset=3)
bbox_expected = np.empty_like(bbox)
bbox_expected[:, 0] = bbox[:, 0] + 3
bbox_expected[:, 1] = bbox[:, 1] + 5
bbox_expected[:, 2] = bbox[:, 2] + 3
bbox_expected[:, 3] = bbox[:, 3] + 5
bbox_expected[:, 0] = bbox[:, 0] + 5
bbox_expected[:, 1] = bbox[:, 1] + 3
bbox_expected[:, 2] = bbox[:, 2] + 5
bbox_expected[:, 3] = bbox[:, 3] + 3
np.testing.assert_equal(out, bbox_expected)


Expand Down

0 comments on commit eeaf69c

Please sign in to comment.