-
Notifications
You must be signed in to change notification settings - Fork 303
Add tile_images #422
Add tile_images #422
Changes from 5 commits
aa7706d
b4267c7
5e50d7c
3a29026
f42f371
111bdd0
81543b6
cc11241
27515ee
2a8df6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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.write_image import write_image # NOQA | ||
from chainercv.utils.image.tile_images import tile_images # NOQA |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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): Number of columns in a tile. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
pad (int): Amount of pad. Default 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about adding a note about the output size? |
||
|
||
""" | ||
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 + 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you add |
||
start_x = x * (W + pad) + pad // 2 + 1 | ||
tile[:, | ||
start_y: start_y + H, | ||
start_x: start_x + W] = imgs[k] | ||
k += 1 | ||
|
||
return tile |
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': [1, 2, 3] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about testing the case |
||
})) | ||
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 + 1 | ||
start_x_11 = W + self.pad + self.pad // 2 + 1 | ||
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__) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from chainercv.utils.image import tile_images
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tile_images
comes beforewrite_image
in alphabetical order.