Skip to content

Commit dcdae0d

Browse files
KumoLiuwyli
andauthored
Fix the use of allow_smaller in CropForeground (#6686)
Fixes #6685 . ### Description When `allow_smaller=True`, it allows the image size to be smaller than the box size and will pad it. But in `generate_spatial_bounding_box`, it generates the wrong bounding box. https://github.com/Project-MONAI/MONAI/blob/e7fb74f32b5371bb54bff7a47447df31d06d8edf/monai/transforms/utils.py#L1001-L1003 Change the default value of `allow_smaller` in `CropForeground` to `False` to be consistent with the previous behavior. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: KumoLiu <[email protected]> Signed-off-by: YunLiu <[email protected]> Co-authored-by: Wenqi Li <[email protected]>
1 parent 544c0bd commit dcdae0d

File tree

6 files changed

+20
-19
lines changed

6 files changed

+20
-19
lines changed

monai/transforms/croppad/array.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ def __init__(
824824
select_fn: Callable = is_positive,
825825
channel_indices: IndexSelection | None = None,
826826
margin: Sequence[int] | int = 0,
827-
allow_smaller: bool = True,
827+
allow_smaller: bool = False,
828828
return_coords: bool = False,
829829
k_divisible: Sequence[int] | int = 1,
830830
mode: str = PytorchPadMode.CONSTANT,
@@ -837,9 +837,9 @@ def __init__(
837837
channel_indices: if defined, select foreground only on the specified channels
838838
of image. if None, select foreground on the whole image.
839839
margin: add margin value to spatial dims of the bounding box, if only 1 value provided, use it for all dims.
840-
allow_smaller: when computing box size with `margin`, whether allow the image size to be smaller
841-
than box size, default to `True`. if the margined size is larger than image size, will pad with
842-
specified `mode`.
840+
allow_smaller: when computing box size with `margin`, whether to allow the final box edges to be outside of
841+
the image edges (the image is smaller than the box). If `True`, part of a padded output box might be outside
842+
of the original image, if `False`, the image edges will be used as the box edges.
843843
return_coords: whether return the coordinates of spatial bounding box for foreground.
844844
k_divisible: make each spatial dimension to be divisible by k, default to 1.
845845
if `k_divisible` is an int, the same `k` be applied to all the input spatial dimensions.

monai/transforms/croppad/dictionary.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ def __init__(
729729
select_fn: Callable = is_positive,
730730
channel_indices: IndexSelection | None = None,
731731
margin: Sequence[int] | int = 0,
732-
allow_smaller: bool = True,
732+
allow_smaller: bool = False,
733733
k_divisible: Sequence[int] | int = 1,
734734
mode: SequenceStr = PytorchPadMode.CONSTANT,
735735
start_coord_key: str = "foreground_start_coord",
@@ -747,9 +747,9 @@ def __init__(
747747
channel_indices: if defined, select foreground only on the specified channels
748748
of image. if None, select foreground on the whole image.
749749
margin: add margin value to spatial dims of the bounding box, if only 1 value provided, use it for all dims.
750-
allow_smaller: when computing box size with `margin`, whether allow the image size to be smaller
751-
than box size, default to `True`. if the margined size is larger than image size, will pad with
752-
specified `mode`.
750+
allow_smaller: when computing box size with `margin`, whether to allow the final box edges to be outside of
751+
the image edges (the image is smaller than the box). If `True`, part of a padded output box might be outside
752+
of the original image, if `False`, the image edges will be used as the box edges.
753753
k_divisible: make each spatial dimension to be divisible by k, default to 1.
754754
if `k_divisible` is an int, the same `k` be applied to all the input spatial dimensions.
755755
mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``,

monai/transforms/utils.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ def generate_spatial_bounding_box(
950950
select_fn: Callable = is_positive,
951951
channel_indices: IndexSelection | None = None,
952952
margin: Sequence[int] | int = 0,
953-
allow_smaller: bool = True,
953+
allow_smaller: bool = False,
954954
) -> tuple[list[int], list[int]]:
955955
"""
956956
Generate the spatial bounding box of foreground in the image with start-end positions (inclusive).
@@ -961,7 +961,6 @@ def generate_spatial_bounding_box(
961961
[1st_spatial_dim_start, 2nd_spatial_dim_start, ..., Nth_spatial_dim_start],
962962
[1st_spatial_dim_end, 2nd_spatial_dim_end, ..., Nth_spatial_dim_end]
963963
964-
If `allow_smaller`, the bounding boxes edges are aligned with the input image edges.
965964
This function returns [0, 0, ...], [0, 0, ...] if there's no positive intensity.
966965
967966
Args:
@@ -970,8 +969,10 @@ def generate_spatial_bounding_box(
970969
channel_indices: if defined, select foreground only on the specified channels
971970
of image. if None, select foreground on the whole image.
972971
margin: add margin value to spatial dims of the bounding box, if only 1 value provided, use it for all dims.
973-
allow_smaller: when computing box size with `margin`, whether allow the image size to be smaller
974-
than box size, default to `True`.
972+
allow_smaller: when computing box size with `margin`, whether to allow the final box edges to be outside of
973+
the image edges (the image is smaller than the box). If `False`, the bounding boxes edges are aligned
974+
with the input image edges, default to `False`.
975+
975976
"""
976977
check_non_lazy_pending_ops(img, name="generate_spatial_bounding_box")
977978
spatial_size = img.shape[1:]
@@ -998,7 +999,7 @@ def generate_spatial_bounding_box(
998999
arg_max = where(dt == dt.max())[0]
9991000
min_d = arg_max[0] - margin[di]
10001001
max_d = arg_max[-1] + margin[di] + 1
1001-
if allow_smaller:
1002+
if not allow_smaller:
10021003
min_d = max(min_d, 0)
10031004
max_d = min(max_d, spatial_size[di])
10041005

tests/test_crop_foreground.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363

6464
TESTS.append(
6565
[
66-
{"select_fn": lambda x: x > 0, "channel_indices": None, "margin": [2, 1], "allow_smaller": True},
66+
{"select_fn": lambda x: x > 0, "channel_indices": None, "margin": [2, 1], "allow_smaller": False},
6767
p([[[0, 0, 0, 0, 0], [0, 1, 2, 1, 0], [0, 2, 3, 2, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]]),
6868
p([[[0, 0, 0, 0, 0], [0, 1, 2, 1, 0], [0, 2, 3, 2, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]]),
6969
True,
@@ -72,7 +72,7 @@
7272

7373
TESTS.append(
7474
[
75-
{"select_fn": lambda x: x > 0, "channel_indices": None, "margin": [2, 1], "allow_smaller": False},
75+
{"select_fn": lambda x: x > 0, "channel_indices": None, "margin": [2, 1], "allow_smaller": True},
7676
p([[[0, 0, 0, 0, 0], [0, 1, 2, 1, 0], [0, 2, 3, 2, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]]),
7777
p([[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 1, 2, 1, 0], [0, 2, 3, 2, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]]),
7878
True,

tests/test_crop_foregroundd.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
"select_fn": lambda x: x > 0,
8989
"channel_indices": None,
9090
"margin": [2, 1],
91-
"allow_smaller": True,
91+
"allow_smaller": False,
9292
},
9393
{
9494
"img": p(
@@ -107,7 +107,7 @@
107107
"select_fn": lambda x: x > 0,
108108
"channel_indices": None,
109109
"margin": [2, 1],
110-
"allow_smaller": False,
110+
"allow_smaller": True,
111111
},
112112
{
113113
"img": p(

tests/test_generate_spatial_bounding_box.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
"select_fn": lambda x: x > 0,
8383
"channel_indices": None,
8484
"margin": [2, 1],
85-
"allow_smaller": False,
85+
"allow_smaller": True,
8686
},
8787
([-1, 0], [6, 5]),
8888
]
@@ -96,7 +96,7 @@
9696
"select_fn": lambda x: x > 0,
9797
"channel_indices": None,
9898
"margin": [2, 1],
99-
"allow_smaller": True,
99+
"allow_smaller": False,
100100
},
101101
([0, 0], [5, 5]),
102102
]

0 commit comments

Comments
 (0)