# ResampleImageFilter 4D Images

**URL:** <https://discourse.itk.org/t/resampleimagefilter-4d-images/2172>\
**Category:** Algorithms\
**Tags:** simpleitk, python\
**Created:** [August 19, 2019, 4:16pm UTC](https://discourse.itk.org/t/resampleimagefilter-4d-images/2172 "2019-08-19T16:16:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SachidanandAlle](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/4491bb/32.png) [@SachidanandAlle](https://discourse.itk.org/u/SachidanandAlle)\
**Post date:** [August 19, 2019, 4:16pm UTC](https://discourse.itk.org/t/resampleimagefilter-4d-images/2172/1 "2019-08-19T16:16:56Z")

</div>

Currently I am getting error when I try to use ResampleImageFilter over 4D image

In the following thread, it is mentioned that 4D support is only limited to ExtractImageFilter

> [@SimpleITK: Channel storage order?](https://discourse.itk.org/t/simpleitk-channel-storage-order/1027/2):
>
> We have some information on multi-channel images in SimpleITK here: [https://simpleitk.readthedocs.io/en/master/Documentation/docs/source/fundamentalConcepts.html#channels](https://simpleitk.readthedocs.io/en/master/Documentation/docs/source/fundamentalConcepts.html#channels) You are correct that SimpleITK stores RGB or vector images with the channels interleaved. If you have an image of [x,y,z,c], where the last channel is the color, this would be considered a 4D Image in SimpleITK. We have limited support for 4D Images, which includes reading, writing, and using the ExtractImageFilter( 4D-\>3D), …

File “/usr/local/lib/python3.5/dist-packages/SimpleITK/SimpleITK.py”, line 58541, in Execute  
return \_SimpleITK.ResampleImageFilter\_Execute(self, \*args)  
RuntimeError: Exception thrown in SimpleITK ResampleImageFilter\_Execute: /tmp/SimpleITK/Code/Common/include/sitkDualMemberFunctionFactory.hxx:218:  
**sitk::ERROR: Image dimension 4 is not supported**

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [August 19, 2019, 7:58pm UTC](https://discourse.itk.org/t/resampleimagefilter-4d-images/2172/2 "2019-08-19T19:58:19Z")

</div>

Hello SachidananAlle,

That is correct, SimpleITK’s `ResampleImageFilter` is not compiled with 4D image support. It does support resampling for 3D multi-channel images in the 3 spatial dimensions.

If you have a questions or need a suggestion of how to do an operation please clearly state your problem and questions.

---

<div class="post-metadata">

**Author:** ![SachidanandAlle](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/4491bb/32.png) [@SachidanandAlle](https://discourse.itk.org/u/SachidanandAlle)\
**Post date:** [August 20, 2019, 11:41am UTC](https://discourse.itk.org/t/resampleimagefilter-4d-images/2172/3 "2019-08-20T11:41:53Z")

</div>

For now, I could use `ExtractImageFilter` to get each slice from 4D image and apply the `ResampleImageFilter` and joined it later using `JoinSeriesImageFilter`

(But it will be great to see SITK supports that directly inside `ResampleImageFilter`)

## Something like as follows:

```
def resample(self, new_spacing, new_size, image):
    resampler = sitk.ResampleImageFilter()
    resampler.SetInterpolator(sitk.sitkLinear)
    resampler.SetOutputSpacing(new_spacing)
    resampler.SetSize(new_size)

    resampler.SetOutputOrigin(image.GetOrigin())
    resampler.SetOutputDirection(image.GetDirection())
    img = resampler.Execute(image)
    return img

def get3dslice(image, slice=0):
    size = list(image.GetSize())
    if len(size) == 4:
        size[3] = 0
        index = [0, 0, 0, slice]

        extractor = sitk.ExtractImageFilter()
        extractor.SetSize(size)
        extractor.SetIndex(index)
        image = extractor.Execute(image)
    return image

# Resample 4D (SITK Doesn't support directly; so iterate through slice and get it done)
new_spacing = [sx,sx,sz]
new_size = [x,y,z]

images = []
size = list(image.GetSize())
for s in range(size[3]):
    img = get3dslice(image, s)
    img = resample(new_spacing, new_size, img)
    images.append(img)

join = sitk.JoinSeriesImageFilter()
image = join.Execute(images)
```

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [August 20, 2019, 1:51pm UTC](https://discourse.itk.org/t/resampleimagefilter-4d-images/2172/4 "2019-08-20T13:51:40Z")

</div>

Looks like a good solution to me. I have done similar things when working with 4D XYZC images. This approach is really asking for a reusable solution something like a python function decorator to operate slice\_by\_slice. Here is what I came up with:

```auto

def slicing_decorator(func):
    """
    A function decorator which extracts image slices of N-1 dimensions and calls func on each slice. The resulting
     images are then concatenated together with JoinSeries.
     
    :param func: A function which take a SimpleITK Image as it's first argument
    :return: The result of running func on each slice of image.
    """
    @wraps(func)
    def slice_by_slice(image, *args, **kwargs):

        size = list(image.GetSize())

        number_of_slices = size[-1]
        extract_size = size
        extract_index = [0]*image.GetDimension()

        img_list = []

        extract_size[-1] = 0
        extractor = sitk.ExtractImageFilter()
        extractor.SetSize(extract_size)

        for slice_idx in range(0, number_of_slices):

            extract_index[-1] = slice_idx
            extractor.SetIndex(extract_index)

            img_list.append(func(extractor.Execute(image), *args, **kwargs))

        return sitk.JoinSeries(img_list, image.GetOrigin()[-1], image.GetSpacing()[-1])

    return slice_by_slice

@slicing_decorator
def my_algo(image):
    print("Executing image of size: {0}".format(image.GetSize()))
    return sitk.SmoothingRecursiveGaussian(image)

```
