I have dicom images converted to nifti format. I need to find if the images are of carotid or coronary vessels from the metadata. I have searched and am convinced that these metadata are not available in Nifti.header.
Are there ways to find that in dicoms using any python package? I can get the metadata using some C++ tools but need to implement the same in python.
Indeed with nifti you loose all of the rich DICOM header information.
Using SimpleITK (>=2.1.0) and your original DICOM image:
import SimpleITK as sitk
image = sitk.ReadImage('my_dicom_image.dcm')
# ignore leading/trailing whitespace and upper/lower case
if image['0018|0015'].strip().lower()== 'carotid':
print('carotid')
(512, 512, 559)
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-19-372160a971c2> in <module>
2 dcmItk = dicomread(casePath) #, imgitk=True)
3 print(dcmItk.GetSize())
----> 4 if dcmItk['0018|0015'].strip().lower()== 'carotid':
5 print('carotid')
~/miniconda3/envs/carotid36/lib/python3.6/site-packages/SimpleITK/SimpleITK.py in __getitem__(self, idx)
3724
3725 if (len(idx) > dim):
-> 3726 raise IndexError("too many indices for image")
3727
3728 # All the indices are integers just return GetPixel value
IndexError: too many indices for image
Another question: are these values 0018|0015 indicate anatomical metadata for all the dicoms?
Additional info: the dicom files nor have .dcm extension neither in single file. Rather are in multiple series/slices. dicomread function encompasses the following codes to read the image given the case/folder path:
The file extension doesn’t matter as you called the reader’s GetGDCMSeriesFileNames which will try all files and return the files associated with the first DICOM series it encounters.
The tag, 0018|0015 corresponds to BodyPartExamined, which is defined by the DICOM standard.
To see which DICOM tags were loaded: image.GetMetaDataKeys().
You are likely using a SimpleITK version <2.1.0, hence the error (see original response above which specifically says version >=2.1.0). For older versions the syntax is image.GetMetaData('0018|0015').strip().lower()
I see, then the metadata is not available inherently.
Are there reasons for image.GetMetaDataKeys() not printing anything?
How did you read the metadata? I am reading dicoms using dicomread shared in previous replies.
It is multi-frame IOD, only a very small part of DICOM attributes is visible, ‘metadata dictionary’ contains only top-level tags. In particular case there is related sequence is shared groups, but it is empty
I can read the metadata using the following itk functions for dicom or given a casePath_(name of the directory)
imageType = itk.Image[itk.F, 3]
# print(imageType)
reader = itk.ImageSeriesReader[imageType].New()
dicomIO = itk.GDCMImageIO.New() # GDCMImageIO is not available in sitk
dicomFN = itk.GDCMSeriesFileNames.New()
reader.SetImageIO(dicomIO)
dicomFN.SetUseSeriesDetails(True)
dicomFN.SetDirectory(casePath_)
uids = dicomFN.GetSeriesUIDs()
fnames = dicomFN.GetFileNames(uids[0])
reader.SetFileNames(fnames)
reader.Update()
# image = reader.GetOutput()
# ##------------------------------------------------------------------------------##
metaData = dicomIO.GetMetaDataDictionary()
# ## now you can access the meta data using indexing by key.
# ## The key we are conserned with is the body site key '0018|0015'
print(metaData['0018|0015'])
Are there any such versions in sitk to read the metadata?
What I figured that sitk has no GDCMImageIO unlike itk and maybe sitk doesn’t support all the metadata readability as itk
Hello, sitk.ReadImage only works when the dicom files are saved in a single file with .dcm extension. But many dicom files are stored as multiple slices in a single directory. How do you suggest reading those dicoms’ metadata.
I use the following function to read both single .dcm and multiple sliced dicom images.
def dicomread(casePath, imgitk=None):
if not os.path.isdir(casePath):
print("The path doesn't exist.")
reader = sitk.ImageSeriesReader()
dicom_names = reader.GetGDCMSeriesFileNames(casePath)
reader.SetFileNames(dicom_names)
image = reader.Execute() # type --> 'SimpleITK.SimpleITK.Image'
if image.GetDimension() == 4 and image.GetSize()[3] == 1:
# image = sitk.GetArrayFromImage(image)[0,...]
image = image[:, :, :, 0]
return image
would it be possible to add the metadata part you mentioned inside/with this function?
The reader in your code contains all of the metadata. Please see this usage example on read-the-docs, the relevant function is GetMetaData.
Highly recommend that you spend some time skimming the SimpleITK documentation, specifically the examples on read the docs and the Jupyter notebooks to better familiarize yourself with the toolkit.