[vtkusers] vtk image from itk
Bradley Lowekamp
blowekamp at mail.nih.gov
Mon Jun 15 13:54:07 EDT 2015
Hello,
It looks like you are now mixing SimpleITK, WrapITK, and VTK for python. I am not sure adding WrapITK will make things any easier.
There is a complication with transitioning between SimpleITK and numpy with the order of the indexes. In the SimpleITK image buffer x is the fast axis and z is the slowest.
When you index a SimpleITK image you do:
img.GetPixel(x,y,z) or img[x,y,z]
similarly
(sizeX, sizeY, sizeZ) = img.GetSize()
Now, numpy arrays are indexed in the oppo site order:
nda = sitk.GetArrayFromImage(img)
nda[z,y,z]
and
(sizeZ, sizeY, sizeX) = nda.shape()
There is also a IPython notebook which covers this issue [1].
HTH,
Brad
[1] http://simpleitk.github.io/SimpleITK-Notebooks/01_Image_Basics.html
On Jun 15, 2015, at 1:38 PM, gregthom992 <gregthom992 at gmail.com> wrote:
> Hi David ,
>
> Thanks for your information. I ignored the origin and it didn't fix my
> images.
> I then made another attempt at a function to convert to vtk from SimpleITK.
> This still gave me the "mangled" image. I am seeing that my img_array.shape
> is [198 512 512] but my desired image Dimension should be [512 512 198]
> Do you think that is the problem ? I am not sure how SimpleITK
> GetArrayFromImage
> does with flipping dimensions. Any help will be appreciated.
> Greg
>
>
> ##CODE:
>
> # image = sitk image
> sitktype = image.GetPixelID()
> vtktype = pixelmap[sitktype]
>
> # Get ndarray from SimpleITK image
> img_array = sitk.GetArrayFromImage(sitk.Cast(image, sitk.sitkFloat32))
>
> Dimension = image.GetDimension()
> OutputPixelType = itk.F
> OutputImageType = itk.Image[OutputPixelType, Dimension]
>
> #PyBuffer connector
> pyBufferConnector = itk.PyBuffer[OutputImageType]
>
> # Get ITK image from ndarray
> itkImage = pyBufferConnector.GetImageFromArray(img_array)
> # preserve ITK image information
> itkImage.SetSpacing(image.GetSpacing())
> #itkImage.SetOrigin(image.GetOrigin())
> #itkImage.SetDirection(image.GetDirection())
>
> itk_vtk_converter = itk.ImageToVTKImageFilter[OutputImageType].New()
> itk_vtk_converter.SetInput(itkImage)
> itk_vtk_converter.Update()
>
> ##END CODE
>
>
>
>
>
> --
> View this message in context: http://vtk.1045678.n5.nabble.com/vtk-image-from-itk-tp5732315p5732342.html
> Sent from the VTK - Users mailing list archive at Nabble.com.
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>
> Search the list archives at: http://markmail.org/search/?q=vtkusers
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers
More information about the vtkusers
mailing list