How to extract multiple components image from dicom series?

The dicom files can be correctly recognized as multiple components image by ITK-SNAP:

The proof is : I can change the component/view of the dicom file:

But when I extract nii.gz file from the dicom series, the views/components are concatenated together, just like:

Are there any problems of my python code? Could you help me to extract the nii.gz file from the dicom series by a correct way?

here is my python code:

def read_dicom(src_dir, out_dir):
    if not os.path.exists(out_dir):
        os.mkdir(out_dir)
        
    PixelType = itk.ctype("signed short")
    Dimension = 3

    ImageType = itk.Image[PixelType, Dimension] 

    namesGenerator = itk.GDCMSeriesFileNames.New()
    namesGenerator.SetUseSeriesDetails(True)
    namesGenerator.SetGlobalWarningDisplay(False)
    namesGenerator.SetDirectory(src_dir)

    seriesUID = namesGenerator.GetSeriesUIDs()

    if len(seriesUID) < 1:
        print("No DICOMs in: " + src_dir)
        sys.exit(1)
    
    for uid in seriesUID:
        seriesIdentifier = uid
        fileNames = namesGenerator.GetFileNames(seriesIdentifier)
        reader = itk.ImageSeriesReader[ImageType].New()
        dicomIO = itk.GDCMImageIO.New()
        dicomIO.LoadPrivateTagsOn()
        reader.SetImageIO(dicomIO)
        reader.SetFileNames(fileNames)
        reader.ForceOrthogonalDirectionOff()

        image = reader.GetOutput()
        print("IMage Size: ", image.GetLargestPossibleRegion().GetSize())
        writer = itk.ImageFileWriter[ImageType].New()
        writer.SetFileName("./results.nii.gz")
        writer.UseCompressionOn()
        writer.SetInput(image)
        writer.Update()

Thanks for your help!!! Please!!! :blush:

I found that itk-snap can recognize the number of volume/components.

Does your PixelType need to be a Vector?

I’m not an ITK-Python guy, but here’s an example for creating an image of vectors:

https://examples.itk.org/src/core/common/createanimageofvectors/documentation

Yes, you are right. Thanks for reminding me about the PixelType.
The PixelType need to be a Vector. But after I set as follow:

ComponentType = itk.ctype("float")
PixelType = itk.Vector[ComponentType, 2]
ImageType = itk.Image[PixelType, Dimension]

It still didn’t work. It does read pixel as vector, but the value of the vector is the same one.

Now, I think that my problem has become how to read dicom as image of vector. That’s right?

Hi @jinquan_guan ,

It may be worth trying

itk.imread

A directory with DICOM files can be passed in directly.

The pixel type is auto-detected by default. If that is not desired or possible, a desired type can passed. If series restrictions are needed, the series_uid kwarg can be set. More information can be found with help(itk.imread).