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

Class RandSpatialCrop

monai/transforms/croppad/array.py:558–628  ·  view source on GitHub ↗

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 larger tha

Source from the content-addressed store, hash-verified

556
557
558class RandSpatialCrop(Randomizable, Crop):
559 """
560 Crop image with random size or specific size ROI. It can crop at a random position as center
561 or at the image center. And allows to set the minimum and maximum size to limit the randomly generated ROI.
562
563 Note: even `random_size=False`, if a dimension of the expected ROI size is larger than the input image size,
564 will not crop that dimension. So the cropped result may be smaller than the expected ROI, and the cropped results
565 of several images may not have exactly the same shape.
566
567 This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
568 for more information.
569
570 Args:
571 roi_size: if `random_size` is True, it specifies the minimum crop region.
572 if `random_size` is False, it specifies the expected ROI size to crop. e.g. [224, 224, 128]
573 if a dimension of ROI size is larger than image size, will not crop that dimension of the image.
574 If its components have non-positive values, the corresponding size of input image will be used.
575 for example: if the spatial size of input data is [40, 40, 40] and `roi_size=[32, 64, -1]`,
576 the spatial size of output data will be [32, 40, 40].
577 max_roi_size: if `random_size` is True and `roi_size` specifies the min crop region size, `max_roi_size`
578 can specify the max crop region size. if None, defaults to the input image size.
579 if its components have non-positive values, the corresponding size of input image will be used.
580 random_center: crop at random position as center or the image center.
581 random_size: crop with random size or specific size ROI.
582 if True, the actual size is sampled from `randint(roi_size, max_roi_size + 1)`.
583 lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False.
584 """
585
586 def __init__(
587 self,
588 roi_size: Sequence[int] | int,
589 max_roi_size: Sequence[int] | int | None = None,
590 random_center: bool = True,
591 random_size: bool = False,
592 lazy: bool = False,
593 ) -> None:
594 super().__init__(lazy)
595 self.roi_size = roi_size
596 self.max_roi_size = max_roi_size
597 self.random_center = random_center
598 self.random_size = random_size
599 self._size: Sequence[int] | None = None
600 self._slices: tuple[slice, ...]
601
602 def randomize(self, img_size: Sequence[int]) -> None:
603 self._size = fall_back_tuple(self.roi_size, img_size)
604 if self.random_size:
605 max_size = img_size if self.max_roi_size is None else fall_back_tuple(self.max_roi_size, img_size)
606 if any(i > j for i, j in zip(self._size, max_size)):
607 raise ValueError(f"min ROI size: {self._size} is larger than max ROI size: {max_size}.")
608 self._size = tuple(self.R.randint(low=self._size[i], high=max_size[i] + 1) for i in range(len(img_size)))
609 if self.random_center:
610 valid_size = get_valid_patch_size(img_size, self._size)
611 self._slices = get_random_patch(img_size, valid_size, self.R)
612
613 def __call__(self, img: torch.Tensor, randomize: bool = True, lazy: bool | None = None) -> torch.Tensor: # type: ignore
614 """
615 Apply the transform to `img`, assuming `img` is channel-first and

Callers 6

__init__Method · 0.90
run_testFunction · 0.90
test_valueMethod · 0.90
test_random_shapeMethod · 0.90
__init__Method · 0.85

Calls

no outgoing calls

Tested by 3

run_testFunction · 0.72
test_valueMethod · 0.72
test_random_shapeMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…