# Splitting a 3D image into a 4D image then saving (C++)

**URL:** https://discourse.itk.org/t/splitting-a-3d-image-into-a-4d-image-then-saving-c/4316
**Category:** Beginner Questions
**Created:** [August 6, 2021, 5:59am UTC](https://discourse.itk.org/t/splitting-a-3d-image-into-a-4d-image-then-saving-c/4316 "2021-08-06T05:59:06Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![kevo](https://discourse.itk.org/letter_avatar_proxy/v4/letter/k/54ee81/32.png) [@kevo](https://discourse.itk.org/u/kevo)
#### Post date: [August 6, 2021, 5:59am UTC](https://discourse.itk.org/t/splitting-a-3d-image-into-a-4d-image-then-saving-c/4316/1 "2021-08-06T05:59:06Z")

</div>

Dear ITK community,

I would like to take a 3D segmentation (nifti) and then create a new 4D nifti where the 4th dimension has has split up the label image where each element is the 3D image with just one label in each of them - is there an obvious way to do this?

The end goal would be to perform transformations on each of the labels and stitch it back together.

any help (or pointing to examples) would be great!

Thank you!

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [August 6, 2021, 12:42pm UTC](https://discourse.itk.org/t/splitting-a-3d-image-into-a-4d-image-then-saving-c/4316/2 "2021-08-06T12:42:57Z")

</div>

Hello @kevo,

Is there a specific reason you want to split the labels? If they all undergo the same transformation, which I suspect they do, just use the [ResampleImageFilter](https://itk.org/Doxygen/html/classitk_1_1ResampleImageFilter.html) with the [NearestNeighborInterpolateImageFunction](https://itk.org/Doxygen/html/classitk_1_1NearestNeighborInterpolateImageFunction.html), see the [ITK resampling example](https://itk.org/ITKExamples/src/Filtering/ImageGrid/ResampleAnImage/Documentation.html).

---

<div class="post-metadata">

### Author: ![kevo](https://discourse.itk.org/letter_avatar_proxy/v4/letter/k/54ee81/32.png) [@kevo](https://discourse.itk.org/u/kevo)
#### Post date: [August 9, 2021, 12:08am UTC](https://discourse.itk.org/t/splitting-a-3d-image-into-a-4d-image-then-saving-c/4316/3 "2021-08-09T00:08:29Z")

</div>

Hi @zivy thank you for your email,

It would be great if the [NearestNeighborInterpolateImageFunction](https://itk.org/Doxygen/html/classitk_1_1NearestNeighborInterpolateImageFunction.html) was the option, but unfortunately Nearest neighbour Interpolants are quite lossy. In my research - what has worked better was a splitting of the labels and performing specialised transforms for each. I’ve tried ITK’s nearest neighbour implementation in python and its no better than other algorithms unfortunately.

So this is why I would like to create a 4D image, so any help would be great! 🙂

FYI: Performing my specialised transforms in python work well but It takes close to 30s and I would prefer to speed this up directly in C++.

Cheers,  
Kevo.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)
#### Post date: [August 9, 2021, 2:13pm UTC](https://discourse.itk.org/t/splitting-a-3d-image-into-a-4d-image-then-saving-c/4316/4 "2021-08-09T14:13:36Z")

</div>

For a few years we switched to representing segmentations using such 4D volumes but we reverted to using 3D volumes (except for storing overlapping segments) because of increased memory requirement and incompatibility with processing algorithms. So, I would not recommend to switch to this representation just to improve segmentation quality. Instead, you can keep a 3D labelmap and increase its resolution as needed.

If you just temporarily want to split the segmentation to separate volumes then it is very easy to do, for example using threshold filter.

Nearest neighbor interpolation is not optimal, especially when the target resolution is higher. You can use these [readily available approaches for labelmap interpolation](https://discourse.itk.org/t/resampling-along-the-z-axis-of-binary-segmentation-stack/4286/7) or you can port your slow Python code to C++ and add Python wrapping for it (for example, make an ITK filter from it and add it as an ITK remote module).

---

<div class="post-metadata">

### Author: ![kevo](https://discourse.itk.org/letter_avatar_proxy/v4/letter/k/54ee81/32.png) [@kevo](https://discourse.itk.org/u/kevo)
#### Post date: [August 11, 2021, 5:01am UTC](https://discourse.itk.org/t/splitting-a-3d-image-into-a-4d-image-then-saving-c/4316/5 "2021-08-11T05:01:00Z")

</div>

Awesome thank you @lassoan that was very helpful! I don’t have any experience with ITK remote module, but I will look into it!

Out of interest is it easy to convert a segmented image into a 3D label map to python, i.e.:

- read segmentation as label map
- resample label map
- convert back to typical segmentation

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)
#### Post date: [August 11, 2021, 5:20pm UTC](https://discourse.itk.org/t/splitting-a-3d-image-into-a-4d-image-then-saving-c/4316/6 "2021-08-11T17:20:10Z")

</div>

> [@kevo](#):
>
> is it easy to convert a segmented image into a 3D label map to python

For me, a segmentation, label map, Python numpy array are almost the same. The only difference is how you store extra metadata - at the minimum, image geometry (origin, spacing, and axis directions), but preferably also segment names, standard terminology, and other optional attributes.

ITK can already preserve image geometry using xarray. I don’t think there is a common standard for storing additional segmentation metadata in xarrays, but maybe it will come out as a side product of the [OME-Zarr - next-generation biomedical file format effort](https://ngff.openmicroscopy.org/latest/).
