# Stack several images along channel dimension to create multichannel images (e.g. 4D volume)

**URL:** <https://discourse.slicer.org/t/stack-several-images-along-channel-dimension-to-create-multichannel-images-e-g-4d-volume/25813>\
**Category:** Support\
**Created:** [October 20, 2022, 3:19pm UTC](https://discourse.slicer.org/t/stack-several-images-along-channel-dimension-to-create-multichannel-images-e-g-4d-volume/25813 "2022-10-20T15:19:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mangotee](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@mangotee](https://discourse.slicer.org/u/mangotee)\
**Post date:** [October 20, 2022, 3:19pm UTC](https://discourse.slicer.org/t/stack-several-images-along-channel-dimension-to-create-multichannel-images-e-g-4d-volume/25813/1 "2022-10-20T15:19:57Z")

</div>

Hi,  
I sometimes need to stack several 2D/3D images/volumes along the channel dimension, create a multichannel image and save it as a 4D Nifti/Nrrd. For simplicity’s sake, you can assume that I already have co-registered the images, and that they are all resampled into the first image’s pixel/voxel grid as reference. Fwiw, I know how to convert the images to numpy arrays and stack them along a channel dimension, but I don’t know how to create a new multichannel volume from that and make sure that it has an identical geometry as in the reference image header.  
Thanks for any tips in advance!  
Best,  
Ahmad

---

<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:** [October 20, 2022, 4:02pm UTC](https://discourse.slicer.org/t/stack-several-images-along-channel-dimension-to-create-multichannel-images-e-g-4d-volume/25813/2 "2022-10-20T16:02:03Z")

</div>

> [@mangotee](#):
>
> w to convert the images to numpy arrays and stack them along a channel dimension, but I don’t know how to create a new multichannel volume from that and make sure that it has an identical geometry as

You can use sequences for this. If all the volumes in the sequence have the same geometry then they will be saved as a single 4D nrrd file. You can pick one of the channels and display it in the scene by adding a sequence browser node for each. See examples for getting/setting data in sequences in the [script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#sequences).

Alternatively, you can store all the channels in a single vector volume. For example, you can create a 6-channel volume with random content like this:

```python
import numpy as np
volumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLVectorVolumeNode')
voxels = np.random.rand(3,4,5,6)
slicer.util.updateVolumeFromArray(volumeNode, voxels)

```

The disadvantage is that you cannot easily browse the channels but for example you can extract any of them using the “Vector to scalar volume” module (or in Python, using `slicer.util.arrayFromVolume()`).

---

<div class="post-metadata">

**Author:** ![mangotee](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@mangotee](https://discourse.slicer.org/u/mangotee)\
**Post date:** [October 24, 2022, 11:04am UTC](https://discourse.slicer.org/t/stack-several-images-along-channel-dimension-to-create-multichannel-images-e-g-4d-volume/25813/3 "2022-10-24T11:04:29Z")

</div>

Hi Andreas,

thanks for the tip. If I use your numpy-based code example…:

> [@lassoan](#):
>
> ```auto
> import numpy as np
> volumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLVectorVolumeNode')
> voxels = np.random.rand(3,4,5,6)
> slicer.util.updateVolumeFromArray(volumeNode, voxels)
> 
> ```

…, except that `voxels` is a stacked 4D array volume `[0,1,2,...,N]`, how would I assign the same reference geometry as e.g. `volume0` to the newly-created `volumeNode`, such that when I save that node as Nifti/Nrrd, it would overlap perfectly with `volume0`?

---

<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:** [October 24, 2022, 11:42am UTC](https://discourse.slicer.org/t/stack-several-images-along-channel-dimension-to-create-multichannel-images-e-g-4d-volume/25813/4 "2022-10-24T11:42:42Z")

</div>

You also need set the image geometry, by copying it from the original image:

> <https://github.com/Slicer/Slicer/blob/415aee60cdb6b23dad60ddf874216139c499453d/Modules/Scripted/VectorToScalarVolume/VectorToScalarVolume.py#L411-L413>
