[Insight-users] How can I implement the conversion between itk image and SimpleITK image in Python?
Bradley Lowekamp
blowekamp at mail.nih.gov
Mon Jan 27 08:44:05 EST 2014
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
More information about the Insight-users
mailing list