Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions monai/transforms/croppad/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,8 @@ class RandCropByPosNegLabel(Randomizable, Transform):
If a dimension of the expected spatial size is bigger than the input image size,
will not crop that dimension. So the cropped result may be smaller than expected size, and the cropped
results of several images may not have exactly same shape.
And if the crop ROI is partly out of the image, will automatically adjust the crop center to ensure the
valid crop ROI.

Args:
spatial_size: the spatial size of the crop region e.g. [224, 224, 128].
Expand Down Expand Up @@ -1021,6 +1023,8 @@ class RandCropByLabelClasses(Randomizable, Transform):
If a dimension of the expected spatial size is bigger than the input image size,
will not crop that dimension. So the cropped result may be smaller than expected size, and the cropped
results of several images may not have exactly same shape.
And if the crop ROI is partly out of the image, will automatically adjust the crop center to ensure the
valid crop ROI.

Args:
spatial_size: the spatial size of the crop region e.g. [224, 224, 128].
Expand Down
4 changes: 4 additions & 0 deletions monai/transforms/croppad/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,8 @@ class RandCropByPosNegLabeld(Randomizable, MapTransform, InvertibleTransform):
If a dimension of the expected spatial size is bigger than the input image size,
will not crop that dimension. So the cropped result may be smaller than the expected size,
and the cropped results of several images may not have exactly the same shape.
And if the crop ROI is partly out of the image, will automatically adjust the crop center
to ensure the valid crop ROI.

Args:
keys: keys of the corresponding items to be transformed.
Expand Down Expand Up @@ -1259,6 +1261,8 @@ class RandCropByLabelClassesd(Randomizable, MapTransform, InvertibleTransform):
If a dimension of the expected spatial size is bigger than the input image size,
will not crop that dimension. So the cropped result may be smaller than expected size, and the cropped
results of several images may not have exactly same shape.
And if the crop ROI is partly out of the image, will automatically adjust the crop center to ensure the
valid crop ROI.

Args:
keys: keys of the corresponding items to be transformed.
Expand Down
7 changes: 7 additions & 0 deletions tests/test_rand_crop_by_pos_neg_labeld.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ def test_type_shape(self, input_param, input_data, expected_shape):
for i, item in enumerate(result):
self.assertEqual(item[PostFix.meta(k)]["patch_index"], i)

def test_correct_center(self):
cropper = RandCropByPosNegLabeld(keys="label", label_key="label", spatial_size=[3, 3])
cropper.set_random_state(0)
test_image = {"label": np.asarray([[[1, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0], [1, 0, 0, 1]]])}
result = cropper(test_image)
np.testing.assert_allclose(result[0]["label"], np.asarray([[[0, 0, 1], [0, 0, 0], [0, 0, 0]]]))


if __name__ == "__main__":
unittest.main()