diff --git a/monai/transforms/croppad/array.py b/monai/transforms/croppad/array.py index 7557565b83..d4bab741eb 100644 --- a/monai/transforms/croppad/array.py +++ b/monai/transforms/croppad/array.py @@ -58,8 +58,11 @@ class SpatialPad(Transform): for additional details. Args: - spatial_size: the spatial size of output data after padding. + c: the spatial size of output data after padding, if a dimension of the input + data size is bigger than the pad size, will not pad that dimension. If its components have non-positive values, the corresponding size of input image will be used (no padding). + for example: if the spatial size of input data is [30, 30, 30] and `spatial_size=[32, 25, -1]`, + the spatial size of output data will be [32, 30, 30]. method: {``"symmetric"``, ``"end"``} Pad image symmetric on every side or only pad at the end sides. Defaults to ``"symmetric"``. mode: {``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``, ``"mean"``, @@ -214,6 +217,9 @@ def __call__(self, img: np.ndarray, mode: Optional[Union[NumpyPadMode, str]] = N class SpatialCrop(Transform): """ General purpose cropper to produce sub-volume region of interest (ROI). + If a dimension of the expected ROI size is bigger than the input image size, will not crop that dimension. + So the cropped result may be smaller than the expected ROI, and the cropped results of several images may + not have exactly the same shape. It can support to crop ND spatial (channel-first) data. The cropped region can be parameterised in various ways: @@ -233,9 +239,11 @@ def __init__( """ Args: roi_center: voxel coordinates for center of the crop ROI. - roi_size: size of the crop ROI. + roi_size: size of the crop ROI, if a dimension of ROI size is bigger than image size, + will not crop that dimension of the image. roi_start: voxel coordinates for start of the crop ROI. - roi_end: voxel coordinates for end of the crop ROI. + roi_end: voxel coordinates for end of the crop ROI, if a coordinate is out of image, + use the end coordinate of image. roi_slices: list of slices for each of the spatial dimensions. """ if roi_slices: @@ -272,10 +280,16 @@ def __call__(self, img: Union[np.ndarray, torch.Tensor]): class CenterSpatialCrop(Transform): """ Crop at the center of image with specified ROI size. + If a dimension of the expected ROI size is bigger than the input image size, will not crop that dimension. + So the cropped result may be smaller than the expected ROI, and the cropped results of several images may + not have exactly the same shape. Args: roi_size: the spatial size of the crop region e.g. [224,224,128] + if a dimension of ROI size is bigger than image size, will not crop that dimension of the image. If its components have non-positive values, the corresponding size of input image will be used. + for example: if the spatial size of input data is [40, 40, 40] and `roi_size=[32, 64, -1]`, + the spatial size of output data will be [32, 40, 40]. """ def __init__(self, roi_size: Union[Sequence[int], int]) -> None: @@ -318,10 +332,17 @@ class RandSpatialCrop(Randomizable, Transform): Crop image with random size or specific size ROI. It can crop at a random position as center or at the image center. And allows to set the minimum and maximum size to limit the randomly generated ROI. + Note: even `random_size=False`, if a dimension of the expected ROI size is bigger than the input image size, + will not crop that dimension. So the cropped result may be smaller than the expected ROI, and the cropped results + of several images may not have exactly the same shape. + Args: roi_size: if `random_size` is True, it specifies the minimum crop region. if `random_size` is False, it specifies the expected ROI size to crop. e.g. [224, 224, 128] + if a dimension of ROI size is bigger than image size, will not crop that dimension of the image. If its components have non-positive values, the corresponding size of input image will be used. + for example: if the spatial size of input data is [40, 40, 40] and `roi_size=[32, 64, -1]`, + the spatial size of output data will be [32, 40, 40]. max_roi_size: if `random_size` is True and `roi_size` specifies the min crop region size, `max_roi_size` can specify the max crop region size. if None, defaults to the input image size. if its components have non-positive values, the corresponding size of input image will be used. @@ -423,10 +444,17 @@ class RandSpatialCropSamples(Randomizable, Transform): the minimum size to limit the randomly generated ROI. It will return a list of cropped images. + Note: even `random_size=False`, if a dimension of the expected ROI size is bigger than the input image size, + will not crop that dimension. So the cropped result may be smaller than the expected ROI, and the cropped + results of several images may not have exactly the same shape. + Args: roi_size: if `random_size` is True, it specifies the minimum crop region. if `random_size` is False, it specifies the expected ROI size to crop. e.g. [224, 224, 128] + if a dimension of ROI size is bigger than image size, will not crop that dimension of the image. If its components have non-positive values, the corresponding size of input image will be used. + for example: if the spatial size of input data is [40, 40, 40] and `roi_size=[32, 64, -1]`, + the spatial size of output data will be [32, 40, 40]. num_samples: number of samples (crop regions) to take in the returned list. max_roi_size: if `random_size` is True and `roi_size` specifies the min crop region size, `max_roi_size` can specify the max crop region size. if None, defaults to the input image size. @@ -640,9 +668,16 @@ class RandCropByPosNegLabel(Randomizable, Transform): [0, 0, 0, 0, 0], [0, 0, 0]] [0, 0, 0]] [0, 0, 0, 0, 0]]] + If a dimension of the expected spatial size is bigger than the input image size, + will not crop that dimension. So the cropped result may be smaller than expected size, and the cropped + results of several images may not have exactly same shape. + Args: spatial_size: the spatial size of the crop region e.g. [224, 224, 128]. - If its components have non-positive values, the corresponding size of `label` will be used. + if a dimension of ROI size is bigger than image size, will not crop that dimension of the image. + if its components have non-positive values, the corresponding size of `label` will be used. + for example: if the spatial size of input data is [40, 40, 40] and `spatial_size=[32, 64, -1]`, + the spatial size of output data will be [32, 40, 40]. label: the label image that is used for finding foreground/background, if None, must set at `self.__call__`. Non-zero indicates foreground, zero indicates background. pos: used with `neg` together to calculate the ratio ``pos / (pos + neg)`` for the probability diff --git a/monai/transforms/croppad/dictionary.py b/monai/transforms/croppad/dictionary.py index 614dd16882..ed01559ff5 100644 --- a/monai/transforms/croppad/dictionary.py +++ b/monai/transforms/croppad/dictionary.py @@ -116,8 +116,11 @@ def __init__( Args: keys: keys of the corresponding items to be transformed. See also: :py:class:`monai.transforms.compose.MapTransform` - spatial_size: the spatial size of output data after padding. + spatial_size: the spatial size of output data after padding, if a dimension of the input + data size is bigger than the pad size, will not pad that dimension. If its components have non-positive values, the corresponding size of input image will be used. + for example: if the spatial size of input data is [30, 30, 30] and `spatial_size=[32, 25, -1]`, + the spatial size of output data will be [32, 30, 30]. method: {``"symmetric"``, ``"end"``} Pad image symmetric on every side or only pad at the end sides. Defaults to ``"symmetric"``. mode: {``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``, ``"mean"``, @@ -296,6 +299,9 @@ class SpatialCropd(MapTransform, InvertibleTransform): """ Dictionary-based wrapper of :py:class:`monai.transforms.SpatialCrop`. General purpose cropper to produce sub-volume region of interest (ROI). + If a dimension of the expected ROI size is bigger than the input image size, will not crop that dimension. + So the cropped result may be smaller than the expected ROI, and the cropped results of several images may + not have exactly the same shape. It can support to crop ND spatial (channel-first) data. The cropped region can be parameterised in various ways: @@ -319,9 +325,11 @@ def __init__( keys: keys of the corresponding items to be transformed. See also: :py:class:`monai.transforms.compose.MapTransform` roi_center: voxel coordinates for center of the crop ROI. - roi_size: size of the crop ROI. + roi_size: size of the crop ROI, if a dimension of ROI size is bigger than image size, + will not crop that dimension of the image. roi_start: voxel coordinates for start of the crop ROI. - roi_end: voxel coordinates for end of the crop ROI. + roi_end: voxel coordinates for end of the crop ROI, if a coordinate is out of image, + use the end coordinate of image. roi_slices: list of slices for each of the spatial dimensions. allow_missing_keys: don't raise exception if key is missing. """ @@ -360,12 +368,18 @@ def inverse(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.ndar class CenterSpatialCropd(MapTransform, InvertibleTransform): """ Dictionary-based wrapper of :py:class:`monai.transforms.CenterSpatialCrop`. + If a dimension of the expected ROI size is bigger than the input image size, will not crop that dimension. + So the cropped result may be smaller than the expected ROI, and the cropped results of several images may + not have exactly the same shape. Args: keys: keys of the corresponding items to be transformed. See also: monai.transforms.MapTransform roi_size: the size of the crop region e.g. [224,224,128] + if a dimension of ROI size is bigger than image size, will not crop that dimension of the image. If its components have non-positive values, the corresponding size of input image will be used. + for example: if the spatial size of input data is [40, 40, 40] and `roi_size=[32, 64, -1]`, + the spatial size of output data will be [32, 40, 40]. allow_missing_keys: don't raise exception if key is missing. """ @@ -467,12 +481,19 @@ class RandSpatialCropd(Randomizable, MapTransform, InvertibleTransform): center or at the image center. And allows to set the minimum and maximum size to limit the randomly generated ROI. Suppose all the expected fields specified by `keys` have same shape. + Note: even `random_size=False`, if a dimension of the expected ROI size is bigger than the input image size, + will not crop that dimension. So the cropped result may be smaller than the expected ROI, and the cropped + results of several images may not have exactly the same shape. + Args: keys: keys of the corresponding items to be transformed. See also: monai.transforms.MapTransform roi_size: if `random_size` is True, it specifies the minimum crop region. if `random_size` is False, it specifies the expected ROI size to crop. e.g. [224, 224, 128] + if a dimension of ROI size is bigger than image size, will not crop that dimension of the image. If its components have non-positive values, the corresponding size of input image will be used. + for example: if the spatial size of input data is [40, 40, 40] and `roi_size=[32, 64, -1]`, + the spatial size of output data will be [32, 40, 40]. max_roi_size: if `random_size` is True and `roi_size` specifies the min crop region size, `max_roi_size` can specify the max crop region size. if None, defaults to the input image size. if its components have non-positive values, the corresponding size of input image will be used. @@ -633,12 +654,19 @@ class RandSpatialCropSamplesd(Randomizable, MapTransform, InvertibleTransform): specified by `keys` have same shape, and add `patch_index` to the corresponding meta data. It will return a list of dictionaries for all the cropped images. + Note: even `random_size=False`, if a dimension of the expected ROI size is bigger than the input image size, + will not crop that dimension. So the cropped result may be smaller than the expected ROI, and the cropped + results of several images may not have exactly the same shape. + Args: keys: keys of the corresponding items to be transformed. See also: monai.transforms.MapTransform roi_size: if `random_size` is True, it specifies the minimum crop region. if `random_size` is False, it specifies the expected ROI size to crop. e.g. [224, 224, 128] + if a dimension of ROI size is bigger than image size, will not crop that dimension of the image. If its components have non-positive values, the corresponding size of input image will be used. + for example: if the spatial size of input data is [40, 40, 40] and `roi_size=[32, 64, -1]`, + the spatial size of output data will be [32, 40, 40]. num_samples: number of samples (crop regions) to take in the returned list. max_roi_size: if `random_size` is True and `roi_size` specifies the min crop region size, `max_roi_size` can specify the max crop region size. if None, defaults to the input image size. @@ -946,12 +974,19 @@ class RandCropByPosNegLabeld(Randomizable, MapTransform, InvertibleTransform): and add `patch_index` to the corresponding meta data. And will return a list of dictionaries for all the cropped images. + If a dimension of the expected spatial size is bigger than the input image size, + will not crop that dimension. So the cropped result may be smaller than the expected size, + and the cropped results of several images may not have exactly the same shape. + Args: keys: keys of the corresponding items to be transformed. See also: :py:class:`monai.transforms.compose.MapTransform` label_key: name of key for label image, this will be used for finding foreground/background. spatial_size: the spatial size of the crop region e.g. [224, 224, 128]. - If its components have non-positive values, the corresponding size of `data[label_key]` will be used. + if a dimension of ROI size is bigger than image size, will not crop that dimension of the image. + if its components have non-positive values, the corresponding size of `data[label_key]` will be used. + for example: if the spatial size of input data is [40, 40, 40] and `spatial_size=[32, 64, -1]`, + the spatial size of output data will be [32, 40, 40]. pos: used with `neg` together to calculate the ratio ``pos / (pos + neg)`` for the probability to pick a foreground voxel as a center rather than a background voxel. neg: used with `pos` together to calculate the ratio ``pos / (pos + neg)`` for the probability