mosaic_grid#

Classes#

MosaicGrid

This class is used to manage and process mosaic grid images. A mosaic grid is a 2D image containing all the tiles

Functions#

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

Add a single volume into a mosaic.

getAverageBlendingWeights(mask)

Computes the average blending weights over the mask in ND.

getDiffusionBlendingWeights(fixedMask[, movingMask, ...])

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

resampleITK(vol, newshape[, interpolator])

Resamples a volume / image using ITK.

Module Contents#

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

This class is used to 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 script-linum-create-mosaic-grid 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]#

Sets the affine matrix given an overlap fraction.

Parameters:

overlap_fraction (float, optional) – An overlap fraction between 0 and 1, defaults to 0.2

Return type:

None

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

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

Return type:

None

get_image()[source]#

To get the original image.

compute_mosaic_shape()[source]#

Compute the mosaic grid shape.

Return type:

None

get_tiles()[source]#

Returns the tiles from the mosaic grid.

Returns:

tuple containing the tiles and the tile positions in the grid.

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

Returns a list of neighboring tiles.

Parameters:

neighborhood_type (str) – 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.

Note

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:

numpy.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 (numpy.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:

numpy.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]#
get_neighbor_overlap(n_id)[source]#

Extract the tile overlaps for a given neighbor pair.

Parameters:

n_id – The neighbor pair id.

Returns:

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

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

Crop all tiles in the mosaic grid.

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

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

Return type:

None

Note

  • This also resets the affine transform using the overlap_fraction.

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

Performs a 2D reconstruction of the mosaic grid.

Parameters:

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

Returns:

Stitched mosaic.

Return type:

numpy.ndarray

Note

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]#
Parameters:
  • random_fraction (float)

  • threshold (float | None)

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

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

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

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

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

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

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), defaults to 0.2

Return type:

None

mosaic_grid.addVolumeToMosaic(volume, pos, mosaic, blendingMethod='diffusion', factor=3, width=1.0)[source]#

Add a single volume into a mosaic.

Parameters.#

volumendarray

Volume to add to the mosaic

pos(2,) tuple

Position of this volume in mosaic coordinates (XY in pixel)

mosaicndarray

Mosaic in which the volume is stitched

blendingMethodstr, optional

Blending method to use (available : ‘diffusion’, ‘average’, ‘none’)

factorint, optional

Subsampling factor used by the diffusion blending method.

widthfloat, optional

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

returns:

The updated mosaic

rtype:

ndarray

mosaic_grid.getAverageBlendingWeights(mask)[source]#

Computes the average blending weights over the mask in ND.

Parameters:

mask (ndarray) – Bool ndarray describing the overlap.

Returns:

Blending weights

Return type:

ndarray

mosaic_grid.getDiffusionBlendingWeights(fixedMask, movingMask=None, factor=8, nSteps=500.0, convergence_threshold=0.0001, k=1)[source]#

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

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

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

  • factor (int) – Subsampling factor

  • nSteps (int) – Number of diffusion steps.

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

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

Returns:

ND blending weights.

Return type:

numpy.ndarray

mosaic_grid.resampleITK(vol, newshape, interpolator='linear')[source]#

Resamples a volume / image using ITK.

Parameters:
  • vol (numpy.ndarray) – 2D/3D array to resample.

  • newshape (tuple) – New shape of the array, or resampling factor (if a single integer is given)

  • interpolation – Interpolation method to use. Available are: ‘NN’ (NearestNeighbor) and ‘linear’

  • interpolator (str)

Returns:

Resampled array

Return type:

numpy.ndarray