Mask creation and manipulation

libertem.masks.background_subtraction(centerX, centerY, imageSizeX, imageSizeY, radius, radius_inner, antialiased=False)[source]
libertem.masks.balance(template)[source]

Accept a template with both positive and negative values and scale the negative part in such a way that the sum is zero.

This is useful to generate masks that return zero when applied to a uniform background or linear gradient.

libertem.masks.bounding_radius(centerX, centerY, imageSizeX, imageSizeY)[source]

Calculate a radius around centerX, centerY that covers the whole frame

libertem.masks.circular(centerX, centerY, imageSizeX, imageSizeY, radius, antialiased=False)[source]

Make a circular mask as a 2D array

Parameters
  • centreX (float) – Centre point of the mask.

  • centreY (float) – Centre point of the mask.

  • imageSizeX (int) – Size of the image to be masked.

  • imageSizeY (int) – Size of the image to be masked.

  • radius (float) – Radius of the mask.

Returns

Array with the shape (imageSizeX, imageSizeY) with the mask.

Return type

Numpy 2D Array

libertem.masks.gradient_x(imageSizeX, imageSizeY, dtype=<class 'numpy.float32'>)[source]
libertem.masks.gradient_y(imageSizeX, imageSizeY, dtype=<class 'numpy.float32'>)[source]
libertem.masks.is_sparse(a)[source]
libertem.masks.polar_map(centerX, centerY, imageSizeX, imageSizeY, stretchY=1.0, angle=0.0)[source]

Return a map of radius and angle.

The optional parameters stretchY and angle allow to stretch and rotate the coordinate system into an elliptical form. This is useful to generate modified input data for functions that generate a template as a function of radius and angle.

Parameters
  • centerX (float) – Center of the coordinate system in pixel coordinates

  • centerY (float) – Center of the coordinate system in pixel coordinates

  • imageSizeX (int) – Size of the map to generate in px

  • imageSizeY (int) – Size of the map to generate in px

  • stretchY (float, optional) – Stretch the radius elliptically by amount stretchY in direction angle in radians. angle = 0 means in Y direction.

  • angle (float, optional) – Stretch the radius elliptically by amount stretchY in direction angle in radians. angle = 0 means in Y direction.

Returns

Map of radius and angle of shape (imageSizeY, imageSizeX)

Return type

Tuple[numpy.ndarray, numpy.ndarray]

libertem.masks.radial_bins(centerX, centerY, imageSizeX, imageSizeY, radius=None, radius_inner=0, n_bins=None, normalize=False, use_sparse=None, dtype=None)[source]

Generate antialiased rings

libertem.masks.radial_gradient(centerX, centerY, imageSizeX, imageSizeY, radius, antialiased=False)[source]

Generate a linear radial gradient from 0 to 1 within radius

libertem.masks.radial_gradient_background_subtraction(r, r0, r_outer, delta=1)[source]

Generate a template with a linear radial gradient from 0 to 1 inside r0, linear transition region for antialiasing between [r0 - delta/2, r0 + delta/2[, and a negative ring with value -1 in [r0 + delta/2, r_outer].

The function accepts the radius for each pixel as a parameter so that a distorted version can be generated with the stretchY and angle parameters of polar_map().

Parameters
  • r (numpy.ndarray) – Map of radius for each pixel, typically 2D. This allows to work in distorted coordinate systems by assigning arbitrary radius values to each pixel. polar_map() can generate elliptical maps as an example.

  • r0 (float) – Inner radius to fill with a linear gradient in units of r

  • r_outer (float) – Outer radius of ring from r0 to fill with -1 in units of r

  • delta (float, optional) – Width of transition region between inner and outer in units of r with linear gradient for antialiasing or smoothening. Defaults to 1.

Returns

NumPy numpy.ndarray with the same shape and type of r with mask values assigned as described in the description.

Return type

numpy.ndarray

libertem.masks.rectangular(X, Y, Width, Height, imageSizeX, imageSizeY)[source]

Make a rectangular mask as a 2D array of bool. :param X: Centre point of the mask. :type X: Corner coordinates :param Y: Centre point of the mask. :type Y: Corner coordinates :param imageSizeX: Size of the image to be masked. :type imageSizeX: int :param imageSizeY: Size of the image to be masked. :type imageSizeY: int :param Width: :type Width: Width and Height of the rectangle :param Height: :type Height: Width and Height of the rectangle

Returns

Array with the shape (imageSizeX, imageSizeY) with the mask.

Return type

Numpy 2D Array

libertem.masks.ring(centerX, centerY, imageSizeX, imageSizeY, radius, radius_inner, antialiased=False)[source]

Make a ring mask as a double array.

Parameters
  • centreX (float) – Centre point of the mask.

  • centreY (float) – Centre point of the mask.

  • imageSizeX (int) – Size of the image to be masked.

  • imageSizeY (int) – Size of the image to be masked.

  • radius (float) – Outer radius of the ring.

  • radius_inner (float) – Inner radius of the ring.

Returns

Array with the shape (imageSizeX, imageSizeY) with the mask.

Return type

Numpy 2D Array

libertem.masks.sparse_circular_multi_stack(mask_index, centerX, centerY, imageSizeX, imageSizeY, radius)[source]
libertem.masks.sparse_template_multi_stack(mask_index, offsetX, offsetY, template, imageSizeX, imageSizeY)[source]

Stamp the template in a multi-mask 3D stack at the positions indicated by mask_index, offsetY, offsetX. The function clips the bounding box as necessary.

libertem.masks.to_dense(a)[source]
libertem.masks.to_sparse(a)[source]