[ITK-users] Iterating through images in SimpleITK
Ali Ghayoor
alii.ghayoor at gmail.com
Wed Aug 26 12:47:01 EDT 2015
Hello ITK users,
Here is my conversation with 'Bradley Lowekamp' about the ways to iterate
through an image in SimpleITK, since ITK image iterators are not included in
SimpleITK interface.
We were thinking that the question and answers may be helpful to entire
community of ITK users.
Regards,
Ali
From: Bradley Lowekamp <blowekamp at mail.nih.gov>
Date: Tuesday, August 25, 2015 at 12:02 PM
To: Ali Ghayoor <ali-ghayoor at uiowa.edu>
Subject: Re: Iterating through images in SimpleITK
This is numpy boadcasting:
http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html
Very similar to how to use matlab efficiently.
>From this notebook [1], look at this:
def marschner_lobb(size=40, alpha=0.25, f_M=6.0):
img = sitk.PhysicalPointSource( sitk.sitkVectorFloat32, [size]*3,
[-1]*3, [2.0/size]*3)
imgx = sitk.VectorIndexSelectionCast(img, 0)
imgy = sitk.VectorIndexSelectionCast(img, 1)
imgz = sitk.VectorIndexSelectionCast(img, 2)
del img
r = sitk.Sqrt(imgx**2 + imgy**2)
del imgx, imgy
pr = sitk.Cos((2.0*math.pi*f_M)*sitk.Cos((math.pi/2.0)*r))
return (1.0 - sitk.Sin((math.pi/2.0)*imgz) +
alpha*(1.0+pr))/(2.0*(1.0+alpha))
This simple computation of a function, could be implemented as a function of
XYX. But it's implemented using operations on the whole images. Many
algorithms can be structured this way where you might first thing you need
the index.
I don't know what you are trying to do so I can't be more specific.
Brad
[1]
http://insightsoftwareconsortium.github.io/SimpleITK-Notebooks/20_Expand_Wit
h_Interpolators.html
On Aug 25, 2015, at 12:36 PM, Ghayoor, Ali <ali-ghayoor at uiowa.edu> wrote:
> Brad,
>
> Thank you for your response. I think iterating using the python interface
> is the easiest way if you do not need to check the index that you are
> operating on at each iteration. In my case, I need to know the index
> information at each iteration.
>
> Also, I am curious about your suggested fastest way, but I did not quite
> understand that. Could you please explain that with a little more details?
>
> Thank you,
> Ali
>
>
> On 8/24/15, 10:09 PM, "Bradley Lowekamp" <blowekamp at mail.nih.gov> wrote:
>
>> Hello Ali,
>>
>> As for iterating on SimpleITK image, there is the totally Python
>> interface to make the Image class iterable [1]. This is as slow as the 3
>> nested for loop, which is about as slow as iterating on a numpy array.
>> This is the easiest and most Pythonic way.
>>
>> The fastest way is to reconsider your algorithm to operating on the whole
>> image/array by using broadcasts and multiplying by 0s and 1s for
>> conditionals.
>>
>> HTH,
>> Brad
>>
>> p.s. This kind of question should be directed to the ITK mailing list so
>> that the entire community can benefit.
>>
>>
>> [1]
>> https://github.com/SimpleITK/SimpleITK/blob/6acfc05c01c946316cac9bd8803d62
>> a4e0cda5b9/Wrapping/Python.i#L289-L310
>>
>> On Aug 24, 2015, at 10:45 PM, Ghayoor, Ali <ali-ghayoor at uiowa.edu> wrote:
>>
>>> Hello Brad,
>>>
>>> I noticed that image iterators are not included in SimpleITK interface, so I
>>> needed your
>>> suggestion about the best way to iterate through an image while using
>>> SimpleITK in Ipython notebook. I'm not sure that using three nested
>>> 'for' loops is the most efficient way to do that and was thinking maybe
>>> there is a better method.
>>>
>>> Thank you for your help,
>>> Ali
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/insight-users/attachments/20150826/1785ea35/attachment.html>
More information about the Insight-users
mailing list