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

D'Isidoro Fabio fisidoro at ethz.ch
Wed Apr 12 14:16:10 EDT 2017


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/Tools/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/Tools/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/Tools/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


More information about the Community mailing list