From 8992c98bc7e0ea2ea4f7c2a9e7e7d344cf3c15fe Mon Sep 17 00:00:00 2001 From: Mathieu Goulet Date: Sun, 6 Jul 2025 23:40:51 -0400 Subject: [PATCH] Update image_helper.py --- rt_utils/image_helper.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/rt_utils/image_helper.py b/rt_utils/image_helper.py index b030987..5770b42 100644 --- a/rt_utils/image_helper.py +++ b/rt_utils/image_helper.py @@ -161,8 +161,10 @@ def get_pixel_to_patient_transformation_matrix(series_data): row_direction, column_direction, slice_direction = get_slice_directions(first_slice) mat = np.identity(4, dtype=np.float32) - mat[:3, 0] = row_direction * row_spacing - mat[:3, 1] = column_direction * column_spacing + #The following might appear counter-intuitive, i.e. multiplying the row direction with the column spacing and vice-versa + #But is the correct way to create the transformation matrix, see https://nipy.org/nibabel/dicom/dicom_orientation.html + mat[:3, 0] = row_direction * column_spacing + mat[:3, 1] = column_direction * row_spacing mat[:3, 2] = slice_direction * slice_spacing mat[:3, 3] = offset @@ -183,9 +185,11 @@ def get_patient_to_pixel_transformation_matrix(series_data): # inv(M) = [ inv(rotation&scaling) -inv(rotation&scaling) * translation ] # [ 0 1 ] + #The following might appear counter-intuitive, i.e. dividing the row direction with the column spacing and vice-versa + #But is the correct way to create the inverse transformation matrix, see https://nipy.org/nibabel/dicom/dicom_orientation.html linear = np.identity(3, dtype=np.float32) - linear[0, :3] = row_direction / row_spacing - linear[1, :3] = column_direction / column_spacing + linear[0, :3] = row_direction / column_spacing + linear[1, :3] = column_direction / row_spacing linear[2, :3] = slice_direction / slice_spacing mat = np.identity(4, dtype=np.float32)