vtkImageReslice is very slow

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

        reslice = vtk.vtkImageReslice()
        reslice.SetOutputSpacing(spacing[0], spacing[0], spacing[0])

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

t1 = time.time()
t2 = time.time()

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

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?

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.

1 Like

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

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?

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.

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.

@dgobbi Thank you for your kindly reply.

@lassoan Thank you for your kindly reply.