A Python tool for converting DICOM series into 3D volume data in MHD or NIfTI format. This tool automatically processes DICOM files, extracts metadata, calculates slice spacing, and generates properly formatted 3D medical images.
pydicom numpy SimpleITK
text
pip install pydicom numpy SimpleITK Usage Command Line Interface bash # Convert to MHD format python dicom_converter.py /path/to/dicom/folder output_volume.mhd # Convert to NIfTI format python dicom_converter.py /path/to/dicom/folder output_volume.nii.gz Parameters input_folder: Path to folder containing DICOM files (.dcm) output_file: Output file path (.mhd or .nii.gz) Processing Pipeline 1. DICOM File Discovery Scans specified folder for .dcm files Validates DICOM file integrity Reports total number of files found 2. Instance Number Sorting Reads InstanceNumber from DICOM headers Sorts files in correct slice order Ensures proper volume reconstruction 3. Metadata Extraction Extracted Metadata: Pixel spacing (X, Y dimensions) Slice spacing (Z dimension) Image orientation and position Rows and columns dimensions Bits allocated and stored Pixel representation 4. Slice Spacing Calculation Primary Methods: SliceLocation Difference: Uses SliceLocation DICOM tag ImagePositionPatient: Calculates Euclidean distance between slices Fallback: Defaults to 1.0 mm if calculation fails 5. Volume Data Creation Reads pixel arrays from all DICOM files Applies rescaling (slope and intercept) if needed Stacks slices into 3D volume array Maintains original data type and values 6. File Export Supported Formats: MHD/RAW: MetaImage format with enhanced metadata NIfTI: Standard neuroimaging format Output Specifications MHD Format Features Proper voxel spacing in all three dimensions Transform matrix for correct orientation Anatomical orientation (RAS) Offset and center of rotation information NIfTI Format Features Correct spatial coordinates Proper voxel dimensions Compatible with most medical imaging software