[Insight-users] Wrapping a custom C++ function in Python

Julien Lamy lamy at unistra.fr
Tue Jun 9 06:27:07 EDT 2009


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

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.

 > I'm not sure, because that's a function and wrapitk currently only wrap
 > classes, but it may be easier to do that with a wrapitk external project.

This is going to be complicated for our specific project. But I'll
create a small external WrapITK project and look at the generated Makefiles.

Thanks,
-- 
Julien


More information about the Insight-users mailing list