[Insight-users] How can I implement the conversion between itk image and SimpleITK image in Python?
Bradley Lowekamp
blowekamp at mail.nih.gov
Tue Jan 28 08:35:35 EST 2014
On Jan 27, 2014, at 11:48 PM, Guo Hengkai <guohengkaighk at gmail.com> wrote:
> Hello Brad,
>
> Thank you for your respond. And I agree with you. But when building the PyBuffer, I fail in compiling it.
>
> My environment is Win7 64bit + ITK4.4.2 + Python2.7.3 + VS2010(32bit). And because I failed in wrapping with python in ITK (error: '__noop' was not declared in this scope), I just used Pythonxy (http://code.google.com/p/pythonxy/) instead.
>
> I added PyBuffer in the directory "/Modules/External/" and tried to compile the ITK again in VS2010. But this time I got the error information below:
>
> 5> In file included from D:/Develop/bin/InsightToolkit/Wrapping/Modules/ITKBufferConvertion/itkPyBuffer.cxx:21:
> 5> D:/Develop/src/InsightToolkit-4.4.2/Modules/External/itkPyBuffer/include/itkPyBuffer.h:31:25: error: arrayobject.h: No such file or directory
> 5> In file included from D:/Develop/bin/InsightToolkit/Wrapping/Modules/ITKBufferConvertion/itkPyBuffer.cxx:21:
> 5> D:/Develop/src/InsightToolkit-4.4.2/Modules/External/itkPyBuffer/include/itkPyBuffer.h:97: error: 'NPY_DOUBLE' was not declared in this scope
> 5> D:/Develop/src/InsightToolkit-4.4.2/Modules/External/itkPyBuffer/include/itkPyBuffer.h:98: error: 'NPY_FLOAT' was not declared in this scope
> 5> D:/Develop/src/InsightToolkit-4.4.2/Modules/External/itkPyBuffer/include/itkPyBuffer.h:100: error: 'NPY_COMPLEX64' was not declared in this scope
> 5> D:/Develop/src/InsightToolkit-4.4.2/Modules/External/itkPyBuffer/include/itkPyBuffer.h:101: error: 'NPY_COMPLEX128' was not declared in this scope
> 5> D:/Develop/src/InsightToolkit-4.4.2/Modules/External/itkPyBuffer/include/itkPyBuffer.h:103: error: 'NPY_LONGLONG' was not declared in this scope
> 5> D:/Develop/src/InsightToolkit-4.4.2/Modules/External/itkPyBuffer/include/itkPyBuffer.h:104: error: 'NPY_ULONGLONG' was not declared in this scope
> 5> D:/Develop/src/InsightToolkit-4.4.2/Modules/External/itkPyBuffer/include/itkPyBuffer.h:106: error: 'NPY_LONG' was not declared in this scope
> 5> D:/Develop/src/InsightToolkit-4.4.2/Modules/External/itkPyBuffer/include/itkPyBuffer.h:107: error: 'NPY_ULONG' was not declared in this scope
> 5> D:/Develop/src/InsightToolkit-4.4.2/Modules/External/itkPyBuffer/include/itkPyBuffer.h:109: error: 'NPY_INT' was not declared in this scope
> 5> D:/Develop/src/InsightToolkit-4.4.2/Modules/External/itkPyBuffer/include/itkPyBuffer.h:110: error: 'NPY_UINT' was not declared in this scope
> 5> D:/Develop/src/InsightToolkit-4.4.2/Modules/External/itkPyBuffer/include/itkPyBuffer.h:112: error: 'NPY_USHORT' was not declared in this scope
> 5> D:/Develop/src/InsightToolkit-4.4.2/Modules/External/itkPyBuffer/include/itkPyBuffer.h:113: error: 'NPY_SHORT' was not declared in this scope
> 5> D:/Develop/src/InsightToolkit-4.4.2/Modules/External/itkPyBuffer/include/itkPyBuffer.h:115: error: 'NPY_BYTE' was not declared in this scope
> 5> D:/Develop/src/InsightToolkit-4.4.2/Modules/External/itkPyBuffer/include/itkPyBuffer.h:116: error: 'NPY_UBYTE' was not declared in this scope
>
> Do you have any idea? Should I change to VS2008 because someone said that it would not be possible because python 2.7.3 was not compiled with VS10 (http://permalink.gmane.org/gmane.comp.lib.itk.user/38599)?
>
> Thank you!
> Hengkai
>
>
>
> 2014-01-27 Bradley Lowekamp <blowekamp at mail.nih.gov>
> Hello,
>
> It's been a little bit since I did this. But just used numpy/PyBuffers to convert between SimpleITK and WrapITK. I added the following repository as an External module when I built WrapITK:
>
> https://github.com/tobiasmaier/itkPyBuffer
>
> That gave me the needed numpy conversion needed to get an image out of WrapITK. While SimpleITK has the GetImageFromArray and GetArrayFromImage methods.
>
> This does solve the problem of converting between the two wrapping layers. You are looking for a more efficient direct conversion? Why does this not direcly solve your problem? Due to some of SimpleITK's lazy copying, you may get some unexpected behavior by heavily mixing between the two. So there are going to be some usage complication in mixing the two. But it should be possible to write some SWIG interface code to better integrate the two. I would hope that it couple be done with out introducing a dependency directly between the two. If you get an idea or a solution here please let me know.
>
> Brad
>
> On Jan 27, 2014, at 8:18 AM, Hengkai Guo <guohengkaighk at gmail.com> wrote:
>
> > Dear all,
> >
> > I'm now using SimpleITK in Python. When combining itk with it, I had a problem.
> >
> > I know the way to implement the conversion between itk image and SimpleITK
> > image in C++, which is an example in SimpleITK (Examples/ITKIntegration.cxx).
> >
> > //
> > // Extract the itk image from the SimpleITK image
> > //
> > InternalImageType::Pointer itkImage =
> > dynamic_cast <InternalImageType*>( image.GetITKBase() );
> > ...
> > //
> > // Return to the simpleITK setting by making a SimpleITK image using the
> > // output of the blur filter.
> > //
> > sitk::Image blurredImage = sitk::Image( blurFilter->GetOutput() );
> >
> > But in Python, when I used "sitk.Image(itkImage)" in which itkImage is an
> > itkImage, I got an error "this = _SimpleITK.new_Image(*args)
> > NotImplementedError: Wrong number or type of arguments for overloaded
> > function 'new_Image'". And in python it's impossible to convert the type
> > like "dynamic_cast" in C++.
> >
> > There's one possible solution for this, which use PyBuffer to convert
> > itkImage into numpy array and then use SimpleITK to convert array into
> > SimpleITK image finally. But I fail in building the itkPyBuffer. And I think
> > this method isn't direct to solve this problem.
> >
> > I have tried my best to search in the Internet, but found nothing. Can you
> > help me to convert the C++ code into available python code? 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://www.itk.org/mailman/listinfo/insight-users
>
>
>
>
> --
> Guo Hengkai (Undergraduate)
>
> 郭亨凯 (本科生)
> Dept. of Biomedical Engineering
> School of Medicine,Tsinghua Univ.
>
> 清华大学,医学院,生物医学工程系
> Beijing, China 中国,北京
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20140128/fa1c4bef/attachment.html>
More information about the Insight-users
mailing list