# Convert single files into one .nii

**URL:** https://discourse.slicer.org/t/convert-single-files-into-one-nii/5407
**Category:** Support
**Tags:** dicom
**Created:** [January 17, 2019, 9:55pm UTC](https://discourse.slicer.org/t/convert-single-files-into-one-nii/5407 "2019-01-17T21:55:45Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![siaeleni](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@siaeleni](https://discourse.slicer.org/u/siaeleni)
#### Post date: [January 17, 2019, 9:55pm UTC](https://discourse.slicer.org/t/convert-single-files-into-one-nii/5407/1 "2019-01-17T21:55:45Z")

</div>

Hi,

I have multiple single .ima files. These contain single slices info.  
What I want to achieve is to merge all these into one .nii file.

What is the best way to do that?  
Thanks

---

<div class="post-metadata">

### Author: ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)
#### Post date: [January 17, 2019, 10:17pm UTC](https://discourse.slicer.org/t/convert-single-files-into-one-nii/5407/2 "2019-01-17T22:17:49Z")

</div>

Dear Helen,  
You could use [dcm2nii](http://people.cas.sc.edu/rorden/mricron/dcm2nii.html).  
Best.

---

<div class="post-metadata">

### Author: ![siaeleni](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@siaeleni](https://discourse.slicer.org/u/siaeleni)
#### Post date: [January 17, 2019, 10:25pm UTC](https://discourse.slicer.org/t/convert-single-files-into-one-nii/5407/3 "2019-01-17T22:25:32Z")

</div>

Thanks Frederic,  
I want to use python for this task, can I achieve it using dcm2nii?

---

<div class="post-metadata">

### Author: ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)
#### Post date: [January 17, 2019, 10:36pm UTC](https://discourse.slicer.org/t/convert-single-files-into-one-nii/5407/4 "2019-01-17T22:36:18Z")

</div>

Not directly, but yest it’s possible, for example by a thing like that:  
After that you load MRIcroGL (dcm2niix)

> import os  
> command1 = “dcm2nix /YourDirectory/data.IMA”  
> os.system(command1)

---

<div class="post-metadata">

### Author: ![Chris\_Rorden](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chris_rorden/32/4073_2.png) [@Chris\_Rorden](https://discourse.slicer.org/u/Chris_Rorden)
#### Post date: [January 18, 2019, 2:15pm UTC](https://discourse.slicer.org/t/convert-single-files-into-one-nii/5407/5 "2019-01-18T14:15:16Z")

</div>

The [**Alternatives** section of the dcm2niix page lists several tools for converting DICOM images to NIfTI](https://github.com/rordenlab/dcm2niix). The [**Links** section](https://github.com/rordenlab/dcm2niix) lists several tools that exploit dcm2niix, many of these are Python-based scripts. I think [dicom2nifti](https://github.com/icometrix/dicom2nifti) would be my recommendation for Python-based converter. In my experience, most of the tools listed in the alternatives section do pretty well for most modalities, but you want to carefully validate the converter you have special modalities. In particular, CT scans can have unequal slice spacing and gantry tilt while DWI scans have gradient information. If you are planning to use Slicer in particular, you may want to consider scripting DWIconvert, as is used my most Slicer users and therefore heavily tested. The nice [nrrdify Python script shows how to use DWIconvert to create NRRD images](https://github.com/pieper/nrrdify) and I think it could be easily adapted if you want NIfTI images.

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [January 18, 2019, 2:38pm UTC](https://discourse.slicer.org/t/convert-single-files-into-one-nii/5407/6 "2019-01-18T14:38:02Z")

</div>

> [@Chris\_Rorden](#):
>
> DWIconvert, as is used my most Slicer users

It is used for DWI import in Slicer, but various DICOM plugins are used for other information objects.

How dcm2niix can handle 4D CTs with unequal spacing, missing and unaligned slices, and gantry tilt? Does dcm2niix support CT reconstruction based on different DICOM tags (frame time, heart cycle phase, various vendor-specific tags)? Can dcm2niix can load multiple series as a 4D volume? If there are many options for interpreting a 4D data set, how dcm2niix tells the user the available options (and probability of which ones are thr most likely correct interpretations) and how the user can choose between those? Slicer has a sophisticated plugin system that can deal with all these, but it would be nice if some of this complex logic could be offloaded to external libraries.

---

<div class="post-metadata">

### Author: ![ihnorton](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ihnorton/32/9_2.png) [@ihnorton](https://discourse.slicer.org/u/ihnorton)
#### Post date: [January 18, 2019, 2:50pm UTC](https://discourse.slicer.org/t/convert-single-files-into-one-nii/5407/7 "2019-01-18T14:50:57Z")

</div>

> [@siaeleni](#):
>
> I want to use python for this task, can I achieve it using dcm2nii?

You can run dcm2niix via [heudiconv](https://github.com/nipy/heudiconv) or [nipype](https://github.com/nipy/nipype).

---

<div class="post-metadata">

### Author: ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)
#### Post date: [January 18, 2019, 3:09pm UTC](https://discourse.slicer.org/t/convert-single-files-into-one-nii/5407/8 "2019-01-18T15:09:37Z")

</div>

@fedorov and a group of us researched the various options for dicom conversion a while back and collected our notes [on this project page](https://na-mic.github.io/ProjectWeek/PW27_2018_Boston/Projects/DICOMVolumeReconstruction/) . There’s also a [docker based test environment](https://github.com/QIICR/dcmheat) that runs the various tools (slicer, freesurfer, dcm2niix, etc) and compares the results.

As a general rule, it’s best if you don’t need to perform lossy conversions, so keeping the original dicom data is desirable. Also storing derived data in dicom is the method many of us prefer (or are at least working towards).

---

<div class="post-metadata">

### Author: ![Chris\_Rorden](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chris_rorden/32/4073_2.png) [@Chris\_Rorden](https://discourse.slicer.org/u/Chris_Rorden)
#### Post date: [January 19, 2019, 12:18pm UTC](https://discourse.slicer.org/t/convert-single-files-into-one-nii/5407/9 "2019-01-19T12:18:14Z")

</div>

@lassoan I recognize Slicer has several strategies for handling DICOMs. My response was specific to the original post regarding converting files to a single .nii file.

With regards to special situations encountered with DICOMs, you may want to look at the samples that can be downloaded from the [dcm2niix web page](https://www.nitrc.org/plugins/mwiki/index.php/dcm2nii:MainPage). This provides exemplars of archival images, CT scans, DWI scans, and special situations.

The web page also describes how dcm2niix handles various situations, including those described below.

> How dcm2niix can handle 4D CTs with unequal spacing, missing and unaligned slices, and gantry tilt?

The NIfTI format requires equal slice spacing, while CT scans are sometimes acquired with variable slice spacing (e.g. a lot of slices near the brain stem and wider spacing for more superior locations). In this case dcm2niix generates a warning and creates an interpolated volume with equal slice spacing that has ‘\_Eq’ appended to the file name

Unaligned slices are saved as separate volumes.

Gantry tilt is sometimes seen in CT scans. dcm2niix reports this skew in the NIfTI s-form. However, while the NIfTI format supports sheared volumes using the sform, most tools require rectangular volumes (e.g. ITK-based tools like slicer). Therefore, if gantry tilt is detected, dcm2niix also generates an interpolated volume with ‘\_Tilt’ appended to the filename.

> Does dcm2niix support CT reconstruction based on different DICOM tags (frame time, heart cycle phase, various vendor-specific tags)?

Most dcm2niix users work with MRI scans, and so it has been most thoroughly tested in that modality If you have CT examples with these features and dcm2niix does not work as expected, you can generate an issue on the [dcm2niix GitHub page](https://github.com/rordenlab/dcm2niix). A command line option allows you to specify if varying X-ray exposures should be stacked in a single volume or saved as separate volumes, as different users prefer different behavior. In general, different properties (for MRI: echo time, image type, trigger time, etc) are saved as separate files as described below.

> Can dcm2niix can load multiple series as a 4D volume?

In general, separate series are saved as separate files, and this is typically desirable (e.g. for fMRI analyses separate runs are typically saved as separate 4D volumes). An exception is DSC, where Siemens uses separate series for successive volumes. The next release (and current developmental release) of dcm2niix stack these DSC series as a single volume.  
Scans of the same series with the same properties are saved as a single 4D volume. Therefore, scans like DSC, fMRI, ASL, DWI, etc are saved as 4D volumes. Separate files are saved for different properties (e.g. type: magnitude, real, imaginary and phase; echo time; trigger delay time, etc.). These situations are described in the[dcm2niix file naming](https://github.com/rordenlab/dcm2niix/blob/master/FILENAMING.md) page.

---

<div class="post-metadata">

### Author: ![fedorov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fedorov/32/14_2.png) [@fedorov](https://discourse.slicer.org/u/fedorov)
#### Post date: [January 19, 2019, 5:12pm UTC](https://discourse.slicer.org/t/convert-single-files-into-one-nii/5407/10 "2019-01-19T17:12:13Z")

</div>

> [@pieper](#):
>
> @fedorov and a group of us researched the various options for dicom conversion a while back and collected our notes [on this project page](https://na-mic.github.io/ProjectWeek/PW27_2018_Boston/Projects/DICOMVolumeReconstruction/) . There’s also a [docker based test environment](https://github.com/QIICR/dcmheat) that runs the various tools (slicer, freesurfer, dcm2niix, etc) and compares the results.

To be more correct, setting up an environment that packages and tests those tools on a reference sample dataset was the desired goal. Dockers for some of the packages were completed, but the project was never continued after those initial steps.

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [January 24, 2019, 9:59pm UTC](https://discourse.slicer.org/t/convert-single-files-into-one-nii/5407/11 "2019-01-24T21:59:55Z")

</div>

@Chris_Rorden Thanks a lot for the detailed answer. You’ve done a great job with dcm2niix and nicely implemented all the essential image normalization techniques. We could use your library as a DICOM import plugin in Slicer for some 3D and 4D images, simplifying or replacing current scalar and multivolume plugins. We already have lots of infrastructure developments planned for the near future, but we may get back to this in about a year.

---

<div class="post-metadata">

### Author: ![fedorov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fedorov/32/14_2.png) [@fedorov](https://discourse.slicer.org/u/fedorov)
#### Post date: [January 24, 2019, 10:32pm UTC](https://discourse.slicer.org/t/convert-single-files-into-one-nii/5407/12 "2019-01-24T22:32:17Z")

</div>

> [@lassoan](#):
>
> We already have lots of infrastructure developments planned for the near future, but we may get back to this in about a year.

A quick but useful initial step could be to provide dcm2niix based DICOM plugin in an extension. I would even avoid superbuilding it, but maybe just pulling a packaged binary from the dcm2niix repo, or letting users install it locally from package, and point to the location of the binary in a setting.

---

<div class="post-metadata">

### Author: ![ihnorton](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ihnorton/32/9_2.png) [@ihnorton](https://discourse.slicer.org/u/ihnorton)
#### Post date: [January 28, 2019, 10:37pm UTC](https://discourse.slicer.org/t/convert-single-files-into-one-nii/5407/13 "2019-01-28T22:37:33Z")

</div>

> [@fedorov](#):
>
> A quick but useful initial step could be to provide dcm2niix based DICOM plugin in an extension.

Please see the following to add an extension (no DICOM browser plugin). We will use this as a dependency for a scripted module within SlicerDMRI, but made the dcm2niix build part a separate extension for reusability.

> <https://github.com/Slicer/ExtensionsIndex/pull/1610>
>
> \- \[x\] Extension has a reasonable name (not too general, not too narrow, suggests… what the extension is for)
> \- \[x\] Repository name is Slicer+ExtensionName
> \- \[x\] Extension description summarizes in 1-2 sentences what the extension is usable (should be understandable for non-experts)
> \- \[x\] Extension URL and revision (scmurl, scmrevision) is correct, consider using a branch name (master, release, ...) instead of a specific git has to avoid re-submitting pull request whenever the extension is updated
> \- \[x\] Extension icon URL is correct
> \- \[\] Screenshot URLs (screenshoturls) are correct, contains at least one
> \- \[\] Homepage URL points to valid webpage containing the following:
> - \[\] Extension name
> - \[\] Short description: 1-2 sentences, which summarizes what the extension is usable for
> - \[\] At least one nice, informative image, that illustrates what the extension can do. It may be a screenshot.
> - \[\] Description of contained modules: at one sentence for each module
> - \[\] Tutorial: step-by-step description of at least the most typical use case, include a few screenshots, provide download links to sample input data set

> [@fedorov](#):
>
> I would even avoid superbuilding it, but maybe just pulling a packaged binary from the dcm2niix repo, or letting users install it locally from package, and point to the location of the binary in a setting.

I set it up to build with superbuild, because (a) it is easy to build, and (b) we want it to have the same host runtime requirements as Slicer – for example, it would be confusing for users if the dcm2niix-provided binaries are built on a newer linux host and don’t run on the same platform as the rest of Slicer.
