# How to extract multiple components image from dicom series?

**URL:** https://discourse.itk.org/t/how-to-extract-multiple-components-image-from-dicom-series/7097
**Category:** Beginner Questions
**Created:** [July 20, 2024, 4:34am UTC](https://discourse.itk.org/t/how-to-extract-multiple-components-image-from-dicom-series/7097 "2024-07-20T04:34:52Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jinquan\_guan](https://discourse.itk.org/user_avatar/discourse.itk.org/jinquan_guan/32/4299_2.png) [@jinquan\_guan](https://discourse.itk.org/u/jinquan_guan)
#### Post date: [July 20, 2024, 4:34am UTC](https://discourse.itk.org/t/how-to-extract-multiple-components-image-from-dicom-series/7097/1 "2024-07-20T04:34:53Z")

</div>

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

 ![image](https://discourse.itk.org/uploads/default/original/2X/7/764ab955fc2e65dfef01357e5182c9b50d030d54.png)

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

 ![image](https://discourse.itk.org/uploads/default/original/2X/b/b41a42bc2b07831be3a8ddf43ae47bd046bed298.png)

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

 ![image](https://discourse.itk.org/uploads/default/original/2X/f/ff2aa1a1e76259a36137f25f2372536803481831.png)

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:

```auto
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!!! 😊

---

<div class="post-metadata">

### Author: ![jinquan\_guan](https://discourse.itk.org/user_avatar/discourse.itk.org/jinquan_guan/32/4299_2.png) [@jinquan\_guan](https://discourse.itk.org/u/jinquan_guan)
#### Post date: [July 20, 2024, 7:54am UTC](https://discourse.itk.org/t/how-to-extract-multiple-components-image-from-dicom-series/7097/2 "2024-07-20T07:54:15Z")

</div>

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

 ![image](https://discourse.itk.org/uploads/default/original/2X/6/6309868f4adea7de86447c5f7ebf9348dbcd8dff.png)

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [July 20, 2024, 5:31pm UTC](https://discourse.itk.org/t/how-to-extract-multiple-components-image-from-dicom-series/7097/3 "2024-07-20T17:31:20Z")

</div>

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](https://examples.itk.org/src/core/common/createanimageofvectors/documentation)

---

<div class="post-metadata">

### Author: ![jinquan\_guan](https://discourse.itk.org/user_avatar/discourse.itk.org/jinquan_guan/32/4299_2.png) [@jinquan\_guan](https://discourse.itk.org/u/jinquan_guan)
#### Post date: [July 21, 2024, 5:02pm UTC](https://discourse.itk.org/t/how-to-extract-multiple-components-image-from-dicom-series/7097/4 "2024-07-21T17:02:17Z")

</div>

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

```auto
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.

 ![image](https://discourse.itk.org/uploads/default/original/2X/1/1a6ad03b3b30099e0cda7b6932fdc4cc84a69077.png)

---

<div class="post-metadata">

### Author: ![jinquan\_guan](https://discourse.itk.org/user_avatar/discourse.itk.org/jinquan_guan/32/4299_2.png) [@jinquan\_guan](https://discourse.itk.org/u/jinquan_guan)
#### Post date: [July 21, 2024, 5:10pm UTC](https://discourse.itk.org/t/how-to-extract-multiple-components-image-from-dicom-series/7097/5 "2024-07-21T17:10:49Z")

</div>

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

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [July 23, 2024, 2:53pm UTC](https://discourse.itk.org/t/how-to-extract-multiple-components-image-from-dicom-series/7097/6 "2024-07-23T14:53:31Z")

</div>

Hi @jinquan_guan ,

It may be worth trying

```auto
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)`.
