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

Add tile_images #422

Merged
merged 10 commits into from
Oct 5, 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
1 change: 1 addition & 0 deletions chainercv/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from chainercv.utils.download import download_model # NOQA
from chainercv.utils.download import extractall # NOQA
from chainercv.utils.image import read_image # NOQA
from chainercv.utils.image import tile_images # NOQA
from chainercv.utils.image import write_image # NOQA
from chainercv.utils.iterator import apply_prediction_to_iterator # NOQA
from chainercv.utils.iterator import unzip # NOQA
Expand Down
1 change: 1 addition & 0 deletions chainercv/utils/image/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from chainercv.utils.image.read_image import read_image # NOQA
from chainercv.utils.image.tile_images import tile_images # NOQA
from chainercv.utils.image.write_image import write_image # NOQA
49 changes: 49 additions & 0 deletions chainercv/utils/image/tile_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from __future__ import division

import math
import numpy as np


def tile_images(imgs, n_col, pad=2, fill=0):
"""Make a tile of images

Args:
imgs (numpy.ndarray): A batch of images whose shape is BCHW.
n_col (int): The number of columns in a tile.
pad (int): Amount of pad. The default value is 2.
fill (float, tuple or ~numpy.ndarray): The value of padded pixels.
If it is :class:`numpy.ndarray`,
its shape should be :math:`(C, 1, 1)`,
where :math:`C` is the number of channels of :obj:`img`.

Returns:
~numpy.ndarray:
An image array in CHW format.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding a note about the output size?
The size of this image is :math:((H + pad) * \lceil B / n_col \rceil, (W + pad) * n_col)

The size of this image is
:math:`((H + pad) \\times \\lceil B / n_{n_{col}} \\rceil,
(W + pad) \\times n_{col})`.

"""
B, C, H, W = imgs.shape
n_col = min(n_col, B)
n_row = int(math.ceil(B / n_col))

shape = (C,
(H + pad) * n_row,
(W + pad) * n_col)
tile = np.empty(shape, dtype=imgs.dtype)
tile[:] = np.array(fill).reshape((-1, 1, 1))

k = 0
for y in range(n_row):
for x in range(n_col):
if k >= B:
break
start_y = y * (H + pad) + pad // 2
start_x = x * (W + pad) + pad // 2
tile[:,
start_y: start_y + H,
start_x: start_x + W] = imgs[k]
k += 1

return tile
4 changes: 4 additions & 0 deletions docs/source/reference/utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ read_image
~~~~~~~~~~
.. autofunction:: read_image

tile_images
~~~~~~~~~~~
.. autofunction:: tile_images

write_image
~~~~~~~~~~~
.. autofunction:: write_image
Expand Down
37 changes: 37 additions & 0 deletions tests/utils_tests/image_tests/test_tile_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from __future__ import division
import math
import numpy as np
import unittest

from chainer import testing

from chainercv.utils import tile_images


@testing.parameterize(*testing.product({
'fill': [128, (104, 117, 123), np.random.uniform(255, size=(3, 1, 1))],
'pad': [0, 1, 2, 3]
}))
class TestTileImages(unittest.TestCase):

def test_tile_images(self):
B = np.random.randint(10, 20)
n_col = np.random.randint(2, 5)
H = 30
W = 40

imgs = np.random.uniform(255, size=(B, 3, H, W))
tile = tile_images(imgs, n_col, self.pad, fill=self.fill)

n_row = int(math.ceil(B / n_col))
self.assertTrue(n_col >= 1 and n_row >= 1)
start_y_11 = H + self.pad + self.pad // 2
start_x_11 = W + self.pad + self.pad // 2
tile_11 = tile[:,
start_y_11:start_y_11 + H,
start_x_11:start_x_11 + W]

np.testing.assert_equal(tile_11, imgs[(n_col - 1) + 2])


testing.run_module(__name__, __file__)