Commit 80018023 authored by Wallenfang, Nils's avatar Wallenfang, Nils

use offset matrix for placing the tiles

parent 960fed7c
......@@ -142,11 +142,11 @@ def insert_labels(preview, xdim, ydim, overlap):
return np.asarray(img)
def calculate_offsets(x_tile, y_tile, nx, ny, overlap, x_res, y_res):
def calculate_offsets(nx, ny, overlap, x_res, y_res):
"""
Calculate the insertion coordinates of a tile with indices x_tile, y_tile with respect to the overlap.
Calculate the insertion coordinates of every with indices with respect to the overlap.
Returns:
Matrices x_offsets and y_offsets containing the respective coordinate for each tile.
"""
x_offsets = np.array(nx, ny)
y_offsets = np.array(nx, ny)
......@@ -186,7 +186,7 @@ class Preview:
self.suffix = ''
def generate(self, overlap=0.28, scaling=None, image_index=0, origin=IndexOrigin.BOTTOM_RIGHT,
def generate(self, overlap=0.28, scaling=None, image_index=0,
on_tile_done=None, insert_index_labels=True):
"""
Creates a stitched image of the tiffs located in tiff_path.
......@@ -200,9 +200,6 @@ class Preview:
if set to None. Defaults to None.
image_index(int, optional):
Image index that shall be processed for each tile. Defaults to 0.
origin(IndexOrigin):
Determines the coordinate origin of the tiles (where (0, 0) is located).
Defaults to TOP_LEFT.
on_tile_done( (x:int, y:int) -> None):
Callback that gets called upon insertion of a scaled tile into preview
image. X and y indices are passed as integer arguments. Defaults to
......@@ -222,14 +219,18 @@ class Preview:
# resolution of cropped tile (without overlapping region)
x_cropped_res, y_cropped_res = int(((1 - overlap) * self.tile_x) / (scaling/2) + 0.5), int(
((1 - overlap) * self.tile_x) / (scaling/2) + 0.5)
# x_noncropped_res =
((1 - overlap) * self.tile_y) / (scaling/2) + 0.5)
# calculate dimensions of complete preview
# note that outer tiles are not cropped on one side
# TODO check if this works if section consists of a single tile
preview_x, preview_y = (nx) * x_cropped_res, (ny) * y_cropped_res
preview_x = (nx-1) * x_cropped_res + int(self.tile_x/(scaling/2) + 0.5)
preview_y = (ny-1) * y_cropped_res + int(self.tile_y/(scaling/2) + 0.5)
preview = np.zeros((preview_x, preview_y))
x_offsets, y_offsets = calculate_offsets(nx, ny, overlap, x_cropped_res, preview_x, preview_y)
for x in range(nx):
for y in range(ny):
img_path = f"{self.measurement_path}/{x:02d}_{y:02d}"
......@@ -246,21 +247,10 @@ class Preview:
interpolation=cv2.INTER_NEAREST)
# calculate offsets of current tile
if origin == IndexOrigin.BOTTOM_RIGHT:
# note that the coordinate origin of the tiles is bottom-right, which is why the
# tile indices are subtracted from the total number of tiles (nx / ny)
x_from = (nx - (x + 1)) * x_cropped_res
x_to = (nx - x) * x_cropped_res if x != 0 else None
y_from = (ny - (y + 1)) * y_cropped_res
y_to = (ny - y) * y_cropped_res if y != 0 else None
elif origin == IndexOrigin.TOP_LEFT:
# 'normal origin', top left is 0,0
x_from = x * x_cropped_res
x_to = (x + 1) * x_cropped_res
y_from = y * y_cropped_res
y_to = (y + 1) * y_cropped_res
else:
raise ValueError('Invalid origin passed, see enum IndexOrigin.')
x_from = x_offsets[x, y]
x_to = x_from + img_rescaled.shape[0] if x != 0 else None
y_from = y_offsets[x, y]
y_to = y_from + img_rescaled.shape[1] if y != 0 else None
preview[x_from:x_to, y_from:y_to] = img_rescaled
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment