diff --git a/monai/data/image_reader.py b/monai/data/image_reader.py index a640070d16..778f2ca225 100644 --- a/monai/data/image_reader.py +++ b/monai/data/image_reader.py @@ -227,7 +227,9 @@ def _get_spatial_shape(self, img) -> Sequence: img: a ITK image object loaded from a image file. """ - return list(itk.size(img)) + shape = list(itk.size(img)) + shape.reverse() + return shape def _get_array_data(self, img) -> np.ndarray: """ diff --git a/tests/test_load_image.py b/tests/test_load_image.py index caa3bf84a0..9399afaf50 100644 --- a/tests/test_load_image.py +++ b/tests/test_load_image.py @@ -128,6 +128,7 @@ def test_itk_dicom_series_reader(self, input_param, filenames, expected_shape): ), ), self.assertTupleEqual(result.shape, expected_shape) + self.assertTupleEqual(tuple(header["spatial_shape"]), expected_shape) def test_load_png(self): spatial_size = (256, 256) @@ -144,7 +145,6 @@ def test_load_png(self): def test_register(self): spatial_size = (32, 64, 128) - expected_shape = (128, 64, 32) test_image = np.random.rand(*spatial_size) with tempfile.TemporaryDirectory() as tempdir: filename = os.path.join(tempdir, "test_image.nii.gz") @@ -154,7 +154,7 @@ def test_register(self): loader = LoadImage(image_only=False) loader.register(ITKReader()) result, header = loader(filename) - self.assertTupleEqual(tuple(header["spatial_shape"]), expected_shape) + self.assertTupleEqual(tuple(header["spatial_shape"]), spatial_size) self.assertTupleEqual(result.shape, spatial_size) def test_kwargs(self): diff --git a/tests/test_load_imaged.py b/tests/test_load_imaged.py index d85c6cfab4..396167046b 100644 --- a/tests/test_load_imaged.py +++ b/tests/test_load_imaged.py @@ -42,7 +42,6 @@ def test_shape(self, input_param, expected_shape): def test_register(self): spatial_size = (32, 64, 128) - expected_shape = (128, 64, 32) test_image = np.random.rand(*spatial_size) with tempfile.TemporaryDirectory() as tempdir: filename = os.path.join(tempdir, "test_image.nii.gz") @@ -52,7 +51,7 @@ def test_register(self): loader = LoadImaged(keys="img") loader.register(ITKReader()) result = loader({"img": filename}) - self.assertTupleEqual(tuple(result["img_meta_dict"]["spatial_shape"]), expected_shape) + self.assertTupleEqual(tuple(result["img_meta_dict"]["spatial_shape"]), spatial_size) self.assertTupleEqual(result["img"].shape, spatial_size)