# vtkImageReslice to change the slicing axis is too slow

**URL:** https://discourse.vtk.org/t/vtkimagereslice-to-change-the-slicing-axis-is-too-slow/11313
**Category:** Web
**Created:** [April 26, 2023, 2:10pm UTC](https://discourse.vtk.org/t/vtkimagereslice-to-change-the-slicing-axis-is-too-slow/11313 "2023-04-26T14:10:39Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Tim\_De\_Bauw](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/76d3ee/32.png) [@Tim\_De\_Bauw](https://discourse.vtk.org/u/Tim_De_Bauw)
#### Post date: [April 26, 2023, 2:10pm UTC](https://discourse.vtk.org/t/vtkimagereslice-to-change-the-slicing-axis-is-too-slow/11313/1 "2023-04-26T14:10:39Z")

</div>

Hi,

I am trying to implement a feature where the slicing axis can be changed in real time. I have made a proof of concept using the by using the vtkImageReslice, where I use ResliceAxes to change the rotation of the different axis (Add the source to the vtkImageReslice and have it output a 3D volume, add the output of the vtkImageReslice to a vtkImageMapper, and slice the mapper). It works as expected, but the performance is really slow in realtime.

Are there ways to improve this? Do you have other solutions in mind?

---

<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: [April 26, 2023, 2:34pm UTC](https://discourse.vtk.org/t/vtkimagereslice-to-change-the-slicing-axis-is-too-slow/11313/2 "2023-04-26T14:34:09Z")

</div>

If you could show some of your code, even just the parts with vtkImageReslice and vtkImageMapper, that would help us provide you with an answer.

---

<div class="post-metadata">

### Author: ![Tim\_De\_Bauw](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/76d3ee/32.png) [@Tim\_De\_Bauw](https://discourse.vtk.org/u/Tim_De_Bauw)
#### Post date: [April 26, 2023, 2:39pm UTC](https://discourse.vtk.org/t/vtkimagereslice-to-change-the-slicing-axis-is-too-slow/11313/3 "2023-04-26T14:39:44Z")

</div>

How I initialize the actor, mapper, etc:

```auto
    this.mapper = vtkImageMapper.newInstance();
    this.actor = vtkImageSlice.newInstance();
    this.actor.setMapper(this.mapper);

    this.filter = vtkImageReslice.newInstance();
    this.filter.setOutputDimensionality(3);
    const axes = mat4.create();
    this.filter.setResliceAxes(axes);
    this.filter.setInputData(source);

    this.getMapper().setInputConnection(this.filter.getOutputPort(0));

```

How I update the reslicer:

```auto
    const I = mat4.create();
    mat4.rotate(I, I, Math.PI/4, [1, 0, 0]);
    this.filter.setResliceAxes(I);

```

How I retrieve the correct slice:

```auto
    this.mapper.getSlice();

```

---

<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: [April 26, 2023, 2:58pm UTC](https://discourse.vtk.org/t/vtkimagereslice-to-change-the-slicing-axis-is-too-slow/11313/4 "2023-04-26T14:58:02Z")

</div>

There are some fairly simple things that might speed up the execution.

Call `this.filter.update()` on vtkImageReslice. This should ensure that it updates the whole volume exactly once. If you don’t do this, then every time that the mapper shows a different slice, it will re-execute vtkImageReslice for that slice.

It will also help if you keep a separate vtkImageReslice for each direction, so that it isn’t necessary to re-execute the reslice when you want to switch from one direction to another. But if this isn’t possible, then make sure to call `update()` on vtkImageReslice after you change directions.

Or, you can use vtkImageResliceMapper instead of vtkImageReslice and vtkImageMapper.

---

<div class="post-metadata">

### Author: ![finetjul](https://discourse.vtk.org/user_avatar/discourse.vtk.org/finetjul/32/154_2.png) [@finetjul](https://discourse.vtk.org/u/finetjul)
#### Post date: [April 26, 2023, 2:58pm UTC](https://discourse.vtk.org/t/vtkimagereslice-to-change-the-slicing-axis-is-too-slow/11313/5 "2023-04-26T14:58:25Z")

</div>

How about applying a user matrix onto the image actor, i.e. `actor.setUserMatrix(axes)`

(and do not do `vtkImageReslice`)

---

<div class="post-metadata">

### Author: ![Tim\_De\_Bauw](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/76d3ee/32.png) [@Tim\_De\_Bauw](https://discourse.vtk.org/u/Tim_De_Bauw)
#### Post date: [April 26, 2023, 3:00pm UTC](https://discourse.vtk.org/t/vtkimagereslice-to-change-the-slicing-axis-is-too-slow/11313/6 "2023-04-26T15:00:33Z")

</div>

Its actually the recalculation of the reslice axis after rotation that seems to be slow for me, not the actual calculation of the slices

---

<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: [April 26, 2023, 3:04pm UTC](https://discourse.vtk.org/t/vtkimagereslice-to-change-the-slicing-axis-is-too-slow/11313/7 "2023-04-26T15:04:29Z")

</div>

Ah, yes, you mentioned specifically that you want to change the rotation in real time. Then definitely you don’t want to rotate the whole volume. You just want one rotated slice.

---

<div class="post-metadata">

### Author: ![Tim\_De\_Bauw](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/76d3ee/32.png) [@Tim\_De\_Bauw](https://discourse.vtk.org/u/Tim_De_Bauw)
#### Post date: [April 26, 2023, 3:04pm UTC](https://discourse.vtk.org/t/vtkimagereslice-to-change-the-slicing-axis-is-too-slow/11313/8 "2023-04-26T15:04:47Z")

</div>

> Blockquote How about applying a user matrix onto the image actor, i.e. `actor.setUserMatrix(axes)`

I tried this approach, but for me it does not seem to change the cutting plane of the mapper, it seems to tilt the slice compared to the camera

---

<div class="post-metadata">

### Author: ![finetjul](https://discourse.vtk.org/user_avatar/discourse.vtk.org/finetjul/32/154_2.png) [@finetjul](https://discourse.vtk.org/u/finetjul)
#### Post date: [April 26, 2023, 3:09pm UTC](https://discourse.vtk.org/t/vtkimagereslice-to-change-the-slicing-axis-is-too-slow/11313/9 "2023-04-26T15:09:16Z")

</div>

Then I agree with David, 2 options:

> [@dgobbi](#):
>
> Then definitely you don’t want to rotate the whole volume. You just want one rotated slice.

→ `this.filter.setOutputDimensionality(2)`

Or

> [@dgobbi](#):
>
> Or, you can use vtkImageResliceMapper instead of vtkImageReslice and vtkImageMapper.

---

<div class="post-metadata">

### Author: ![Tim\_De\_Bauw](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/76d3ee/32.png) [@Tim\_De\_Bauw](https://discourse.vtk.org/u/Tim_De_Bauw)
#### Post date: [April 26, 2023, 3:12pm UTC](https://discourse.vtk.org/t/vtkimagereslice-to-change-the-slicing-axis-is-too-slow/11313/10 "2023-04-26T15:12:40Z")

</div>

And would you then have a vtkImageReslice instance for the three orthogonal axes? Scrolling on a plane would then involve updating the resliceAxis for the vtImageReslice instance of the corresponding plane?

---

<div class="post-metadata">

### Author: ![finetjul](https://discourse.vtk.org/user_avatar/discourse.vtk.org/finetjul/32/154_2.png) [@finetjul](https://discourse.vtk.org/u/finetjul)
#### Post date: [April 26, 2023, 4:01pm UTC](https://discourse.vtk.org/t/vtkimagereslice-to-change-the-slicing-axis-is-too-slow/11313/11 "2023-04-26T16:01:48Z")

</div>

That’s right.

---

<div class="post-metadata">

### Author: ![Tim\_De\_Bauw](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/76d3ee/32.png) [@Tim\_De\_Bauw](https://discourse.vtk.org/u/Tim_De_Bauw)
#### Post date: [April 26, 2023, 4:13pm UTC](https://discourse.vtk.org/t/vtkimagereslice-to-change-the-slicing-axis-is-too-slow/11313/12 "2023-04-26T16:13:40Z")

</div>

Okay, thanks a lot for the info! I will try out using the vtkImageResliceMapper

---

<div class="post-metadata">

### Author: ![Rajan\_Mistri](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rajan_mistri/32/8707_2.png) [@Rajan\_Mistri](https://discourse.vtk.org/u/Rajan_Mistri)
#### Post date: [April 8, 2024, 12:13pm UTC](https://discourse.vtk.org/t/vtkimagereslice-to-change-the-slicing-axis-is-too-slow/11313/13 "2024-04-08T12:13:07Z")

</div>

Hi, I’ve encountered a similar issue with setting the reslice axes being too slow. Have you discovered any solutions for this problem? It seems to work perfectly fine in the resliceCursorWidget without any noticeable slowdown.
