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

Class CenterSpatialCrop

monai/transforms/croppad/array.py:490–529  ·  view source on GitHub ↗

Crop at the center of image with specified ROI size. If a dimension of the expected ROI size is larger 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

Source from the content-addressed store, hash-verified

488
489
490class CenterSpatialCrop(Crop):
491 """
492 Crop at the center of image with specified ROI size.
493 If a dimension of the expected ROI size is larger than the input image size, will not crop that dimension.
494 So the cropped result may be smaller than the expected ROI, and the cropped results of several images may
495 not have exactly the same shape.
496
497 This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
498 for more information.
499
500 Args:
501 roi_size: the spatial size of the crop region e.g. [224,224,128]
502 if a dimension of ROI size is larger than image size, will not crop that dimension of the image.
503 If its components have non-positive values, the corresponding size of input image will be used.
504 for example: if the spatial size of input data is [40, 40, 40] and `roi_size=[32, 64, -1]`,
505 the spatial size of output data will be [32, 40, 40].
506 lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False.
507 """
508
509 def __init__(self, roi_size: Sequence[int] | int, lazy: bool = False) -> None:
510 super().__init__(lazy=lazy)
511 self.roi_size = roi_size
512
513 def compute_slices(self, spatial_size: Sequence[int]) -> tuple[slice]: # type: ignore[override]
514 roi_size = fall_back_tuple(self.roi_size, spatial_size)
515 roi_center = [i // 2 for i in spatial_size]
516 return super().compute_slices(roi_center=roi_center, roi_size=roi_size)
517
518 def __call__(self, img: torch.Tensor, lazy: bool | None = None) -> torch.Tensor: # type: ignore[override]
519 """
520 Apply the transform to `img`, assuming `img` is channel-first and
521 slicing doesn&#x27;t apply to the channel dim.
522
523 """
524 lazy_ = self.lazy if lazy is None else lazy
525 return super().__call__(
526 img=img,
527 slices=self.compute_slices(img.peek_pending_shape() if isinstance(img, MetaTensor) else img.shape[1:]),
528 lazy=lazy_,
529 )
530
531
532class CenterScaleCrop(Crop):

Callers 9

__call__Method · 0.90
__call__Method · 0.90
__init__Method · 0.90
inverseMethod · 0.90
__init__Method · 0.90
__init__Method · 0.90
__call__Method · 0.85
__call__Method · 0.85
__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…