@@ -46,13 +46,16 @@ def is_dicom_file_or_directory(path: PathLike) -> bool:
4646 return False
4747
4848
49- def extract_affine_from_dicom (dicom_slices : list ) -> np .ndarray :
49+ def extract_affine_from_dicom (
50+ dicom_slices : list [tuple [int , np .ndarray , pydicom .Dataset ]]
51+ ) -> np .ndarray :
5052 """
5153 Extract the affine transformation matrix from DICOM header information.
5254 Converts from DICOM LPS (Left-Posterior-Superior) to NIfTI RAS.
5355
5456 Args:
55- dicom_slices: List of tuples (instance_number, pixel_array, dicom_dataset)
57+ dicom_slices: List of tuples (instance_number, pixel_array, header) where
58+ header is a pydicom.Dataset containing DICOM metadata tags
5659
5760 Returns:
5861 4x4 affine transformation matrix mapping voxel coordinates to RAS world coordinates
@@ -61,19 +64,19 @@ def extract_affine_from_dicom(dicom_slices: list) -> np.ndarray:
6164 RuntimeError: If required DICOM tags are missing
6265 """
6366 # use the first slice to extract most parameters
64- first_ds = dicom_slices [0 ][2 ]
67+ first_header = dicom_slices [0 ][2 ]
6568
6669 try :
6770 # ImageOrientationPatient (0020,0037): direction cosines for row and column
68- orientation = np .array (first_ds .ImageOrientationPatient , dtype = float )
71+ orientation = np .array (first_header .ImageOrientationPatient , dtype = float )
6972 row_cosine = orientation [:3 ] # direction cosines for rows
7073 col_cosine = orientation [3 :] # direction cosines for columns
7174
7275 # ImagePositionPatient (0020,0032): position of the upper-left voxel
73- position = np .array (first_ds .ImagePositionPatient , dtype = float )
76+ position = np .array (first_header .ImagePositionPatient , dtype = float )
7477
7578 # PixelSpacing is [row_spacing, col_spacing], so map to dy, dx
76- dy , dx = np .array (first_ds .PixelSpacing , dtype = float )
79+ dy , dx = np .array (first_header .PixelSpacing , dtype = float )
7780
7881 except AttributeError as e :
7982 raise RuntimeError (
@@ -91,7 +94,7 @@ def extract_affine_from_dicom(dicom_slices: list) -> np.ndarray:
9194 dz = np .dot (second_pos - first_pos , slice_cosine )
9295 else :
9396 # single slice - try to get from SliceThickness or default to 1.0
94- dz = float (getattr (first_ds , 'SliceThickness' , 1.0 ))
97+ dz = float (getattr (first_header , 'SliceThickness' , 1.0 ))
9598
9699 # Construct affine in DICOM LPS space
97100 affine = np .eye (4 )
@@ -131,9 +134,9 @@ def convert_dicom_to_nifti(input_path: PathLike, output_filepath: PathLike) -> N
131134 slices = []
132135 for dcm_file in dicom_files :
133136 try :
134- ds = pydicom .dcmread (dcm_file )
137+ header = pydicom .dcmread (dcm_file )
135138 # Transpose to swap (Row, Col) -> (X, Y) for NIfTI
136- slices .append ((ds .get ('InstanceNumber' , 0 ), ds .pixel_array .T , ds ))
139+ slices .append ((header .get ('InstanceNumber' , 0 ), header .pixel_array .T , header ))
137140 except Exception :
138141 # skip files that aren't valid dicom
139142 continue
0 commit comments