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

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!

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 with the NearestNeighborInterpolateImageFunction, see the ITK resampling example.

Hi @zivy thank you for your email,

It would be great if the NearestNeighborInterpolateImageFunction 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! :slight_smile:

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.

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

4 Likes

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
1 Like

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.

1 Like