organoid_tracker.core.mask module

class organoid_tracker.core.mask.Mask(box: BoundingBox)

Bases: object

Class used for drawing and applying masks.

  • First, you set the bounds using one of the set_bounds methods. The bounds use position coords, not the coords of the raw image.

  • Second, you create the mask using add_from_labeled, dilate, etc. (or draw your mask directly on get_mask_array)

  • Third, you apply the mask to an image using

add_from_function(func: Callable[[ndarray, ndarray, ndarray], ndarray])

Calls the function (x, y, z) -> bool for all coordinates in the mask, and expands the mask to the coords for which the function returns True.

Note: the function is called with numpy arrays instead of single values, so that the function does not need to be called over and over, but just once.

Example for drawing a sphere of radius r around (0, 0, 0):

self.add_from_function(lambda x, y, z: x ** 2 + y ** 2 + z ** 2 <= r ** 2)

add_from_labeled(labeled_image: Image, label: int)

This adds all values of the given full size image with the given color (== label) to the mask. It is advisable to first crop this mask to a small region (for example to the bounding box of the desired label), as the mask array will be copied.

center_around(position: Position)

Centers this mask around the given position. So offset_x will become smaller than position.x and max_x will become larger, and position.x will be halfway. Same for the y and z axis.

One exception to offset_x < position.x: if size_x is 1, then offset_x will be int(position.x).

count_pixels() int

Returns the amount of pixels in the mask. Note: if the mask contains illegal values (values that are not 1 or 0), this will return an incorrect value. If the

create_masked_and_normalized_image(image: Image)

Create normalized subimage (floating point numbers from 0 to 1). Throws OutsideImageError when the mask is fully outside the image. Pixels outside the mask are set to NaN.

create_masked_image(image: Image) ndarray

Creates a subimage where all pixels outside the mask are set to 0.

create_masked_image_nan(image: Image) ndarray

Creates a subimage (float64) where all pixels outside the mask are set to NaN.

dilate_xy(iterations: int = 1)

Dilates the mask image in the xy direction.

get_mask_array() ndarray

Gets a 3D array to draw the mask on. Make sure to set appropriate bounds for this array first. Note that to save memory the coords of this array are offset by (offset_z, offset_y, offset_x), so pixel (0, 0, 0) in this array is actually pixel (offset_x, offset_y, offset_z) when applied to an image.

This array only contains the numbers 0 and 1. You are allowed to modify this array in order to draw a mask, but make sure to only use the values 0 and 1. You can also draw a mask using the add_from_ functions.

has_zero_volume() bool

If this mask has no volume, get_mask_array and related methods will fail.

property offset_x: int

The x coordinate that this mask starts at (position coords, not image coords).

property offset_y: int

The y coordinate that this mask starts at (position coords, not image coords).

property offset_z: int

The z coordinate that this mask starts at (position coords, not image coords).

set_bounds(box: BoundingBox)

Shrinks the bounding box to the given box. In this way, smaller arrays for the mask can be allocated. Setting a bounding box twice or setting it after get_mask_array has been called is not allowed.

set_bounds_around(x: float, y: float, z: float, padding_x: float, padding_y: float, padding_z: float)

Shrinks the bounding box of the shape that is going to be drawn. In this way, smaller arrays for the mask can be allocated. Setting a bounding box twice or setting it after get_mask_array has been called is not allowed.

If all three paddings are set to 0, an image of exactly 1 pixel will remain.

set_bounds_exact(min_x: float, min_y: float, min_z: float, max_x: float, max_y: float, max_z: float)

Shrinks the bounding box of the shape that is going to be drawn. In this way, smaller arrays for the mask can be allocated. Setting a bounding box twice or setting it after get_mask_array has been called is not allowed.

stamp_image(image: Image, stamp_value: Union[int, float])

Stamps (part of) this mask on the given image. Takes offset of this mask into account.

exception organoid_tracker.core.mask.OutsideImageError

Bases: Exception

Thrown when a mask falls completely outside an image.

organoid_tracker.core.mask.create_mask_for(image: Image) Mask

Creates a mask that will never expand beyond the size of the given image.