Subclass of :py:class:`monai.transforms.RandSpatialCrop`. 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 scale of image size to limit the randomly generated ROI. Th
| 629 | |
| 630 | |
| 631 | class RandScaleCrop(RandSpatialCrop): |
| 632 | """ |
| 633 | Subclass of :py:class:`monai.transforms.RandSpatialCrop`. Crop image with |
| 634 | random size or specific size ROI. It can crop at a random position as |
| 635 | center or at the image center. And allows to set the minimum and maximum |
| 636 | scale of image size to limit the randomly generated ROI. |
| 637 | |
| 638 | This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>` |
| 639 | for more information. |
| 640 | |
| 641 | Args: |
| 642 | roi_scale: if `random_size` is True, it specifies the minimum crop size: `roi_scale * image spatial size`. |
| 643 | if `random_size` is False, it specifies the expected scale of image size to crop. e.g. [0.3, 0.4, 0.5]. |
| 644 | If its components have non-positive values, will use `1.0` instead, which means the input image size. |
| 645 | max_roi_scale: if `random_size` is True and `roi_scale` specifies the min crop region size, `max_roi_scale` |
| 646 | can specify the max crop region size: `max_roi_scale * image spatial size`. |
| 647 | if None, defaults to the input image size. if its components have non-positive values, |
| 648 | will use `1.0` instead, which means the input image size. |
| 649 | random_center: crop at random position as center or the image center. |
| 650 | random_size: crop with random size or specified size ROI by `roi_scale * image spatial size`. |
| 651 | if True, the actual size is sampled from |
| 652 | `randint(roi_scale * image spatial size, max_roi_scale * image spatial size + 1)`. |
| 653 | lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False. |
| 654 | """ |
| 655 | |
| 656 | def __init__( |
| 657 | self, |
| 658 | roi_scale: Sequence[float] | float, |
| 659 | max_roi_scale: Sequence[float] | float | None = None, |
| 660 | random_center: bool = True, |
| 661 | random_size: bool = False, |
| 662 | lazy: bool = False, |
| 663 | ) -> None: |
| 664 | super().__init__( |
| 665 | roi_size=-1, max_roi_size=None, random_center=random_center, random_size=random_size, lazy=lazy |
| 666 | ) |
| 667 | self.roi_scale = roi_scale |
| 668 | self.max_roi_scale = max_roi_scale |
| 669 | |
| 670 | def get_max_roi_size(self, img_size): |
| 671 | ndim = len(img_size) |
| 672 | self.roi_size = [ceil(r * s) for r, s in zip(ensure_tuple_rep(self.roi_scale, ndim), img_size)] |
| 673 | if self.max_roi_scale is not None: |
| 674 | self.max_roi_size = [ceil(r * s) for r, s in zip(ensure_tuple_rep(self.max_roi_scale, ndim), img_size)] |
| 675 | else: |
| 676 | self.max_roi_size = None |
| 677 | |
| 678 | def randomize(self, img_size: Sequence[int]) -> None: |
| 679 | self.get_max_roi_size(img_size) |
| 680 | super().randomize(img_size) |
| 681 | |
| 682 | def __call__(self, img: torch.Tensor, randomize: bool = True, lazy: bool | None = None) -> torch.Tensor: # type: ignore |
| 683 | """ |
| 684 | Apply the transform to `img`, assuming `img` is channel-first and |
| 685 | slicing doesn't apply to the channel dim. |
| 686 | |
| 687 | """ |
| 688 | self.get_max_roi_size(img.peek_pending_shape() if isinstance(img, MetaTensor) else img.shape[1:]) |
no outgoing calls
searching dependent graphs…