Skip to content

Commit

Permalink
Fixed image generator tile renderer/viewing
Browse files Browse the repository at this point in the history
  • Loading branch information
folterj committed Dec 3, 2023
1 parent df2e927 commit 31e0f04
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions OmeSliCC/ImageGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ def __init__(self, size, tile_size, dtype=np.uint8, seed=None):
self.tile_size = tile_size
self.dtype = dtype

ranges = np.flip(np.ceil(np.divide(self.size, self.tile_size)).astype(int))
self.tile_indices = list(np.ndindex(tuple(ranges)))

if seed is not None:
np.random.seed(seed)

Expand All @@ -133,9 +136,8 @@ def get_tiles(self):
max_val = 2 ** (8 * np.dtype(dtype).itemsize) - 1
else:
max_val = 1
ranges = np.flip(np.ceil(np.divide(self.size, self.tile_size)).astype(int))
# flip: cycle over indices in x, y, z order using range = [z, y, x]
for indices in tqdm(list(np.ndindex(tuple(ranges)))):
for indices in tqdm(self.tile_indices):
self.range0 = np.flip(indices) * tile_size
self.range1 = np.min([self.range0 + self.tile_size, self.size], 0)
shape = list(reversed(self.range1 - self.range0))
Expand Down Expand Up @@ -165,23 +167,35 @@ def save_tiff(filename, data, shape=None, dtype=None, tile_size=None, bigtiff=No
compression=compression)


def render_image(data, shape, dtype):
#for slice1, tile in tqdm(data):
# image[slice1] = tile

image = np.zeros(shape, dtype=dtype)
position = np.array([0] * len(shape))
for tile in data:
image[position: position + tile.shape] = tile
position += tile.shape
# TODO: increase/carry-over (x,) y, z
image = image.reshape(shape)
def render_image(image_generator):
data = image_generator.get_tiles()
dtype = image_generator.dtype
tile_indices = image_generator.tile_indices
shape = np.flip(image_generator.size)
multi_dimensional = (len(shape) > 2)

if len(shape) <= 3 and shape[-1] <= 4:
show_image(image)
if multi_dimensional:
shape1 = list(shape)[1:] + [3]
else:
i = shape[3] // 2 + 2
show_image(image[:, :, i, :])
shape1 = list(shape) + [3]
image = np.zeros(shape1, dtype=dtype)
first = True
for indices, tile in zip(tile_indices, data):
tile_shape = tile.shape[:-1]
range0 = np.multiply(indices, tile_shape)
range1 = np.min([range0 + tile_shape, shape], 0)

# break after first z
if not first and np.all(range0[-2:] == [0, 0]):
break

if multi_dimensional:
image[range0[1]: range1[1], range0[2]: range1[2], :] = tile[0, ...]
else:
image[range0[0]: range1[0], range0[1]: range1[1], :] = tile
first = False

show_image(image)


def show_image(image):
Expand Down Expand Up @@ -210,5 +224,4 @@ def show_image(image):
save_tiff(path, data, shape, dtype, tile_size=tile_shape, ome=True)
print('save done')

data = image_generator.get_tiles()
render_image(data, shape, dtype)
render_image(image_generator)

0 comments on commit 31e0f04

Please sign in to comment.