ITK与VTK数据转换

引言

ITK和VTK都是kitware推出的强大开源库,前者专注于图像分析和处理,后者专注于三维显示和三维模型操作。在实际应用的过程中,常常需要将两个库中的图像数据进行互传,实现二者之间的无缝连接。

ITK/VTK图像数据差别

ITK与VTK的图像数据坐标的原点都在图像的左下角,但是在读取图像时,二者的存储方式不同。以常用的dicom图像为例,图像的第一个像素定义为左上角。当使用VTK进行读取时,VTK将图像的第一个像素存储在左下角,因此,读取的图像是沿着Y轴反转的。而ITK则没有翻转的过程,其按照图像像素排列的原始顺序读入图像,然后将坐标原点移动到图像的左下角。二者图像数据读取方式的不同,导致二者之一的图像必须进行翻转,才能与另一个匹配。

常用类

ITK/VTK数据转换的类均存放在ITKVTKGlue模块中。
将ITK转化为VTK图像使用类 itkImageToVTKImageFilter,具体的示例如下:
  typedef itk::Image, 2> ImageType;
  typedef itk::ImageFileReader             ReaderType;
  typedef itk::ImageToVTKImageFilter       ConnectorType;
 
  ReaderType::Pointer reader = ReaderType::New();
  ConnectorType::Pointer connector = ConnectorType::New();
 
  reader->SetFileName(argv[1]);
  connector->SetInput(reader->GetOutput());
 
  vtkSmartPointer actor =
    vtkSmartPointer::New();
#if VTK_MAJOR_VERSION <= 5
  actor->SetInput(connector->GetOutput());
#else
  connector->Update();
  actor->GetMapper()->SetInputData(connector->GetOutput());
#endif
  vtkSmartPointer renderer =
    vtkSmartPointer::New();
  renderer->AddActor(actor);
  renderer->ResetCamera();
将VTK转化为ITK时,使用的类是itkVTKImagetoImageFilter,具体的示例如下:
  vtkSmartPointer reader =
    vtkSmartPointer::New();
  reader->SetFileName(argv[1]);
  // reader->SetNumberOfScalarComponents(1); //doesn't seem to work - use ImageLuminance instead
  reader->Update();
 
 
  // Must convert image to grayscale because itkVTKImageToImageFilter only accepts single channel images
  vtkSmartPointer luminanceFilter =
    vtkSmartPointer::New();
  luminanceFilter->SetInputConnection(reader->GetOutputPort());
  luminanceFilter->Update();
 
  typedef itk::Image ImageType;
 
  typedef itk::VTKImageToImageFilter VTKImageToImageType;
 
  VTKImageToImageType::Pointer vtkImageToImageFilter = VTKImageToImageType::New();
  vtkImageToImageFilter->SetInput(luminanceFilter->GetOutput());
  //vtkImageToImageFilter->SetInput(reader->GetOutput());
  vtkImageToImageFilter->Update();
 
  ImageType::Pointer image = ImageType::New();
  image->Graft(vtkImageToImageFilter->GetOutput()); // Need to do this because QuickView can't accept const
 
  QuickView viewer;
  viewer.AddImage(image.GetPointer()); // Need to do this because QuickView can't accept smart pointers
  viewer.Visualize();

注意事项

ITK/VTK二者图像数据互相转换后,可能会出现图像数据的spacing, direction, origin, position的变化,为了保证二者图像的一致性,常常需要人为设置这些参数,具体的示例如下:
/*
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 VisualizingImageType
void showITKimageInVTKqwidget(VisualizingImageType::Pointer visualizing)
{
     typedef itk::ImageToVTKImageFilter 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();
}

参考文献:

○ http://www.slicer.org/slicerWiki/index.php/Slicer:VTK_ITK_Coordinates
○ http://www.vtk.org/pipermail/vtkusers/2011-July/068611.html
○ http://itk.org/Wiki/ITK/Examples/IO/ImageToVTKImageFilter
○ http://itk.org/Wiki/ITK/Examples/Broken/Images/VTKImageToImageFilter
○ http://www.vtk.org/Wiki/VTK/ExamplesBoneYard/Cxx/VolumeRendering/itkVtkImageConvert
○ http://public.kitware.com/pipermail/insight-users/2012-February/043626.html
○ http://www.cmake.org/pipermail/insight-users/2004-February/006608.html

你可能感兴趣的:(ITK,ITK,vtk)