[Insight-users] Wrapping a custom C++ function in Python
Julien Lamy
lamy at unistra.fr
Tue Jun 16 08:17:52 EDT 2009
Gaëtan Lehmann a écrit :
>
> Le 9 juin 09 à 11:44, Julien Lamy a écrit :
>
>> Gaëtan Lehmann a écrit :
>>> Le 8 juin 09 à 15:12, Julien Lamy a écrit :
>>>>
>>>> I'm trying to wrap a simple C++ function in Python using WrapITK,
>>>> but the Python script fails with the following traceback :
>>>> TypeError: in method 'test_wrapper', argument 1 of type 'itk::Image<
>>>> unsigned short,3 > *'
>>>>
>>>> My C++ function is :
>>>>
>>>> bool test_wrapper(itk::Image<unsigned short, 3>::Pointer input)
>>>> {
>>>> std::cout << input->GetRequestedRegion().GetSize() << std::endl;
>>>> }
>>>>
>>>> My Python script is :
>>>> import numpy
>>>> import itk
>>>> from Wrapper import test_wrapper
>>>>
>>>> data = numpy.arange(3*5*7, dtype=numpy.uint16).reshape((3,5,7))
>>>> image_itk = itk.PyBuffer[itk.Image[itk.US,
>>>> 3]].GetImageFromArray(data)
>>>> test_wrapper(image_itk)
>>>>
>>>> My SWIG wrapper is :
>>>>
>>>> %module Wrapper
>>>> %{
>>>> #include "test_Wrapper.hpp"
>>>> %}
>>>>
>>>> bool test_wrapper(itk::Image<unsigned short, 3> * input);
>>> this last line should be
>>> bool test_wrapper(itkImageUS3 * input);
>>> and you have to #include Base.includes (located at
>>> /usr/local/lib/InsightToolkit/WrapITK/Configuration/Typedefs/Base.includes
>>> on my system), to %include wrap_itkImage.i
>>> (/usr/local/lib/InsightToolkit/WrapITK/Configuration/Typedefs/wrap_itkImage.i).
>>>
>>
>> I'm missing both files. There is actually no trace of a
>> WrapITK/Configuration/Typedefs directory on my system, either in the
>> source, build or installation directories. I compiled ITK 3.14.0 from
>> source with the following cmake command :
>> cmake -D BUILD_EXAMPLES=OFF -D BUILD_SHARED_LIBS=ON -D
>> BUILD_TESTING=OFF \
>> -D CMAKE_BUILD_TYPE=Release \
>> -D CMAKE_INSTALL_PREFIX=MY_LOCAL_DIR \
>> -D ITK_USE_REVIEW=ON \
>> -D USE_WRAP_ITK=ON \
>> -D PY_SITE_PACKAGES_PATH=MY_LOCAL_DIR/lib/python2.5/site-packages \
>> -D WRAP_ITK_JAVA=OFF -D WRAP_ITK_TCL=OFF \
>> ../InsightToolkit-3.14.0
>>
>
> Sorry, my mistake.
> I thought you were using wrapitk from google code, because you have
> listed swig in your programs, but I realize that you are using it only
> for your own project.
> You can't do that with wrapitk shipped with itk. I think the only way to
> go is to use an external project.
>
>
>> I looked through the CMake options, but I found nothing related to
>> wrapping and typedefs. I'll give a try to the packages listed on your
>> code.google.com page and see if I can come up with something.
>>
>
> Let me know if you found any problem.
I first tried the Ubuntu 9.04 (Jaunty) packages from the repository
listed on Google Code, but couldn't use them due to the SWIG 1.3.38/
Python 2.6 bug. So I got the SVN version (the new version of the
callbacks is great, btw), and finally achived what I wanted following
your advice on file inclusion. My SWIG file is now :
%module Wrapper
%{
#include "Base.includes"
#include "test_Wrapper.hpp"
%}
bool test_wrapper(itkImageUS3 * input);
I did not %include wrap_itkImage.i, as this caused errors when running
the Python script.
Thanks for your help.
--
Julien
More information about the Insight-users
mailing list