[ITK] [ITK-users] ITK Python: numpy to itk image (and viceversa)

Dženan Zukić dzenanz at gmail.com
Wed Apr 12 16:50:08 EDT 2017


Hi Fabio,

it looks like we will have to wait somewhat until VS writers get around to
implement __builtin_offsetof
<https://developercommunity.visualstudio.com/content/problem/22196/static-assert-cannot-compile-constexprs-method-tha.html>

I don't know why it doesn't trigger when Python wrapping is OFF. I will
look into it more.

Regards,
Dženan Zukić, PhD, Senior R&D Engineer, Kitware (Carrboro, N.C.)

On Wed, Apr 12, 2017 at 4:26 PM, Francois Budin <francois.budin at kitware.com>
wrote:

> Fabio,
>
> To add a little bit of extra information about using BridgeNumpy:
>
> From NumPy to ITK, the memory is never copied, and always shared between
> the NumPy array and the ITK image.
> From ITK to NumPy, the memory is not copied at first, if the data is not
> changed from the NumPy side (e.g. if the ITK image is changed, the NumPy
> array will be modified too), but if you modify the NumPy array directly,
> NumPy will create a copy of the data. This also means that if you were to
> modify the NumPy array, the modifications would not be visible in the ITK
> image.
>
> A limited workaround to this is:
>
> # Get NumPy array from image
> arr_image = itk.GetArrayFromImage(image)
> # Create copy of array that can be modified in-place
> arr = arr_image.copy()
> # Modify array
>> # Update image in-place with new array
> arr_image.setfield(arr,arr_image.dtype)
>
> The advantage of this method is that the image will be updated. However,
> there is an explicit copy of your data.
> If the copy step is skipped and the original NumPy array is modified, a
> copy of the data will be performed by NumPy and you may not be aware of it
> until you see that the data in the NumPy array and the image do not match
> anymore.
>
> Hope this helps,
> Francois
>
> On Wed, Apr 12, 2017 at 4:02 PM, D'Isidoro Fabio <fisidoro at ethz.ch> wrote:
>
>> Ok, thanks. Moe specifically I am using now *Visual Studio 2017 Preview*.
>>
>>
>>
>> Fabio.
>>
>> *From:* Dženan Zukić [mailto:dzenanz at gmail.com]
>> *Sent:* Mittwoch, 12. April 2017 21:54
>> *To:* D'Isidoro Fabio <fisidoro at ethz.ch>
>> *Cc:* Matt McCormick <matt.mccormick at kitware.com>; insight-users at itk.org
>>
>> *Subject:* Re: [ITK-users] ITK Python: numpy to itk image (and viceversa)
>>
>>
>>
>> Hi Fabio,
>>
>>
>>
>> let me give it a try with VS2017 and Wrapping+NumPy. I will report back
>> when I have an update.
>>
>>
>>
>> Regards,
>>
>> Dženan Zukić, PhD, Senior R&D Engineer, Kitware (Carrboro, N.C.)
>>
>>
>>
>> On Wed, Apr 12, 2017 at 2:16 PM, D'Isidoro Fabio <fisidoro at ethz.ch>
>> wrote:
>>
>> Thank you. I am trying to build ITK Wrap Python with
>> Module_BridgeNumPy=ON with Visual Studio 2017.
>>
>> I get the following type of errors:
>>
>> 12>C:/Program Files (x86)/Microsoft Visual Studio/Preview/Community/VC/To
>> ols/MSVC/14.10.25017/include\xstring(1905,26): error G3F63BFAE:
>> constexpr variable '_Memcpy_move_offset' must be initialized by a constant
>> expression
>> 12>        static constexpr size_t _Memcpy_move_offset =
>> offsetof(_Mydata_t, _Bx);
>> 12>                                ^
>>  ~~~~~~~~~~~~~~~~~~~~~~~~
>> 12>C:/Program Files (x86)/Microsoft Visual Studio/Preview/Community/VC/To
>> ols/MSVC/14.10.25017/include\stdexcept:23:21: note: in instantiation of
>> template class 'std::basic_string<char, std::char_traits<char>,
>> std::allocator<char> >' requested here
>> 12>                : _Mybase(_Message.c_str())
>> 12>                                  ^
>> 12>C:/Program Files (x86)/Microsoft Visual Studio/Preview/Community/VC/To
>> ols/MSVC/14.10.25017/include\xstring:1905:48: note: cast that performs
>> the conversions of a reinterpret_cast is not allowed in a constant
>> expression
>> 12>        static constexpr size_t _Memcpy_move_offset =
>> offsetof(_Mydata_t, _Bx);
>> 12>                                                      ^
>> 12>C:/Program Files (x86)/Windows Kits/10/Include/10.0.10586.0/ucrt\stddef.h:38:32:
>> note: expanded from macro 'offsetof'
>> 12>        #define offsetof(s,m) ((size_t)&reinterpret_cast<char const
>> volatile&>((((s*)0)->m)))
>>
>>
>> The build worked with Visual Studio 2015 instead.
>>
>> Can I fix this issue with Visual Studio 2017 somehow?
>>
>> Thank you.
>>
>> ----------------------------------------------------------------------
>> Fabio D’Isidoro - PhD Student
>> Institute of Biomechanics
>> HPP O 14
>> Hönggerbergring 64
>> 8093 Zürich, Switzerland
>>
>>
>> -----Original Message-----
>> From: Matt McCormick [mailto:matt.mccormick at kitware.com]
>> Sent: Montag, 3. April 2017 20:41
>> To: D'Isidoro Fabio <fisidoro at ethz.ch>
>> Cc: insight-users at itk.org
>> Subject: Re: [ITK-users] ITK Python: numpy to itk image (and viceversa)
>>
>> Hallo Fabio,
>>
>>
>> > I use ITK with Python Wrap. I need to interface my Python code with a
>> > Cython-wrapped C++ code that takes only numpy array as input and
>> > returns numpy array as output.
>>
>> Cool. By the way, you may be interested in scikit-build [1], which is a
>> good way to build Cython-wrapped C++ code. We are using it for the ITK and
>> SimpleITK Python packages, and it has good Cython and CMake support.
>>
>>
>> > Hence, I need to convert the Python itk images into numpy array to be
>> > given as input to the wrapped C++ code, and then convert the numpy
>> > array in output from the wrapped C++ code back into python itk images.
>> >
>> >
>> >
>> > Question 1) How can I do that in an efficient way? I found some posts
>> > on itk.PyBuffer but I could not find anywhere any reference on how to
>> > install it on my itk wrap build.
>>
>> Yes, itk.PyBuffer works great for that. Please review a PR for some
>> additional documentation:
>>
>>   https://github.com/InsightSoftwareConsortium/ITKBridgeNumPy/pull/18
>>
>> This has been available in ITK for a few releases as a Remote module,
>> which can be enabled by setting
>>
>>   Module_BridgeNumPy=ON
>>
>> in ITK's CMake configuration.
>>
>>
>> Since ITK 4.11.0, it is easier to build since it does not require the
>> NumPy headers.
>>
>>
>> In current ITK Git master (to be 4.12.0) the module is enabled by default.
>>
>>
>> Nightly ITK Python packages for ITK Git master are now being built:
>>
>>   https://github.com/InsightSoftwareConsortium/ITKPythonPackage
>>
>> macOS and Linux are available. Windows packages will be available over
>> the coming weeks.
>>
>>
>>
>> > Question 2) The purpose of writing a part of my algorithm in C++ is to
>> > speed up the code. If the conversion between python itk images and
>> > numpy arrays is slow, I would lose all the speed gain obtained with the
>> C++ implementation.
>> > Are there better ways to deal with that?
>>
>> The newer versions ITKBridgeNumPy use a NumPy array view, which does not
>> do any copies during the conversion, and it is very fast.
>>
>>
>> HTH,
>> Matt
>>
>>
>> [1] http://scikit-build.org/
>> _____________________________________
>> 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
>>
>>
>>
>> _____________________________________
>> 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/community/attachments/20170412/20c17151/attachment.html>
-------------- next part --------------
_____________________________________
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


More information about the Community mailing list