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

Class RandSpatialCropSamples

monai/transforms/croppad/array.py:693–773  ·  view source on GitHub ↗

Crop image with random size or specific size ROI to generate a list of N samples. It can crop at a random position as center or at the image center. And allows to set the minimum size to limit the randomly generated ROI. It will return a list of cropped images. Note: even `rand

Source from the content-addressed store, hash-verified

691
692
693class RandSpatialCropSamples(Randomizable, TraceableTransform, LazyTransform, MultiSampleTrait):
694 """
695 Crop image with random size or specific size ROI to generate a list of N samples.
696 It can crop at a random position as center or at the image center. And allows to set
697 the minimum size to limit the randomly generated ROI.
698 It will return a list of cropped images.
699
700 Note: even `random_size=False`, if a dimension of the expected ROI size is larger than the input image size,
701 will not crop that dimension. So the cropped result may be smaller than the expected ROI, and the cropped
702 results of several images may not have exactly the same shape.
703
704 This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
705 for more information.
706
707 Args:
708 roi_size: if `random_size` is True, it specifies the minimum crop region.
709 if `random_size` is False, it specifies the expected ROI size to crop. e.g. [224, 224, 128]
710 if a dimension of ROI size is larger than image size, will not crop that dimension of the image.
711 If its components have non-positive values, the corresponding size of input image will be used.
712 for example: if the spatial size of input data is [40, 40, 40] and `roi_size=[32, 64, -1]`,
713 the spatial size of output data will be [32, 40, 40].
714 num_samples: number of samples (crop regions) to take in the returned list.
715 max_roi_size: if `random_size` is True and `roi_size` specifies the min crop region size, `max_roi_size`
716 can specify the max crop region size. if None, defaults to the input image size.
717 if its components have non-positive values, the corresponding size of input image will be used.
718 random_center: crop at random position as center or the image center.
719 random_size: crop with random size or specific size ROI.
720 The actual size is sampled from `randint(roi_size, img_size)`.
721 lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False.
722
723 Raises:
724 ValueError: When ``num_samples`` is nonpositive.
725
726 """
727
728 backend = RandSpatialCrop.backend
729
730 def __init__(
731 self,
732 roi_size: Sequence[int] | int,
733 num_samples: int,
734 max_roi_size: Sequence[int] | int | None = None,
735 random_center: bool = True,
736 random_size: bool = False,
737 lazy: bool = False,
738 ) -> None:
739 LazyTransform.__init__(self, lazy)
740 if num_samples < 1:
741 raise ValueError(f"num_samples must be positive, got {num_samples}.")
742 self.num_samples = num_samples
743 self.cropper = RandSpatialCrop(roi_size, max_roi_size, random_center, random_size, lazy)
744
745 def set_random_state(
746 self, seed: int | None = None, state: np.random.RandomState | None = None
747 ) -> RandSpatialCropSamples:
748 super().set_random_state(seed, state)
749 self.cropper.set_random_state(seed, state)
750 return self

Callers 4

__init__Method · 0.90
test_loading_arrayMethod · 0.90
test_shapeMethod · 0.90
test_pending_opsMethod · 0.90

Calls

no outgoing calls

Tested by 3

test_loading_arrayMethod · 0.72
test_shapeMethod · 0.72
test_pending_opsMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…