monai.transforms.utils module

apply_transform(transform, data)[source]

Transform data with transform. If data is a list or tuple, each item of data will be transformed and this method returns a list of outcomes. otherwise transform will be applied once with data as the argument.

Parameters
  • transform (callable) – a callable to be used to transform data

  • data (object) – an object to be transformed.

copypaste_arrays(src, dest, srccenter, destcenter, dims)[source]

Calculate the slices to copy a sliced area of array src into array dest. The area has dimensions dims (use 0 or None to copy everything in that dimension), the source area is centered at srccenter index in src and copied into area centered at destcenter in dest. The dimensions of the copied area will be clipped to fit within the source and destination arrays so a smaller area may be copied than expected. Return value is the tuples of slice objects indexing the copied area in src, and those indexing the copy area in dest.

Example

src = np.random.randint(0,10,(6,6))
dest = np.zeros_like(src)
srcslices, destslices = copypaste_arrays(src, dest, (3, 2),(2, 1),(3, 4))
dest[destslices] = src[srcslices]
print(src)
print(dest)

>>> [[9 5 6 6 9 6]
     [4 3 5 6 1 2]
     [0 7 3 2 4 1]
     [3 0 0 1 5 1]
     [9 4 7 1 8 2]
     [6 6 5 8 6 7]]
    [[0 0 0 0 0 0]
     [7 3 2 4 0 0]
     [0 0 1 5 0 0]
     [4 7 1 8 0 0]
     [0 0 0 0 0 0]
     [0 0 0 0 0 0]]
create_control_grid(spatial_shape, spacing, homogeneous=True, dtype=<class 'float'>)[source]

control grid with two additional point in each direction

create_grid(spatial_size, spacing=None, homogeneous=True, dtype=<class 'float'>)[source]

compute a spatial_size mesh.

Parameters
  • spatial_size (sequence of ints) – spatial size of the grid.

  • spacing (sequence of ints) – same len as spatial_size, defaults to 1.0 (dense grid).

  • homogeneous (bool) – whether to make homogeneous coordinates.

  • dtype (type) – output grid data type.

create_rotate(spatial_dims, radians)[source]

create a 2D or 3D rotation matrix

Parameters
  • spatial_dims (2|3) – spatial rank

  • radians (float or a sequence of floats) – rotation radians

  • spatial_dims == 3, the radians sequence corresponds to (when) –

  • in the 1st, 2nd, and 3rd dim respectively. (rotation) –

create_scale(spatial_dims, scaling_factor)[source]

create a scaling matrix :param spatial_dims: spatial rank :type spatial_dims: int :param scaling_factor: scaling factors, defaults to 1. :type scaling_factor: floats

create_shear(spatial_dims, coefs)[source]

create a shearing matrix :param spatial_dims: spatial rank :type spatial_dims: int :param coefs: shearing factors, defaults to 0. :type coefs: floats

create_translate(spatial_dims, shift)[source]

create a translation matrix :param spatial_dims: spatial rank :type spatial_dims: int :param shift: translate factors, defaults to 0. :type shift: floats

generate_pos_neg_label_crop_centers(label, size, num_samples, pos_ratio, image=None, image_threshold=0, rand_state=<module 'numpy.random' from '/home/docs/checkouts/readthedocs.org/user_builds/monai/envs/0.1.0/lib/python3.7/site-packages/numpy/random/__init__.py'>)[source]

Generate valid sample locations based on image with option for specifying foreground ratio Valid: samples sitting entirely within image, expected input shape: [C, H, W, D] or [C, H, W]

Parameters
  • label (numpy.ndarray) – use the label data to get the foreground/background information.

  • size (list or tuple) – size of the ROIs to be sampled.

  • num_samples (int) – total sample centers to be generated.

  • pos_ratio (float) – ratio of total locations generated that have center being foreground.

  • image (numpy.ndarray) – if image is not None, use label = 0 & image > image_threshold to select background. so the crop center will only exist on valid image area.

  • image_threshold (int or float) – if enabled image_key, use image > image_threshold to determine the valid image content area.

  • rand_state (random.RandomState) – numpy randomState object to align with other modules.

generate_spatial_bounding_box(img, select_fn=<function <lambda>>, channel_indexes=None, margin=0)[source]

generate the spatial bounding box of foreground in the image with start-end positions. Users can define arbitrary function to select expected foreground from the whole image or specified channels. And it can also add margin to every dim of the bounding box.

Parameters
  • img (ndarrary) – source image to generate bounding box from.

  • select_fn (Callable) – function to select expected foreground, default is to select values > 0.

  • channel_indexes (int, tuple or list) – if defined, select foregound only on the specified channels of image. if None, select foreground on the whole image.

  • margin (int) – add margin to all dims of the bounding box.

img_bounds(img)[source]

Returns the minimum and maximum indices of non-zero lines in axis 0 of img, followed by that for axis 1.

in_bounds(x, y, margin, maxx, maxy)[source]

Returns True if (x,y) is within the rectangle (margin, margin, maxx-margin, maxy-margin).

is_empty(img)[source]

Returns True if img is empty, that is its maximum value is not greater than its minimum.

one_hot(labels, num_classes)[source]

Converts label image labels to a one-hot vector with num_classes number of channels as last dimension.

rand_choice(prob=0.5)[source]

Returns True if a randomly chosen number is less than or equal to prob, by default this is a 50/50 chance.

rescale_array(arr, minv=0.0, maxv=1.0, dtype=<class 'numpy.float32'>)[source]

Rescale the values of numpy array arr to be from minv to maxv.

rescale_array_int_max(arr, dtype=<class 'numpy.uint16'>)[source]

Rescale the array arr to be between the minimum and maximum values of the type dtype.

rescale_instance_array(arr, minv=0.0, maxv=1.0, dtype=<class 'numpy.float32'>)[source]

Rescale each array slice along the first dimension of arr independently.

resize_center(img, *resize_dims, fill_value=0)[source]

Resize img by cropping or expanding the image from the center. The resize_dims values are the output dimensions (or None to use original dimension of img). If a dimension is smaller than that of img then the result will be cropped and if larger padded with zeros, in both cases this is done relative to the center of img. The result is a new image with the specified dimensions and values from img copied into its center.

zero_margins(img, margin)[source]

Returns True if the values within margin indices of the edges of img in dimensions 1 and 2 are 0.