General purpose cropper to produce sub-volume region of interest (ROI). 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
| 435 | |
| 436 | |
| 437 | class SpatialCrop(Crop): |
| 438 | """ |
| 439 | General purpose cropper to produce sub-volume region of interest (ROI). |
| 440 | If a dimension of the expected ROI size is larger than the input image size, will not crop that dimension. |
| 441 | So the cropped result may be smaller than the expected ROI, and the cropped results of several images may |
| 442 | not have exactly the same shape. |
| 443 | It can support to crop ND spatial (channel-first) data. |
| 444 | |
| 445 | The cropped region can be parameterised in various ways: |
| 446 | - a list of slices for each spatial dimension (allows for use of negative indexing and `None`) |
| 447 | - a spatial center and size |
| 448 | - the start and end coordinates of the ROI |
| 449 | |
| 450 | This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>` |
| 451 | for more information. |
| 452 | """ |
| 453 | |
| 454 | def __init__( |
| 455 | self, |
| 456 | roi_center: Sequence[int] | int | NdarrayOrTensor | None = None, |
| 457 | roi_size: Sequence[int] | int | NdarrayOrTensor | None = None, |
| 458 | roi_start: Sequence[int] | int | NdarrayOrTensor | None = None, |
| 459 | roi_end: Sequence[int] | int | NdarrayOrTensor | None = None, |
| 460 | roi_slices: Sequence[slice] | None = None, |
| 461 | lazy: bool = False, |
| 462 | ) -> None: |
| 463 | """ |
| 464 | Args: |
| 465 | roi_center: voxel coordinates for center of the crop ROI. |
| 466 | roi_size: size of the crop ROI, if a dimension of ROI size is larger than image size, |
| 467 | will not crop that dimension of the image. |
| 468 | roi_start: voxel coordinates for start of the crop ROI. |
| 469 | roi_end: voxel coordinates for end of the crop ROI, if a coordinate is out of image, |
| 470 | use the end coordinate of image. |
| 471 | roi_slices: list of slices for each of the spatial dimensions. |
| 472 | lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False. |
| 473 | |
| 474 | """ |
| 475 | super().__init__(lazy) |
| 476 | self.slices = self.compute_slices( |
| 477 | roi_center=roi_center, roi_size=roi_size, roi_start=roi_start, roi_end=roi_end, roi_slices=roi_slices |
| 478 | ) |
| 479 | |
| 480 | def __call__(self, img: torch.Tensor, lazy: bool | None = None) -> torch.Tensor: # type: ignore[override] |
| 481 | """ |
| 482 | Apply the transform to `img`, assuming `img` is channel-first and |
| 483 | slicing doesn't apply to the channel dim. |
| 484 | |
| 485 | """ |
| 486 | lazy_ = self.lazy if lazy is None else lazy |
| 487 | return super().__call__(img=img, slices=ensure_tuple(self.slices), lazy=lazy_) |
| 488 | |
| 489 | |
| 490 | class CenterSpatialCrop(Crop): |
no outgoing calls
searching dependent graphs…