[Insight-users] converting 4d itk::Image to vtkImageData

chasank chasank at gmail.com
Thu Jun 27 08:10:52 EDT 2013


Hi all,

We have a 4-dimension (3d+time) nifti file. Our purpose is to visualize 3d
images at specified time sequence.

We can read the file and store it into itk::Image<unsigned short, 4 >. After
that, we convert itk::Image< unsigned short, 4 > to itk::VectorImage<
unsigned short, 3 >. However, we cannot convert itk::VectorImage< unsigned
short, 3 > to vtkImageData. We used itk::ImageToVTKImageFilter for this
purpose; but we failed.

Code we've implemented for this purpose is below.

#include <itkImage.h>
#include <itkNiftiImageIO.h>
#include <itkImageFileReader.h>
#include <itkImageToVTKImageFilter.h>
#include <itkImageRegionIteratorWithIndex.h>

#include <iostream>
#include <string>

int main(int argc, char **argv)
{    
    try
    {
        std::string inputFile = argv[1];

        typedef unsigned short                              PixelType;
        typedef itk::VectorImage< PixelType, 3 >     GradientImageType;
        typedef itk::Image< PixelType, 4 >              InputImageType;
        typedef itk::Image< PixelType, 3 >              ImageType3D;

        itk::ImageFileReader< InputImageType >::Pointer reader =
                                                                   
itk::ImageFileReader< InputImageType >::New();
        itk::NiftiImageIO::Pointer niftiIO = itk::NiftiImageIO::New();
        reader->SetFileName( inputFile );
        reader->SetImageIO(niftiIO);
        reader->Update();

        itk::Image<PixelType, 4>::Pointer image4D = reader->GetOutput();
        image4D->Print(std::cout);

        //Set up the gradient image size
        typedef GradientImageType::RegionType   GradRegionType;
        typedef GradientImageType::SizeType     GradSizeType;
        typedef GradientImageType::IndexType    GradIndexType;
        typedef InputImageType::IndexType       Img4dIndexType;

        itk::VectorImage<PixelType, 3>::Pointer gradientImage =
itk::VectorImage<PixelType, 3>::New();

        GradSizeType  sizeGradImage;
        InputImageType::SizeType size4D =
image4D->GetLargestPossibleRegion().GetSize();
        for ( unsigned int i = 0; i <= 2; i++)
        {
            sizeGradImage[i] = size4D[i];
        }

        gradientImage->SetVectorLength(size4D[3]);

        GradIndexType   indexGradImage = {{ 0, 0, 0 }};
        GradRegionType  regionGradImage;
        regionGradImage.SetSize(  sizeGradImage );
        regionGradImage.SetIndex( indexGradImage);
        gradientImage->SetRegions( regionGradImage );

        InputImageType::SpacingType img4Dspacing = image4D->GetSpacing();
        InputImageType::PointType img4Dorigin = image4D->GetOrigin();
        InputImageType::DirectionType img4Ddir = image4D->GetDirection();

        GradientImageType::SpacingType gradSpacing;
        GradientImageType::PointType gradOrigin;
        GradientImageType::DirectionType gradDirs;

        for (unsigned int i = 0; i <= 2; i++)
        {
            gradSpacing[i] = img4Dspacing[i];
            gradOrigin[i] = img4Dorigin[i];
        }

        for (unsigned int i = 0; i < 3; ++i)
        {
            for (unsigned int j = 0; j < 3; ++j)
            {
                gradDirs[i][j] = img4Ddir[i][j];
            }
        }

        gradientImage->SetSpacing( gradSpacing );
        gradientImage->SetOrigin( gradOrigin );
        gradientImage->SetDirection( gradDirs );
        gradientImage->Allocate();

        typedef itk::ImageRegionIteratorWithIndex< GradientImageType >
IteratorType;
        typedef GradientImageType::PixelType GradPixelType;

        IteratorType it( gradientImage, gradientImage->GetRequestedRegion()
);

        for ( it.GoToBegin(); !it.IsAtEnd(); ++it)
        {
            GradIndexType   gradIndex = it.GetIndex();
            GradPixelType   gradPix = it.Get();
            Img4dIndexType  img4dIndex;
            img4dIndex[0] = gradIndex[0];
            img4dIndex[1] = gradIndex[1];
            img4dIndex[2] = gradIndex[2];

            for ( unsigned int i = 0; i < size4D[3]; ++i )
            {
                img4dIndex[3] = i;
                gradPix.SetElement( i, image4D->GetPixel( img4dIndex ) );
            }
            it.Set( gradPix );
        }

        gradientImage->Print(std::cout);


        // How can I convert gradientImage to vtkImageData ? 
        /*
        itk::ImageToVTKImageFilter< GradientImageType >::Pointer converter =
                itk::ImageToVTKImageFilter< GradientImageType >::New();
        converter->SetInput(gradientImage);
        converter->Update();

        vtkSmartPointer< vtkImageData > imageData = vtkSmartPointer<
vtkImageData >::New();
        imageData->DeepCopy(converter->GetOutput());
        */
    }

    catch ( itk::ExceptionObject &ex )
    {
        std::cerr << "Exception : " << ex.what() << std::endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}



--
View this message in context: http://itk-users.7.n7.nabble.com/converting-4d-itk-Image-to-vtkImageData-tp31770.html
Sent from the ITK - Users mailing list archive at Nabble.com.


More information about the Insight-users mailing list