Created
May 10, 2016 21:57
-
-
Save vfdev-5/25efc1309d93edf346c40f9cde46ce2a to your computer and use it in GitHub Desktop.
Image tiler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python2 | |
import numpy as np | |
from tiler import ImageTiler | |
w = 7 | |
h = 9 | |
data = np.arange(0, w*h).reshape((h, w)) | |
print data | |
ts = 5 | |
ovrl = 2 | |
tiles = ImageTiler(data, (ts, ts), ovrl) | |
print tiles.nx, tiles.ny | |
for tile, x, y in tiles: | |
print tile, x, y | |
test_data = data[y:y+tile.shape[0], x:x+tile.shape[1]] | |
s = np.sum(np.abs(test_data[:] - tile[:])) | |
assert s == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python2 | |
import numpy as np | |
class ImageTiler: | |
def __init__(self, data, tile_size=(256, 256), overlapping=64): | |
assert isinstance(data, np.ndarray), "Data should be a numpy array" | |
assert 2*overlapping < min(tile_size[0], tile_size[1]), "2 * Overlapping should be less than tile size" | |
self.data = data | |
self.tile_size = tile_size | |
self.overlapping = overlapping | |
self.nx = ImageTiler._compute_nb_tiles(data.shape[1], tile_size[0], overlapping) | |
self.ny = ImageTiler._compute_nb_tiles(data.shape[0], tile_size[1], overlapping) | |
self._index = 0 | |
self._maxIndex = self.nx * self.ny | |
@staticmethod | |
def _compute_nb_tiles(image_size, tile_size, overlapping): | |
return int(np.ceil((image_size + overlapping)*1.0/(tile_size - overlapping))) | |
def __iter__(self): | |
return self | |
def next(self): | |
if self._index < 0 or self._index >= self._maxIndex: | |
raise StopIteration() | |
image_width = self.data.shape[1] | |
image_height = self.data.shape[0] | |
x_tile_index = self._index % self.nx | |
y_tile_index = int(np.floor(self._index * 1.0 / self.nx)) | |
x = x_tile_index * (self.tile_size[0] - self.overlapping) - self.overlapping | |
y = y_tile_index * (self.tile_size[1] - self.overlapping) - self.overlapping | |
x_tile_size = self.tile_size[0] if x + self.tile_size[0] <= image_width else image_width - x | |
if x_tile_index == 0: | |
x = 0 | |
x_tile_size = self.tile_size[0] - self.overlapping | |
y_tile_size = self.tile_size[1] if y + self.tile_size[1] <= image_height else image_height - y | |
if y_tile_index == 0: | |
y = 0 | |
y_tile_size = self.tile_size[0] - self.overlapping | |
extent = [x, y, x_tile_size, y_tile_size] | |
data = self.data[extent[1]:extent[1]+extent[3], extent[0]:extent[0]+extent[2]] | |
# print "{}, ({}, {}) | extent={}".format(self._index, x_tile_index, y_tile_index, extent) | |
self._index += 1 | |
return data, x, y | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment