[vtkusers] VTKPolyData to 3D image (David Welch)

somi seesomi at gmail.com
Tue Apr 20 09:48:22 EDT 2010


Hi,
While there are some filters in vtk which can be used to voxelize polydata
(vtkPolyDataToImageStencil, vtkImplicitModeller, vtkSelectEnclosedPoints +
image iterator etc) , I found them to be too slow, esp for large images.
If you are ok with a hybrid solution (ITK + VTK ), then I would suggest the
following pipeline:  vtkPolyData  ->ITKSpatialObject
->ItkSpatialObjectToImageFilter. I found this to be much faster. This works
for simple shapes like ellipsoid, cylinders, tubes etc. With some
modifications you could convert any polydata to image (using
meshSpatialObject ).

For example, to convert a sphere polydata you could use the following code:

#include <vtkPolyData.h>
#include <vtkSphereSource.h>
#include <itkImage.h>
#include <vtkSmartPointer.h>
#include <itkSpatialObjectToImageFilter.h>
#include <itkImageFileWriter.h>
#include <itkEllipseSpatialObject.h>

#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif

#ifdef __BORLANDC__
#define ITK_LEAN_AND_MEAN
#endif


int main( int argc, char *argv[] )
{

    // Generate a Sphere in VTK
    vtkSmartPointer<vtkSphereSource> polyData =
vtkSmartPointer<vtkSphereSource>::New();
    polyData->SetRadius(10);
    polyData->SetThetaResolution(10);
    polyData->SetPhiResolution(10);
    polyData->SetCenter(50,50,50);
    polyData->Update();



    typedef unsigned char  PixelType;
    const unsigned int    Dimension = 3;

   // Initialize Ellise in ITK
    typedef itk::EllipseSpatialObject<Dimension>   EllipseType;
    EllipseType::Pointer myEllipse = EllipseType::New();

    //Set Ellipse radius
    EllipseType::ArrayType radius;
    for(unsigned int i = 0; i<3; i++)
      {
      radius[i] = polyData->GetRadius();
      }
    myEllipse->SetRadius(radius);


    // Convert  ITK Mesh to ITK Mesh spatial object

    typedef itk::Image< PixelType, Dimension >       ImageType;

    typedef itk::SpatialObjectToImageFilter<
             EllipseType, ImageType >   SpatialObjectToImageFilterType;

    SpatialObjectToImageFilterType::Pointer imageFilter =
            SpatialObjectToImageFilterType::New();

    // Set Size, Spacing and origin
    ImageType::SizeType size;
    size[ 0 ] =  100;
    size[ 1 ] =  100;
    size[ 2 ] = 100;
    imageFilter->SetSize( size );
    ImageType::SpacingType spacing;
    spacing[0] =  100.0 / size[0];
    spacing[1] =  100.0 / size[1];
    spacing[2] =  100.0 / size[2];

    imageFilter->SetSpacing( spacing );
    imageFilter->SetInput( myEllipse );

    ImageType::PointType origin;
    origin[0]=-50;
    origin[1]=-50;
    origin[2]=-50;
    imageFilter->SetOrigin(origin);


    //Set Inside/Outside voxel value
    const PixelType empty  = 0;
    const PixelType filled =  1;
    myEllipse->SetDefaultInsideValue(  filled);
    myEllipse->SetDefaultOutsideValue(   empty   );
    imageFilter->SetUseObjectValue( true );
    imageFilter->SetOutsideValue( empty );
    imageFilter->Update();

    // Write the image
    typedef itk::ImageFileWriter< ImageType >     WriterType;
    WriterType::Pointer writer = WriterType::New();

    writer->SetFileName("Output.nii");
    writer->SetInput( imageFilter->GetOutput() );
    try
    {
        imageFilter->Update();
        writer->Update();
    }
    catch( itk::ExceptionObject & excp )
    {
        std::cerr << excp << std::endl;
        return EXIT_FAILURE;
    }


    return EXIT_SUCCESS;
}


Thanks,
Somesh

Date: Mon, 19 Apr 2010 11:15:18 -0500
From: David Welch <dmwelch at engineering.uiowa.edu
>
Subject: [vtkusers] VTKPolyData to 3D image
To: vtkusers at vtk.org
Message-ID:
       <q2yfdd3cdf91004190915qf4d908fpf6d53521e1872067 at mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hi all,

I'm want to voxelize a simple sphere surface to a 3D image, inside positive
values, outside negative.  Does anyone have a suggestion how I can go about
doing this?

Thanks,

--
David Welch
Graduate Student
Dept. of Biomedical Engineering
University of Iowa
Lab: (319) 335-5279
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20100420/5ae5da43/attachment.htm>


More information about the vtkusers mailing list