[ITK-users] [ITK] ITK PyBuffer get physical point coordinates

Francois Budin francois.budin at kitware.com
Tue May 2 11:48:51 EDT 2017


Hello Fabio,

The main difference between itk.GetArrayFromImage() and
itk.PyBuffer[ImageType].GetArrayFromImage() is that itk.GetAraryFromImage()
is a wrapping of itk.PyBuffer[ImageType].GetArrayFromImage() in Python that
automatically finds the type of the input image (and is a shorted command
line). The reason why this worked but not
itk.PyBuffer[ImageType.GetArrayFromImage() is because in the wrapping, if a
filter is given instead of an image, the output of the filter will be used.

Which means that what really happen is that you gave a filter to a function
(itk.PyBuffer[ImageType].GetArrayFromImage()) expecting an image.
If you want to work with the long command, you have to make sure that the
input is an image:

image=source.GetOutput()
itk.PyBuffer[ImageType].GetArrayFromImage(image)

And then it should work.
Hope this helps,

Francois

On Tue, May 2, 2017 at 11:20 AM, D'Isidoro Fabio <fisidoro at ethz.ch> wrote:

> Hi Francois,
>
>
>
> It works with your commands, the difference being the use of:
>
>
>
> itk.GetArrayFromImage(source)
>
>
>
> in your version, instead of:
>
>
>
> itk.PyBuffer[ImageType].GetArrayFromImage(source)
>
>
>
> in my version.
>
>
>
> May you explain me the difference, and why using PyBuffer does not work
> for this case?
>
>
>
> Also, when using VectorImage I get the error “No module named
> 'ITKQuadEdgeMeshPython'”. This is the only module that failed in my ITK
> Python build. Is this a known bug or it was just for my build?
>
>
>
> Thank you!
>
>
>
> Fabio.
>
> *From:* Francois Budin [mailto:francois.budin at kitware.com]
> *Sent:* Freitag, 28. April 2017 23:12
> *To:* D'Isidoro Fabio <fisidoro at ethz.ch>
> *Cc:* Lowekamp, Bradley (NIH/NLM/LHC) [C] <blowekamp at mail.nih.gov>;
> insight-users at itk.org
> *Subject:* Re: [ITK-users] [ITK] ITK PyBuffer get physical point
> coordinates
>
>
>
> Hello Fabio,
>
> I was able to use this filter in Python. Here is the commands I typed:
> In [14]: ImageType=itk.Image[itk.Vector[itk.F,2],2]
>
> In [15]: source=itk.PhysicalPointImageSource[ImageType].New()
>
> In [16]: source.SetSize([5,5])
>
> In [17]: source.SetOrigin([0,0])
>
> In [18]: source.SetSpacing([.5,.5])
>
> In [19]: source.Update()
>
> In [20]: itk.GetArrayFromImage(source)
> Out[20]:
> itkndarray([[[ 0. ,  0. ],
>         [ 0.5,  0. ],
>         [ 1. ,  0. ],
>         [ 1.5,  0. ],
>         [ 2. ,  0. ]],
>
>        [[ 0. ,  0.5],
>         [ 0.5,  0.5],
>         [ 1. ,  0.5],
>         [ 1.5,  0.5],
>         [ 2. ,  0.5]],
>
>        [[ 0. ,  1. ],
>         [ 0.5,  1. ],
>         [ 1. ,  1. ],
>         [ 1.5,  1. ],
>         [ 2. ,  1. ]],
>
>        [[ 0. ,  1.5],
>         [ 0.5,  1.5],
>         [ 1. ,  1.5],
>         [ 1.5,  1.5],
>         [ 2. ,  1.5]],
>
>        [[ 0. ,  2. ],
>         [ 0.5,  2. ],
>         [ 1. ,  2. ],
>         [ 1.5,  2. ],
>         [ 2. ,  2. ]]], dtype=float32)
>
> It also worked with VectorImages:
>
> In [22]: ImageType=itk.VectorImage[itk.F,2]
>
> In [23]: source=itk.PhysicalPointImageSource[ImageType].New()
>
> In [24]: source.SetOrigin([0,0])
>
> In [25]: source.SetSize([5,5])
>
> In [26]: source.SetSpacing([.5,.5])
>
> In [27]: source.Update()
>
> In [28]: itk.GetArrayFromImage(source)
>
>
>
> To answer your questions:
>
> 1) This filter is only wrapped over image of vectors and vector images.
> You can have the list of types that are available in Python with
> itk.PhysicalPointImageSource.GetTypes()
>
> 2) You may have a version of ITK Python for which VectorImage is not
> wrapped for PyBuffer. You can try to update your ITK Python [1]
>
> Hope this helps,
>
> Francois
>
>
> [1] https://github.com/InsightSoftwareConsortium/ITKPythonPackage
>
>
>
> On Fri, Apr 28, 2017 at 12:05 PM, D'Isidoro Fabio <fisidoro at ethz.ch>
> wrote:
>
> Thank you for your reply.
>
>
>
> 1)     I have tried as a first step the following:
>
>
>
> DRRPhy = itk.PhysicalPointImageSource[itk.Image[itk.Point[itk.F,
> Dimension], Dimension]].New()
>
>
>
> But I get the error:
>
>
>
> KeyError: "itkTemplate : No template (<class 'itkPointPython.itkPointF3'>,
> 3) for the itk::Image class"
>
>
>
> So I guess I can’t generate an image with itk.Points as PixelType.
> Therefore I chose to use a VectorImage as ImageType in Input to the
> PhysicalPointImageSource.
>
>
>
> sourceImage = itk.PhysicalPointImageSource[itk.VectorImage[itk.F,
> Dimension]].New()
>
> sourceImage.SetSize(size)
>
> sourceImage.SetSpacing(spacing)
>
> sourceImage.SetOrigin(origin)
>
> #DRRPhy.SetIndex(start) it does not work, how to set the start index?
>
> DRRPhy.SetDirection(direction)
>
>
>
> 2)     However, using PyBuffer to get a numpy array form that does not
> work:
>
>
>
> sourceImageArray = itk. PyBuffer[ImageType].GetArrayFromImage(sourceImage)
>
>
>
> I get the error:
>
>
>
> AttributeError: 'itkPhysicalPointImageSourceVIF3' object has no attribute
> 'GetLargestPossibleRegion'
>
>
>
>
>
> Am I doing the right thing in point 1) ?
>
> How could I solve point 2) ?
>
>
>
> Thank you !
>
>
>
> Fabio.
>
>
>
> *From:* Lowekamp, Bradley (NIH/NLM/LHC) [C] [mailto:blowekamp at mail.nih.gov]
>
> *Sent:* Donnerstag, 27. April 2017 23:16
> *To:* D'Isidoro Fabio <fisidoro at ethz.ch>
> *Cc:* insight-users at itk.org
> *Subject:* Re: [ITK] [ITK-users] ITK PyBuffer get physical point
> coordinates
>
>
>
> Hi!
>
>
>
> You can generate an image of the physical locations of each pixel with the
> PhysicalPointImageSource filter:
>
> https://itk.org/Doxygen/html/classitk_1_1PhysicalPointImageSource.html
>
>
>
> Very recently added is the SetReferenceImage, which can be use to copy the
> meta data from one image to the parameters of this filter. If your version
> is not super recent then you can manually set the Size, Spacing, Origin,
> Direction and StartIndex.
>
>
>
> After you generate the point image you should be able to convert to with
> the same method.
>
>
>
> HTH,
>
> Brad
>
>
>
>
>
> On Apr 27, 2017, at 4:58 PM, D'Isidoro Fabio <fisidoro at ethz.ch> wrote:
>
>
>
> Hallo,
>
>
>
> I know it’s possible to get a numpy array of an itk image with:
>
>
>
> itk.PyBuffer[ImageType].GetArrayFromImage(Image)
>
>
>
> Is it by any chance also possible to obtain a (flattended) numpy array of
> the physical coordinates of the image pixels too?
>
>
>
> Thank you!
>
>
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.php
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/insight-users
> _______________________________________________
> Community mailing list
> Community at itk.org
> http://public.kitware.com/mailman/listinfo/community
>
>
>
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.php
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/insight-users
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/insight-users/attachments/20170502/d799585a/attachment-0001.html>


More information about the Insight-users mailing list