6

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.

yujuezhao
  • 1,015
  • 3
  • 11
  • 21
  • 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.GetArrayFromImage` – filippo Sep 11 '19 at 07:03
  • @filippo 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. – yujuezhao Sep 11 '19 at 14:08
  • @filippo 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. – yujuezhao Sep 11 '19 at 14:13
  • 2
    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. – Dave Chen Sep 11 '19 at 14:30
  • @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? – yujuezhao Sep 18 '19 at 13:33
  • 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. – Dave Chen Sep 19 '19 at 15:51

0 Answers0