linumpy.mosaic.grid#

Mosaic grid management and stitching utilities.

Classes#

MosaicGrid

Manage and process mosaic grid images.

Functions#

add_volume_to_mosaic(volume, pos, mosaic[, ...])

Add a single volume into a mosaic.

get_average_blending_weights(mask)

Compute the average blending weights over the mask in ND.

get_diffusion_blending_weights(fixed_mask[, ...])

Compute the diffusion blending (based on laplace equation) in 2D or 3D.

Module Contents#

class linumpy.mosaic.grid.MosaicGrid(image, tile_shape=(512, 512), overlap_fraction=0.2)[source]#

Manage and process mosaic grid images.

A mosaic grid is a 2D image containing all the tiles for a given mosaic, without any overlap. This class can be used for instance to apply processing to all tiles, to optimize the affine transform matrix describing the tile position, and to stitch the tiles together to obtain the reconstructed mosaic.

Note

This class can only deal with 2D mosaic grids for now. To generate a 2D mosaic grid from a collection of volumetric tiles for a given slice, you can use the linum_create_mosaic_grid_3d script.

Parameters:
tile_shape = (512, 512)[source]#
tile_size_x = 512[source]#
tile_size_y = 512[source]#
overlap_fraction = 0.2[source]#
blending_method = None[source]#
dtype[source]#
imin[source]#
imax[source]#
image[source]#
set_affine(overlap_fraction=0.2)[source]#

Set the affine matrix given an overlap fraction.

Parameters:

overlap_fraction (float, optional) – An overlap fraction between 0 and 1, by default 0.2.

Return type:

None

set_blending_method(method='none')[source]#

To set the blending method. Available methodes are ‘none’ and ‘average’, ‘diffusion’.

Parameters:

method (str)

Return type:

None

get_image()[source]#

To get the original image.

Return type:

numpy.ndarray

compute_mosaic_shape()[source]#

Compute the mosaic grid shape.

Return type:

None

get_tiles()[source]#

Return the tiles from the mosaic grid.

Returns:

Tiles and tile positions in the grid.

Return type:

tuple

get_neighbors_around_tile(x, y, neighborhood_type='N4')[source]#

Return all neighboring tiles around a given tile position.

Parameters:
Return type:

tuple

get_neighbors_list(neighborhood_type='N4')[source]#

Return a list of neighboring tiles.

Parameters:

neighborhood_type (str, optional) – Type of neighborhood to consider. ‘N4’ for horizontal and vertical neighbors, ‘N8’ to also consider diagonal neighbors.

Returns:

A list of neighbor pairs given by their grid position.

Return type:

list

Notes

This also updates the neighbors_list object property.

get_tile(x, y)[source]#

Extract a tile from the mosaic grid.

Parameters:
  • x (int) – x position within the mosaic grid.

  • y (int) – y position within the mosaic grid.

Returns:

2D tile.

Return type:

ndarray

set_tile(x, y, tile)[source]#

Set a tile from the mosaic grid.

Parameters:
  • x (int) – x position within the mosaic grid.

  • y (int) – y position within the mosaic grid.

  • tile (ndarray) – 2D tile.

Return type:

None

get_position(x, y)[source]#

Compute the cartesian position of a given tile using the internal affine transform.

Parameters:
  • x (int) – x position within the mosaic grid.

  • y (int) – y position within the mosaic grid.

Returns:

(2,) array containing the cartesian position of this tile (in pixel).

Return type:

ndarray

get_neighbor_tiles(n_id)[source]#

Extract the tiles for a given neighbor pair.

Parameters:

n_id (int) – The neighbor pair id.

Returns:

(2,) tuple containing each tile as a np.ndarray.

Return type:

tuple

get_neighbor_overlap_from_pos(p1, p2)[source]#

Compute the overlapping regions between two tiles given their positions.

Parameters:
Return type:

tuple

get_neighbor_overlap(n_id)[source]#

Extract the tile overlaps for a given neighbor pair.

Parameters:

n_id (int) – The neighbor pair id.

Returns:

(4,) tuple containing (overlap1, overlap2, overlap1_position, overlap2_position).

Return type:

tuple

crop_tiles(xlim=(0, -1), ylim=(0, -1))[source]#

Crop all tiles in the mosaic grid.

Parameters:
  • xlim (tuple, optional) – (2,) tuple containing the x-axis (row) cropping limits.

  • ylim (tuple, optional) – (2,) tuple containing the y-axis (col) cropping limits.

Return type:

None

Notes

This also resets the affine transform using the overlap_fraction.

get_stitched_image(blending_method='none')[source]#

Perform a 2D reconstruction of the mosaic grid.

Parameters:

blending_method (str, optional) – Blending method. Available: ‘none’, ‘average’, ‘diffusion’.

Returns:

Stitched mosaic.

Return type:

ndarray

Notes

The affine transform obtained from the overlap fraction or by the affine transform optimization is used for the reconstruction.

global_overlap_similarity(random_fraction=1.0, threshold=None)[source]#

Compute a global overlap similarity error across all tile pairs.

Parameters:
  • random_fraction (float)

  • threshold (float | None)

Return type:

float

optimize_overlap(step=0.01, omin=0.1, omax=0.5, display=False, random_fraction=1.0, threshold=None)[source]#

Use the similarity between every neighboring tiles to estimate the overlap fraction.

Parameters:
  • step (float, optional) – Overlap fraction steps used for the search.

  • omin (float, optional) – Minimum overlap fraction to consider.

  • omax (float, optional) – Maximum overlap fraction to consider.

  • display (bool, optional) – If set to true, the similarity curve will be displayed at the end of the optimization.

  • random_fraction (float, optional) – Fraction of tiles to use for the similarity computation, by default 1.0.

  • threshold (float, optional) – Threshold for the similarity computation, by default None.

Return type:

None

optimize_affine(initial_overlap=0.2, random_fraction=1.0, threshold=None)[source]#

Optimize the mosaic affine transform.

Parameters:
  • initial_overlap (float, optional) – Initial overlap fraction (between 0 and 1), by default 0.2.

  • random_fraction (float, optional) – Fraction of tiles to use for the similarity computation, by default 1.0.

  • threshold (float, optional) – Threshold for the similarity computation, by default None.

Return type:

None

linumpy.mosaic.grid.add_volume_to_mosaic(volume, pos, mosaic, blending_method='diffusion', factor=3, width=1.0)[source]#

Add a single volume into a mosaic.

Parameters:
  • volume (ndarray) – Volume to add to the mosaic

  • pos ((2,) tuple) – Position of this volume in mosaic coordinates (XY in pixel)

  • mosaic (ndarray) – Mosaic in which the volume is stitched

  • blending_method (str, optional) – Blending method to use (available : ‘diffusion’, ‘average’, ‘none’)

  • factor (int, optional) – Subsampling factor used by the diffusion blending method.

  • width (float, optional) – Blending transition width (between 0 and 1) used by the diffusion blending method, defaults to 1.0.

Returns:

The updated mosaic

Return type:

ndarray

linumpy.mosaic.grid.get_average_blending_weights(mask)[source]#

Compute the average blending weights over the mask in ND.

Parameters:

mask (ndarray) – Bool ndarray describing the overlap.

Returns:

Blending weights.

Return type:

ndarray

linumpy.mosaic.grid.get_diffusion_blending_weights(fixed_mask, moving_mask=None, factor=8, n_steps=500, convergence_threshold=0.0001, k=1)[source]#

Compute the diffusion blending (based on laplace equation) in 2D or 3D.

Parameters:
  • fixed_mask (ndarray) – Fixed volume mask to use as basis for the blending weights.

  • moving_mask (ndarray, optional) – Moving volume data mask. If none is given, it assumes that the whole volume contains data.

  • factor (int, optional) – Subsampling factor.

  • n_steps (int, optional) – Number of diffusion steps.

  • convergence_threshold (float, optional) – Convergence threshold used to end the diffusion.

  • k (int, optional) – Structural element radius used to find the boundary of the mask.

Returns:

ND blending weights.

Return type:

ndarray