VTK的三维imagedata转化为ITK的三维imagedata,以及和ITK的三维imagedata转化为VTK的三维imagedata的代码:
#include <itkVTKImageToImageFilter.h>
#include <vtkImageData.h>
#include <itkImageToVTKImageFilter.h>
#include <itkImage.h>
#include <vtkSmartPointer.h>
typedef int PixelType;
typedef itk::Image< PixelType, 3> ImageType;
//imageVTK:传入的vtk体数据 vtkSmartPointer<vtkImageData> imageVTK = vtkSmartPointer<vtkImageData>::New();
bool VTKImageToITKImage(vtkImageData* imageVTK)
{
typedef itk::VTKImageToImageFilter<ImageType> vtkToitkFilter;
vtkToitkFilter::Pointer itkFilter = vtkToitkFilter::New();
itkFilter->SetInput(imageVTK);
itkFilter->UpdateLargestPossibleRegion();
try
{
itkFilter->Update();
}
catch (itk::ExceptionObject)
{
std::cerr<<"vtk to itk failed!"<<std::endl;
return false;
}
return true;
}
//itkImage: 传入的itk体数据 ImageType::Pointer itkImage = ImageType::New();
bool ITKImageToVTKImage(ImageType::Pointer itkImage)
{
typedef itk::ImageToVTKImageFilter<ImageType> ConnectorType;
ConnectorType::Pointer connector = ConnectorType::New();
connector->SetInput(itkImage );
try
{
connector->Update();
}
catch (itk::ExceptionObject)
{
std::cerr<<"itk to vtk failed!"<<std::endl;
return false;
}
vtkSmartPointer<vtkImageData> imageVTK = vtkSmartPointer<vtkImageData>::New();
imageVTK->ShallowCopy(connector->GetOutput());
return true;
}
ITK/VTK图像数据差别
?ITK与VTK的图像数据的坐标原点都在图像的左下角;
但是在读取图像时,二者的存储方式不同。以常用的dicom图像为例,图像的第一个像素定义为左上角。当使用VTK进行读取时,VTK将图像的第一个像素存储在左下角,因此,读取的图像是沿着Y轴反转的。而ITK则没有翻转的过程,其按照图像像素排列的原始顺序读入图像,然后将坐标原点移动到图像的左下角。二者图像数据读取方式的不同,导致二者之一的图像必须进行翻转,才能与另一个匹配。
ITK TO VTK ITK转化为VTK图像使用类
ITK/VTK数据转换的类均存放在ITKVTKGlue模块? itkImageToVTKImageFilter?中:
#include <itkImageToVTKImageFilter.h>
#include "itkVTKImageIOFactory.h"
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImageRegionIterator.h"
using PixelType = signed short;
constexpr unsigned int Dimension = 3;
//初始化待读取序列的格式类型
using ImageType = itk::Image<PixelType, Dimension>;
using ReaderType = itk::ImageSeriesReader<ImageType>;
using ImageIOType = itk::GDCMImageIO;
using NamesGeneratorType = itk::GDCMSeriesFileNames;
using ConnectorType = itk::ImageToVTKImageFilter<ImageType>;
auto connector1 = ConnectorType::New();
connector1->SetInput(reader->GetOutput());
connector1->Update();
VTK TO ITK? VTK转化为ITK图像使用类itkVTKImagetoImageFilter:
#include <itkVTKImageToImageFilter.h>
#include "itkVTKImageIOFactory.h"
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImageRegionIterator.h"
using PixelType = signed short;
constexpr unsigned int Dimension = 3;
//初始化待读取序列的格式类型
using ImageType = itk::Image<PixelType, Dimension>;
vtkNew<vtkImageData> mTmpImageData;
typedef itk::VTKImageToImageFilter<ImageType> VTKImageToImageType;
VTKImageToImageType::Pointer vtkImageToImageFilter1 = VTKImageToImageType::New();
vtkImageToImageFilter1->SetInput(mTmpImageData);
//vtkImageToImageFilter->SetInput(reader->GetOutput());
vtkImageToImageFilter1->Update();
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 < unsigned char, 3 > ImageType;
void showITKimageInVTKqwidget(ImageType::Pointer visualizing)
{
typedef itk::ImageToVTKImageFilter<ImageType> 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
ImageType::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));
ImageType::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();
}