# vtkImageReslice is very slow

**URL:** https://discourse.vtk.org/t/vtkimagereslice-is-very-slow/4570
**Category:** Support
**Created:** [November 4, 2020, 10:55pm UTC](https://discourse.vtk.org/t/vtkimagereslice-is-very-slow/4570 "2020-11-04T22:55:27Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [November 4, 2020, 10:55pm UTC](https://discourse.vtk.org/t/vtkimagereslice-is-very-slow/4570/1 "2020-11-04T22:55:27Z")

</div>

I use vtkImageReslice to get a resliced image, and the code (python) is:

```auto
        reslice = vtk.vtkImageReslice()
        reslice.SetInputData(image)
        reslice.SetOutputDimensionality(2)
        reslice.SetAutoCropOutput(True)
        reslice.SetInterpolator(interpolateMethod)
        reslice.SetSlabNumberOfSlices(1)
        reslice.SetOutputSpacing(spacing[0], spacing[0], spacing[0])

```

sometimes, I would change the thickness of the resliced image by:

```auto
t1 = time.time()
reslice.SetSlabNumberOfSlices(n)
reslice.Update()
t2 = time.time()

```

I print the calculation time (t2 - t1) of vtkImageReslice for different thickness:

```auto
if n = 1, the cost time is about 0.001s
if n = 40, the cost time is about 1s

```

I wonder why the calculation time would change when the number of slices change? Is there any wrong with my code?

---

<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: [November 5, 2020, 2:59am UTC](https://discourse.vtk.org/t/vtkimagereslice-is-very-slow/4570/2 "2020-11-05T02:59:35Z")

</div>

With `SetSlabNumberOfSlices(n)`, vtkImageReslice must interpolate “`n`” slices and then combine them to create each thick output slice. So it is guaranteed to slow down by at least a factor of “`n`”, but it generally slows down even more than that.

However, since you are seeing a the time increase by a factor of 1000, I suspect that part of the slowdown is due to cache issues. So with the benchmark, try at least to run multiple times and take the average time. Also, you can try increasing `n` a little at a time to see if there is a certain `n` where things suddenly slow down due to running out of cache.

Also try the `vtkImageSlab` filter, which is much faster if you just want to generate thick slices in the X, Y, or Z directions.

---

<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: [November 5, 2020, 5:48am UTC](https://discourse.vtk.org/t/vtkimagereslice-is-very-slow/4570/3 "2020-11-05T05:48:07Z")

</div>

1sec for resampling and averaging 40 slices sounds about right for average-sized medical images on average CPU.

On the other hand, resampling the same image on the same CPU in 0.001sec is most likely a measurement error (for example, it is possible that it is so fast because you have already computed the output before and you just retrieve it).

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [November 5, 2020, 11:27am UTC](https://discourse.vtk.org/t/vtkimagereslice-is-very-slow/4570/4 "2020-11-05T11:27:03Z")

</div>

Sorry, but I need an arbitrary angle of projection, thus I have to use vtkImageReslice.

In addition, I wonder is it possible to accelerate vtk by OMP?

---

<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: [November 5, 2020, 11:43am UTC](https://discourse.vtk.org/t/vtkimagereslice-is-very-slow/4570/5 "2020-11-05T11:43:25Z")

</div>

By default, vtkImageReslice is accelerated via SMP. You can verify this by checking your CPU usage while it is running, all CPU cores will used.

---

<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: [November 5, 2020, 2:11pm UTC](https://discourse.vtk.org/t/vtkimagereslice-is-very-slow/4570/6 "2020-11-05T14:11:14Z")

</div>

If you want magnitudes faster thick slab reslicing then you can reslice on the GPU, using volume rendering. However, it brings in a lot of complications, for example you need to get the whole volume on the GPU, you get the resulting image in the frame buffer (not in the pipeline), there might be limitations in scalar types, etc.

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [November 5, 2020, 2:28pm UTC](https://discourse.vtk.org/t/vtkimagereslice-is-very-slow/4570/7 "2020-11-05T14:28:49Z")

</div>

@dgobbi Thank you for your kindly reply.

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [November 5, 2020, 2:29pm UTC](https://discourse.vtk.org/t/vtkimagereslice-is-very-slow/4570/8 "2020-11-05T14:29:04Z")

</div>

@lassoan Thank you for your kindly reply.
