Is there a faster way to resize/resample 3d volumes other than scipy.ndimage.zoom?
Asked Answered
S

0

7

I know there is a function scipy.ndimage.zoom to resize\resample 3D volumes, but that is for numpy.array and it is notoriously slow. Therefore, I use ResampleImageFilter from SimpleITK instead. I think base on C++ simpleitk work much faster.

BUT there is a small flaw using simpleitk to resample. ResampleImageFilter works on SimpleITK.Image, but not numpy.array, so it is pretty inconvenient to do further operations. Is there any other way to resample 3D volumes?

EDIT
Why I am having this concerns is because I want to take advantage of the fast speed of SimpleITK resampling, and meanwhile I want to keep my code clean. For example, I need to do binary threshold to a volume and then resample the whole thing. So this is my solution,

binthresh = sitk.BinaryThresholdImageFilter()
... #setting up params for the binthresh
img = binarythresh.Execute(img)
resample = sitk.ResampleImageFilter()
... #setting up params for the resample
img = resample.Execute(img)
arr = sitk.GetArrayFromImage(img)
... # numpy operations on the arr

But in fact, using numpy to do the binary threshold is way simpler with logic indexing, which would make my code way more compact.
So in summary, I want to make the most of SimpleITK resampling, but some operations can be better done by numpy, and then my code gets a bit intertwined with both SimpleITK and numpy. And I dont think that is a good thing.

Subshrub answered 11/9, 2019 at 3:14 Comment(6)
I'm curious, always used ndimage.zoom without much issues, , albeit for small volumes. Are you comparing SimpleITK and ndimage with similar setups (i.e. excluding loading overhead, same interpolator, etc)? how much faster does it really get? By the way you can get a numpy array with sitk.GetArrayFromImagePerlite
@Perlite Well, I am dealing with a pretty large volumes, like (514,514,374) CT scans covering the whole lung. In this scenario, zoom with numpy.array is much inferior to the SimpleITK in terms of speed.Subshrub
@Perlite Yes, I can use sitk.ArrayfromImage to get numpy.array. But since SimpleITK provide a much faster resampling, I prefer to sticking to this. However, using SimpleITK would make my code less consice, I would re-edit my post to state my point.Subshrub
How about using SimpleITK's GetArrayViewFromImage. That way you won't be copying the volume back and forth between numpy and SimpleITK. You can then process the image in SimpleITK and see the results in numpy.Bagger
@DaveChen Hi, thanks for your note! But I try to tamper with the array_view from GetArrayViewFromImage but it turns out that the array_view is read only. Is there a way I can use numpy operation to the array of SimpleITK.Image without copying the volume back and forth as you initially suggeted?Subshrub
Yes, you can't really use GetArrayViewFromImage to easily go back and forth. It would be better if you could do all your processing in SimpleITK. BTW, if you have more questions about SimpleITK, you might try the ITK Discourse, discourse.itk.org.Bagger

© 2022 - 2024 — McMap. All rights reserved.