# Representing 4D image data with VTK

**URL:** https://discourse.vtk.org/t/representing-4d-image-data-with-vtk/2401
**Category:** Support
**Created:** [January 10, 2020, 3:14pm UTC](https://discourse.vtk.org/t/representing-4d-image-data-with-vtk/2401 "2020-01-10T15:14:28Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Seun](https://discourse.vtk.org/user_avatar/discourse.vtk.org/seun/32/767_2.png) [@Seun](https://discourse.vtk.org/u/Seun)
#### Post date: [January 10, 2020, 3:14pm UTC](https://discourse.vtk.org/t/representing-4d-image-data-with-vtk/2401/1 "2020-01-10T15:14:28Z")

</div>

Hi everyone,  
I would like to know if it’s possible to represent 4D data as a vtkImageData. As far as I understand, vtkImageData can only be used to encode 3D (i.e: voxel information) but I was wondering if there’s a way to handle 4D in VTK (is there a class already capable of achieving this?).

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [January 10, 2020, 3:34pm UTC](https://discourse.vtk.org/t/representing-4d-image-data-with-vtk/2401/2 "2020-01-10T15:34:43Z")

</div>

vtkImageData is already a 4D array (3 spatial + 1 component). You can extract components for processing or specify active component index in certain mappers.

If you need to manage vector or tensor images then you can either use VTK’s built-in time sequence support (see [vtkTemporalInterpolator](https://vtk.org/doc/nightly/html/classvtkTemporalInterpolator.html) and related classes) or manage arrays of VTK objects on your own. I find VTK’s built-in time support somewhat complicated and limited but I think it is used in ParaView and it works nicely there. To achieve fastest performance, you need to manage array of objects on your own and have actors corresponding to all timestamps in your renderer and animate by showing the current time point and hiding all the others.

---

<div class="post-metadata">

### Author: ![Seun](https://discourse.vtk.org/user_avatar/discourse.vtk.org/seun/32/767_2.png) [@Seun](https://discourse.vtk.org/u/Seun)
#### Post date: [January 10, 2020, 3:44pm UTC](https://discourse.vtk.org/t/representing-4d-image-data-with-vtk/2401/3 "2020-01-10T15:44:32Z")

</div>

Hi Andras,  
thanks for the expeditious response. I’ll have a closer look at the built in time sequence support as well as vtkImageData itself (there’s obviously something I missed). Thanks a lot.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [January 10, 2020, 4:31pm UTC](https://discourse.vtk.org/t/representing-4d-image-data-with-vtk/2401/4 "2020-01-10T16:31:34Z")

</div>

I agree with Andras’ assesment: it’s easiest to just store the time points in different components of the vtkImageData. In essense, each voxel becomes a vector.

In fact, at work we use this technique so often that we simply call it “multiplexing” and we call a 4D vtkImageData a “multiplex”. The vtkNIFTIImageReader and my vtkDICOMReader have a “TimeAsVectorOn()” method that automatically multiplexes 4D images by turning each voxel into a time vector.

In the simplest case, you can take a time sequence of input images and combine them with vtkImageAppendComponents. A single image can later be extracted with vtkImageExtractComponents. If the individual images have multiple channels (e.g. RGB), then the required number of components can be extracted.

Also, some VTK filters and mappers have a method called SetActiveComponent() that allows you to choose which component is filtered/mapped, e.g. vtkImageMapToColors::SetActiveComponent(). This means that you do not have to use vtkImageExtractComponents before using these filters, since they can do the component extraction for you.

For vtkImageReslice, the vtkImageInterpolator class has methods called SetComponentOffset() and SetComponentCount() that can be used to choose which components to use when interpolating. So by using vtkImageReslice::SetInterpolator(), it’s possible to make vtkImageReslice use certain components and ignore the other components.

---

<div class="post-metadata">

### Author: ![kit](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/k/9fc29f/32.png) [@kit](https://discourse.vtk.org/u/kit)
#### Post date: [January 12, 2020, 5:45pm UTC](https://discourse.vtk.org/t/representing-4d-image-data-with-vtk/2401/5 "2020-01-12T17:45:53Z")

</div>

Hi,

I have a project

> **[batearedcollie/seisTK](https://github.com/batearedcollie/seisTK)**
>
> Contribute to batearedcollie/seisTK development by creating an account on GitHub.

Which includes an object vtkHyperCube. This is essentially a vtkImageData which handles higher dimensionsal grids. Essentially it just wraps the higher dimensions into the 3D grid referenced by vtkImageData. You should be able to do anything with vtkHyperCube you can do with vtkImageData, but it has not been used for things other than number cruching yet.

If nothing else it might provide some help in creating a custom data type more suited to your needs.

---

<div class="post-metadata">

### Author: ![Seun](https://discourse.vtk.org/user_avatar/discourse.vtk.org/seun/32/767_2.png) [@Seun](https://discourse.vtk.org/u/Seun)
#### Post date: [January 13, 2020, 2:25pm UTC](https://discourse.vtk.org/t/representing-4d-image-data-with-vtk/2401/6 "2020-01-13T14:25:27Z")

</div>

Thanks Kit,  
I’ll take a look, hopefully it might give me an idea on how to solve my issue.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [January 13, 2020, 7:04pm UTC](https://discourse.vtk.org/t/representing-4d-image-data-with-vtk/2401/7 "2020-01-13T19:04:53Z")

</div>

A similar 4D data set implementation is available in 3D Slicer’s [Sequences extension](https://www.slicer.org/wiki/Documentation/Nightly/Extensions/Sequences). It supports not just images (up to 5D) but sequences of any other VTK data types. The basic container class is [vtkMRMLSequenceNode](https://github.com/SlicerRt/Sequences/blob/master/Sequences/MRML/vtkMRMLSequenceNode.h).

The implementation is based on [MRML](https://github.com/Slicer/Slicer/tree/master/Libs/MRML), which is a VTK-based library for representing, visualizing, processing, and interacting with medical data scenes. Data representation only depends on VTK, there are optional widgets use Qt and some IO classes use ITK.

You can use it as is (easiest to do it in 3D Slicer itself) or to get inspiration or copy-paste code parts that you find useful to your project.
