[vtkusers] Convert ITK image to VTK volume keeping proper orientation and position

Dženan Zukić dzenanz at gmail.com
Tue Jul 5 08:33:28 EDT 2011


I hope this function can save someone a lot of time and headaches. It takes
an itk::Image and converts it into a vtkVolume, keeping it in DICOM's
patient physical world space. Its main usage is combining it with segmented
structures (vtkPolyData) whose vertices are in DICOM patient space, that is,
vertex coordinates were obtained using
visualizing->TransformIndexToPhysicalPoint().

typedef itk::Image<unsigned char, 3> VisualizingImageType

void showITKimageInVTKqwidget(VisualizingImageType::Pointer visualizing)
{
    typedef itk::ImageToVTKImageFilter<VisualizingImageType>
itkVtkConverter;
    itkVtkConverter::Pointer conv=itkVtkConverter::New();
    conv->SetInput(visualizing);

    vtkGPUVolumeRayCastMapper *mapper = vtkGPUVolumeRayCastMapper::New();
    mapper->SetInput(conv->GetOutput());

    vtkVolume *volume=vtkVolume::New();
    volume->SetProperty( myTransferFunction );
    volume->SetMapper( mapper );

    // vtkVolume only manages spacing by itself.
    // Here we take care of position and orientation so it is in DICOM
physical space
VisualizingImageType::DirectionType d=visualizing->GetDirection();
 vtkMatrix4x4 *mat=vtkMatrix4x4::New(); //identity matrix
for (int i=0; i<3; i++)
 for (int k=0; k<3; k++)
mat->SetElement(i,k, d(i,k));
VisualizingImageType::PointType origin=visualizing->GetOrigin();
 volume->SetOrigin(-origin[0], -origin[1], -origin[2]);
volume->SetPosition(-origin[0], -origin[1], -origin[2]);
 for (int i=0; i<3; i++)
mat->SetElement(i,3, origin[i]);
volume->SetUserMatrix(mat);

    vtkRenderer *renderer =
myQWidget->GetRenderWindow()->GetRenderers()->GetFirstRenderer();
    renderer->AddVolume( volume );
    myQWidget->GetRenderWindow()->Render();
    renderer->ResetCamera();
}

Dženan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20110705/5356ab9f/attachment.htm>


More information about the vtkusers mailing list