Skip to content

Commit 2adb7fb

Browse files
committed
fix issue with non-diagonal direction matrix
1 parent 12f4d4b commit 2adb7fb

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

src/itk_torch_affine_matrix_bridge.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def compute_reference_space_affine_matrix(image, ref_image):
9494
ref_direction_matrix, ref_inv_direction_matrix = [m[:ndim, :ndim].numpy() for m in compute_direction_matrix(ref_image)]
9595

9696
# Matrix calculation
97-
matrix = ref_direction_matrix @ ref_spacing_matrix @ inv_direction_matrix @ inv_spacing_matrix
97+
matrix = ref_direction_matrix @ ref_spacing_matrix @ inv_spacing_matrix @ inv_direction_matrix
9898

9999
# Offset calculation
100100
pixel_offset = -1
@@ -123,8 +123,7 @@ def itk_to_monai_affine(image, matrix, translation, center_of_rotation=None, ref
123123
between the center of the image and the center of rotation.
124124
reference_image: The coordinate space that matrix and translation were defined
125125
in respect to. If not supplied, the coordinate space of image
126-
is used. Note: it does not work properly when the direction
127-
matrix is non-diagonal.
126+
is used.
128127
129128
Returns:
130129
A 4x4 MONAI affine matrix.
@@ -149,17 +148,17 @@ def itk_to_monai_affine(image, matrix, translation, center_of_rotation=None, ref
149148
offset_matrix, inverse_offset_matrix = compute_offset_matrix(image, center_of_rotation)
150149
affine_matrix = inverse_offset_matrix @ affine_matrix @ offset_matrix
151150

151+
# Adjust direction
152+
direction_matrix, inverse_direction_matrix = compute_direction_matrix(image)
153+
affine_matrix = inverse_direction_matrix @ affine_matrix @ direction_matrix
154+
152155
# Adjust based on spacing. It is required because MONAI does not update the
153156
# pixel array according to the spacing after a transformation. For example,
154157
# a rotation of 90deg for an image with different spacing along the two axis
155158
# will just rotate the image array by 90deg without also scaling accordingly.
156159
spacing_matrix, inverse_spacing_matrix = compute_spacing_matrix(image)
157160
affine_matrix = inverse_spacing_matrix @ affine_matrix @ spacing_matrix
158161

159-
# Adjust direction
160-
direction_matrix, inverse_direction_matrix = compute_direction_matrix(image)
161-
affine_matrix = inverse_direction_matrix @ affine_matrix @ direction_matrix
162-
163162
return affine_matrix @ reference_affine_matrix
164163

165164
def monai_to_itk_affine(image, affine_matrix, center_of_rotation=None):

src/test_cases.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,16 @@ def test_use_reference_space(ref_filepath, filepath):
186186
image.SetSpacing([1.2, 2.0, 1.7][:ndim])
187187
ref_image.SetSpacing([1.9, 1.5, 1.3][:ndim])
188188

189-
direction = np.eye(3, dtype=np.float64)
190-
direction[0, 0] = 0.68
191-
direction[1, 1] = 1.05
192-
direction[2, 2] = 1.83
189+
direction = np.array([[1.02895588, 0.22791448, 0.02429561],
190+
[0.21927512, 1.28632268, -0.14932226],
191+
[0.47455613, 0.38534345, 0.98505633]],
192+
dtype=np.float64)
193193
image.SetDirection(direction[:ndim, :ndim])
194194

195-
ref_direction = np.eye(3, dtype=np.float64)
196-
ref_direction[0, 0] = 1.25
197-
ref_direction[1, 1] = 0.99
198-
ref_direction[2, 2] = 1.50
195+
ref_direction = np.array([[1.26032417, -0.19243174, 0.54877414],
196+
[0.31958275, 0.9543068, 0.2720827],
197+
[-0.24106769, -0.22344502, 0.9143302]],
198+
dtype=np.float64)
199199
ref_image.SetDirection(ref_direction[:ndim, :ndim])
200200

201201
image.SetOrigin([57.3, 102.0, -20.9][:ndim])
@@ -224,8 +224,4 @@ def test_use_reference_space(ref_filepath, filepath):
224224
print("[Min, Max] ITK: [{}, {}]".format(output_array_itk.min(), output_array_itk.max()))
225225
print("[Min, Max] diff: [{}, {}]".format(diff_output.min(), diff_output.max()))
226226

227-
itk.imwrite(itk.GetImageFromArray(diff_output), "./output/diff.tif")
228-
itk.imwrite(itk.GetImageFromArray(output_array_monai), "./output/output_monai.tif")
229-
itk.imwrite(itk.GetImageFromArray(output_array_itk), "./output/output_itk.tif")
230-
231227

0 commit comments

Comments
 (0)