diff --git a/monai/data/image_reader.py b/monai/data/image_reader.py index f5e199e2a3..b4ae562911 100644 --- a/monai/data/image_reader.py +++ b/monai/data/image_reader.py @@ -1359,7 +1359,7 @@ def _get_affine(self, header: dict) -> np.ndarray: x, y = direction.shape affine_diam = min(x, y) + 1 affine: np.ndarray = np.eye(affine_diam) - affine[:x, :y] = direction + affine[:x, :y] = direction.T affine[: (affine_diam - 1), -1] = origin # len origin is always affine_diam - 1 return affine diff --git a/tests/test_nrrd_reader.py b/tests/test_nrrd_reader.py index 649b9fa94d..5bf958e970 100644 --- a/tests/test_nrrd_reader.py +++ b/tests/test_nrrd_reader.py @@ -40,8 +40,8 @@ "dimension": 4, "space": "left-posterior-superior", "sizes": [3, 4, 4, 1], - "space directions": [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]], - "space origin": [0.0, 0.0, 0.0], + "space directions": [[0.7, 0.0, 0.0], [0.0, 0.0, -0.8], [0.0, 0.9, 0.0]], + "space origin": [1.0, 5.0, 20.0], }, ] @@ -110,6 +110,10 @@ def test_read_with_header(self, data_shape, filename, expected_shape, dtype, ref np.testing.assert_allclose(image_array, test_image) self.assertIsInstance(image_header, dict) self.assertTupleEqual(tuple(image_header["spatial_shape"]), expected_shape) + np.testing.assert_allclose( + image_header["affine"], + np.array([[-0.7, 0.0, 0.0, -1.0], [0.0, 0.0, -0.9, -5.0], [0.0, -0.8, 0.0, 20.0], [0.0, 0.0, 0.0, 1.0]]), + ) @parameterized.expand([TEST_CASE_8]) def test_read_with_header_index_order_c(self, data_shape, filename, expected_shape, dtype, reference_header):