I use the following code snippet for manipulating DICOM tags on individual slice:
...
# insert tags per slice
general_metadata = gdcmIO.GetMetaDataDictionary()
metadata_array = []
for slice_counter in range(1, num_slices):
slice_metadata = itk.MetaDataDictionary(general_metadata)
# instance number
slice_metadata['0020|0013'] = slice_counter
metadata_array.append(slice_metadata)
seriesWriter.SetMetaDataDictionaryArray(metadata_array)
...
The entire program can be seen in the bottom of the post if needed.
I however get the following error when executing my program:
Traceback (most recent call last):
File "nifti2dicom.py", line 74, in <module>
nifti_to_dicom(args.input_file, args.output_dir, args.output_prefix)
File "nifti2dicom.py", line 61, in nifti_to_dicom
seriesWriter.SetMetaDataDictionaryArray(metadata_array)
TypeError: in method 'itkImageSeriesWriterIUS3IUS2_SetMetaDataDictionaryArray', argument 2 of type 'std::vector< itkMetaDataDictionary *,std::allocator< itkMetaDataDictionary * > > const *const'
I have tested many different ways of parsing the metadata_array to the SetMetaDataDictionaryArray function but cannot seem to find the correct way of doing this.
Any help or pointer to information would be of great value.
The full program code for convert a nifti file to a DICOM Series:
# inspired by: https://itk.org/Doxygen50/html/Examples_2IO_2ImageReadDicomSeriesWrite_8cxx-example.html
import argparse
import os
import itk
def nifti_to_dicom(input_filename, output_dir, prefix):
PixelType = itk.US
Dimension = 3
ReaderType = itk.Image[PixelType, Dimension]
reader = itk.ImageSeriesReader[ReaderType].New()
dicomIO = itk.NiftiImageIO.New()
reader.SetImageIO(dicomIO)
reader.SetFileName(input_filename)
reader.Update()
image = reader.GetOutput()
region = image.GetLargestPossibleRegion()
start = region.GetIndex()
size = region.GetSize()
namesGenerator = itk.NumericSeriesFileNames.New()
format = output_dir + "/" + prefix + "%04d.dcm"
namesGenerator.SetSeriesFormat(format)
start_index = start[2] + 1
namesGenerator.SetStartIndex(start_index)
end_index = start_index + size[2] - 1
namesGenerator.SetEndIndex(end_index)
namesGenerator.SetIncrementIndex(1)
OutputPixelType = itk.US
OutputDimension = 2
SeriesWriterType = itk.Image[OutputPixelType, OutputDimension]
seriesWriter = itk.ImageSeriesWriter[ReaderType, SeriesWriterType].New()
seriesWriter.SetInput(image)
seriesWriter.SetFileNames(namesGenerator.GetFileNames())
gdcmIO = itk.GDCMImageIO.New()
# inspired by: https://itk.org/ITKExamples/src/IO/GDCM/ReadAndPrintDICOMTags/Documentation.html
metadata = gdcmIO.GetMetaDataDictionary()
slice_spacing = image.GetSpacing()[2]
# format, removes 0 from e.g. 1.0 if it is 0
metadata['0018|0050'] = '{0:g}'.format(slice_spacing)
metadata['0008|0060'] = "MR"
seriesWriter.SetImageIO(gdcmIO)
# insert tags per slice
# inspired by: https://itk.org/ITKExamples/src/IO/GDCM/ResampleDICOMSeries/Documentation.html
num_slices = size[2]
general_metadata = gdcmIO.GetMetaDataDictionary()
metadata_array = []
for slice_counter in range(1, num_slices):
slice_metadata = itk.MetaDataDictionary(general_metadata)
# instance number
slice_metadata['0020|0013'] = slice_counter
metadata_array.append(slice_metadata)
seriesWriter.SetMetaDataDictionaryArray(metadata_array)
os.makedirs(output_dir, exist_ok=True)
seriesWriter.Update()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input-file', required=True)
parser.add_argument('-o', '--output-dir', required=True)
parser.add_argument('-p', '--output-prefix', default="slice_")
args = parser.parse_args()
nifti_to_dicom(args.input_file, args.output_dir, args.output_prefix)