MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / CropForegroundd

Class CropForegroundd

monai/transforms/croppad/dictionary.py:834–932  ·  view source on GitHub ↗

Dictionary-based version :py:class:`monai.transforms.CropForeground`. Crop only the foreground object of the expected images. The typical usage is to help training and evaluation if the valid part is small in the whole medical image. The valid part can be determined by any field in

Source from the content-addressed store, hash-verified

832
833
834class CropForegroundd(Cropd):
835 """
836 Dictionary-based version :py:class:`monai.transforms.CropForeground`.
837 Crop only the foreground object of the expected images.
838 The typical usage is to help training and evaluation if the valid part is small in the whole medical image.
839 The valid part can be determined by any field in the data with `source_key`, for example:
840 - Select values > 0 in image field as the foreground and crop on all fields specified by `keys`.
841 - Select label = 3 in label field as the foreground to crop on all fields specified by `keys`.
842 - Select label > 0 in the third channel of a One-Hot label field as the foreground to crop all `keys` fields.
843 Users can define arbitrary function to select expected foreground from the whole source image or specified
844 channels. And it can also add margin to every dim of the bounding box of foreground object.
845
846 This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
847 for more information.
848 """
849
850 def __init__(
851 self,
852 keys: KeysCollection,
853 source_key: str,
854 select_fn: Callable = is_positive,
855 channel_indices: IndexSelection | None = None,
856 margin: Sequence[int] | int = 0,
857 allow_smaller: bool = False,
858 k_divisible: Sequence[int] | int = 1,
859 mode: SequenceStr = PytorchPadMode.CONSTANT,
860 start_coord_key: str | None = "foreground_start_coord",
861 end_coord_key: str | None = "foreground_end_coord",
862 allow_missing_keys: bool = False,
863 lazy: bool = False,
864 **pad_kwargs,
865 ) -> None:
866 """
867 Args:
868 keys: keys of the corresponding items to be transformed.
869 See also: :py:class:`monai.transforms.compose.MapTransform`
870 source_key: data source to generate the bounding box of foreground, can be image or label, etc.
871 select_fn: function to select expected foreground, default is to select values > 0.
872 channel_indices: if defined, select foreground only on the specified channels
873 of image. if None, select foreground on the whole image.
874 margin: add margin value to spatial dims of the bounding box, if only 1 value provided, use it for all dims.
875 allow_smaller: when computing box size with `margin`, whether to allow the image edges to be smaller than the
876 final box edges. If `False`, part of a padded output box might be outside of the original image, if `True`,
877 the image edges will be used as the box edges. Default to `False`.
878 The default value is changed from `True` to `False` in v1.5.0.
879 k_divisible: make each spatial dimension to be divisible by k, default to 1.
880 if `k_divisible` is an int, the same `k` be applied to all the input spatial dimensions.
881 mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``,
882 ``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``}
883 available modes for PyTorch Tensor: {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}.
884 One of the listed string values or a user supplied function. Defaults to ``"constant"``.
885 See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
886 https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
887 it also can be a sequence of string, each element corresponds to a key in ``keys``.
888 start_coord_key: key to record the start coordinate of spatial bounding box for foreground.
889 end_coord_key: key to record the end coordinate of spatial bounding box for foreground.
890 allow_missing_keys: don&#x27;t raise exception if key is missing.
891 lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False.

Callers 6

test_train_timingMethod · 0.90
test_valueMethod · 0.90
test_pending_opsMethod · 0.90
test_inverse.pyFile · 0.90

Calls

no outgoing calls

Tested by 5

test_train_timingMethod · 0.72
test_valueMethod · 0.72
test_pending_opsMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…