简体   繁体   English

我可以在SimpleITK.Image上使用numpy操作而不使用GetArrayFromImage进行转换吗

[英]Can I use numpy operation on SimpleITK.Image without converting using GetArrayFromImage

I can do so in SimpleITK , 我可以在SimpleITK做到这SimpleITK

img = sitk.ReadImage(file_path)
array = sitk.GetArrayFromImage(img)
array[array > 10] = 10
array[array < 0] = 0

Or I can do it like, 或者我可以这样做,

img = sitk.ReadImage(file_path)
binthresh = sitk.BinaryThresholdFilter()
... # set up params for binthresh
img = binthresh.Execute(img)

But the thing is, I want to take advantage of the fast speed of SimpleITK.ResampleImageFilter`, therefore I have to use it like, 但问题是,我想利用SimpleITK.ResampleImageFilter`的快速速度,因此我必须像这样使用它,

img = sitk.ReadImage(file_path)
binthresh = sitk.BinaryThresholdFilter()
... # set up params for binthresh
img = binthresh.Execute(img)
resample = sitk.ResampleImageFilter()
... # set up params for resample
img = resample.Execute(img)

In fact, I hope there is a way like this, 实际上,我希望有这样的方法,

img = sitk.ReadImage(file_path)
array_view = sitk.GetArrayViewFromImage(img)
array_view[array_view > 10] = 10
array_view[array_view < 0] = 0
resample = sitk.ResampleImageFilter()
... # set up params for resample
img = resample.Execute(img)

The code block above seems much compact, but the array_view from GetArrayViewFromImage is read-only. 上面的代码块看起来很紧凑,但是GetArrayViewFromImagearray_view是只读的。 Therefore, is there a way to do the equivalent? 因此,有没有办法做到这一点?

With SimpleITK's current implementations of GetArrayViewFromImage , it is safest to prevent alias (shallow copies) so that potential invalid memory access is avoided. 使用SimpleITK的GetArrayViewFromImage的当前实现,最安全的方法是防止别名(浅副本),从而避免潜在的无效内存访问。 Therefore, no writable access methods are implemented. 因此,没有实现可写访问方法。 However, the current development is working on developing a safe reference counted access method that will enable writing. 但是,当前的开发正在开发一种安全的引用计数访问方法,该方法将使编写成为可能。

However, there is an equivalent image filter in SimpleITK to do the same operation the ClampImageFilter . 然而,有在SimpleITK等效图像滤波器做同样的操作ClampImageFilter It is faster as well: 它也更快:


In [1]: import numpy as np                                                                                                                                      

In [2]: import SimpleITK as sitk                                                                                                                                

In [3]: a = np.random.rand(512,512,128)*100-50                                                                                                                  

In [4]: img = sitk.GetImageFromArray(a)                                                                                                                         

In [5]: %timeit -n 10 sitk.Clamp(img, lowerBound=0, upperBound=10)                                                                                              
41.5 ms ± 1.11 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [6]: def f(a, copy=False): 
   ...:     out = a 
   ...:     if copy: 
   ...:         out = np.copy(a) 
   ...:     out[out>10] = 10 
   ...:     out[out<0] = 0 
   ...:     return out 
   ...:                                                                                                                                                         

In [7]: %timeit -n 10 f(a, copy=False)                                                                                                                          
49.7 ms ± 11.7 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [8]: %timeit -n 10 f(a, copy=True)                                                                                                                           
84.8 ms ± 228 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM