Crop an image using a bounding box. The bounding box is generated by selecting foreground using select_fn at channels channel_indices. margin is added in each spatial dimension of the bounding box. The typical usage is to help training and evaluation if the valid part is small in the wh
| 774 | |
| 775 | |
| 776 | class CropForeground(Crop): |
| 777 | """ |
| 778 | Crop an image using a bounding box. The bounding box is generated by selecting foreground using select_fn |
| 779 | at channels channel_indices. margin is added in each spatial dimension of the bounding box. |
| 780 | The typical usage is to help training and evaluation if the valid part is small in the whole medical image. |
| 781 | Users can define arbitrary function to select expected foreground from the whole image or specified channels. |
| 782 | And it can also add margin to every dim of the bounding box of foreground object. |
| 783 | For example: |
| 784 | |
| 785 | .. code-block:: python |
| 786 | |
| 787 | image = np.array( |
| 788 | [[[0, 0, 0, 0, 0], |
| 789 | [0, 1, 2, 1, 0], |
| 790 | [0, 1, 3, 2, 0], |
| 791 | [0, 1, 2, 1, 0], |
| 792 | [0, 0, 0, 0, 0]]]) # 1x5x5, single channel 5x5 image |
| 793 | |
| 794 | |
| 795 | def threshold_at_one(x): |
| 796 | # threshold at 1 |
| 797 | return x > 1 |
| 798 | |
| 799 | |
| 800 | cropper = CropForeground(select_fn=threshold_at_one, margin=0) |
| 801 | print(cropper(image)) |
| 802 | [[[2, 1], |
| 803 | [3, 2], |
| 804 | [2, 1]]] |
| 805 | |
| 806 | This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>` |
| 807 | for more information. |
| 808 | |
| 809 | """ |
| 810 | |
| 811 | def __init__( |
| 812 | self, |
| 813 | select_fn: Callable = is_positive, |
| 814 | channel_indices: IndexSelection | None = None, |
| 815 | margin: Sequence[int] | int = 0, |
| 816 | allow_smaller: bool = False, |
| 817 | return_coords: bool = False, |
| 818 | k_divisible: Sequence[int] | int = 1, |
| 819 | mode: str = PytorchPadMode.CONSTANT, |
| 820 | lazy: bool = False, |
| 821 | **pad_kwargs, |
| 822 | ) -> None: |
| 823 | """ |
| 824 | Args: |
| 825 | select_fn: function to select expected foreground, default is to select values > 0. |
| 826 | channel_indices: if defined, select foreground only on the specified channels |
| 827 | of image. if None, select foreground on the whole image. |
| 828 | margin: add margin value to spatial dims of the bounding box, if only 1 value provided, use it for all dims. |
| 829 | allow_smaller: when computing box size with `margin`, whether to allow the image edges to be smaller than the |
| 830 | final box edges. If `False`, part of a padded output box might be outside of the original image, if `True`, |
| 831 | the image edges will be used as the box edges. Default to `False`. |
| 832 | The default value is changed from `True` to `False` in v1.5.0. |
| 833 | return_coords: whether return the coordinates of spatial bounding box for foreground. |
no outgoing calls
searching dependent graphs…