Skip to content

Commit c68e922

Browse files
authored
5253 fixes output offset - spacing (#5254)
Signed-off-by: Wenqi Li <[email protected]> Fixes #5253 ### Description enhance computing offset for spacing ### 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). - [x] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [x] 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: Wenqi Li <[email protected]>
1 parent 5b3b741 commit c68e922

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

monai/data/utils.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -875,17 +875,19 @@ def compute_shape_offset(
875875
corners_out = inv_mat @ corners
876876
corners_out = corners_out[:-1] / corners_out[-1]
877877
out_shape = np.round(corners_out.ptp(axis=1)) if scale_extent else np.round(corners_out.ptp(axis=1) + 1.0)
878-
mat = inv_mat[:-1, :-1]
879-
i = 0
878+
all_dist = inv_mat[:-1, :-1] @ corners[:-1, :]
879+
offset = None
880880
for i in range(corners.shape[1]):
881-
min_corner = np.min(mat @ corners[:-1, :] - mat @ corners[:-1, i : i + 1], 1)
881+
min_corner = np.min(all_dist - all_dist[:, i : i + 1], 1)
882882
if np.allclose(min_corner, 0.0, rtol=AFFINE_TOL):
883+
offset = corners[:-1, i] # corner is the smallest, shift the corner to origin
883884
break
884-
offset = corners[:-1, i]
885+
if offset is None: # otherwise make output image center aligned with the input image center
886+
offset = in_affine_[:-1, :-1] @ (shape / 2.0) + in_affine_[:-1, -1] - out_affine_[:-1, :-1] @ (out_shape / 2.0)
885887
if scale_extent:
886888
in_offset = np.append(0.5 * (shape / out_shape - 1.0), 1.0)
887889
offset = np.abs((in_affine_ @ in_offset / in_offset[-1])[:-1]) * np.sign(offset)
888-
return out_shape.astype(int, copy=False), offset
890+
return out_shape.astype(int, copy=False), offset # type: ignore
889891

890892

891893
def to_affine_nd(r: Union[np.ndarray, int], affine: NdarrayTensor, dtype=np.float64) -> NdarrayTensor:

tests/test_spacing.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,24 @@
230230
*device,
231231
]
232232
)
233+
TESTS.append( # 5D input
234+
[
235+
{"pixdim": 0.5, "padding_mode": "zeros", "mode": "nearest", "scale_extent": True},
236+
torch.ones((1, 368, 336, 368)), # data
237+
torch.tensor(
238+
[
239+
[0.41, 0.005, 0.008, -79.7],
240+
[-0.0049, 0.592, 0.0664, -57.4],
241+
[-0.0073, -0.0972, 0.404, -32.1],
242+
[0.0, 0.0, 0.0, 1.0],
243+
]
244+
),
245+
{},
246+
torch.ones((1, 302, 403, 301)),
247+
*device,
248+
]
249+
)
250+
233251

234252
TESTS_TORCH = []
235253
for track_meta in (False, True):

0 commit comments

Comments
 (0)