This repository contains the complete code for converting nifti files to dicom series. I needed this conversion during my internship while working on tumor segmentation, so I wanted to share the method I discovered with you.
You can simply clone this repository and begin using it. All of the details can be found on my blog, which I dedicated to this function.
You can install the requirements with:
pip install -r requirements.txt
This is the main function that does all the work:
def convertNsave(arr,file_dir, index=0):
"""
`arr`: parameter will take a numpy array that represents only one slice.
`file_dir`: parameter will take the path to save the slices
`index`: parameter will represent the index of the slice, so this parameter will be used to put
the name of each slice while using a for loop to convert all the slices
"""
dicom_file = pydicom.dcmread('images/dcmimage.dcm')
arr = arr.astype('uint16')
dicom_file.Rows = arr.shape[0]
dicom_file.Columns = arr.shape[1]
dicom_file.PhotometricInterpretation = "MONOCHROME2"
dicom_file.SamplesPerPixel = 1
dicom_file.BitsStored = 16
dicom_file.BitsAllocated = 16
dicom_file.HighBit = 15
dicom_file.PixelRepresentation = 1
dicom_file.PixelData = arr.tobytes()
dicom_file.save_as(os.path.join(file_dir, f'slice{index}.dcm'))
Version | Description | Limitation |
---|---|---|
1.0.0 | Convert nifti to dicom by filling an existing dicom file with the new information | All the slices have the same Series Number |
1.1.0 | The same method as the previous version | The generated dicoms can't be opened in all the medical imaging software |
2.0.0 | New method based on SimpleITK | All the previous issues are resolved |
Full course about medical imaging segmentation is coming soon, join the waitlist here: