用到的类为vtkPolyDataToImageStencil
效果:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void convertData2()
{
vtkSmartPointer sphereSource =
vtkSmartPointer::New();
sphereSource->SetRadius(20);
sphereSource->SetPhiResolution(30);
sphereSource->SetThetaResolution(30);
vtkSmartPointer pd = sphereSource->GetOutput();
sphereSource->Update();
vtkSmartPointer whiteImage =
vtkSmartPointer::New();
double bounds[6];
pd->GetBounds(bounds);
double spacing[3]; // desired volume spacing
spacing[0] = 0.5;
spacing[1] = 0.5;
spacing[2] = 0.5;
whiteImage->SetSpacing(spacing);
// compute dimensions
int dim[3];
for (int i = 0; i < 3; i++)
{
dim[i] = static_cast(ceil((bounds[i * 2 + 1] - bounds[i * 2]) / spacing[i]));
}
whiteImage->SetDimensions(dim);
whiteImage->SetExtent(0, dim[0] - 1, 0, dim[1] - 1, 0, dim[2] - 1);
double origin[3];
origin[0] = bounds[0] + spacing[0] / 2;
origin[1] = bounds[2] + spacing[1] / 2;
origin[2] = bounds[4] + spacing[2] / 2;
whiteImage->SetOrigin(origin);
#if VTK_MAJOR_VERSION <= 5
whiteImage->SetScalarTypeToUnsignedChar();
whiteImage->AllocateScalars();
#else
whiteImage->AllocateScalars(VTK_UNSIGNED_CHAR,1);
#endif
// fill the image with foreground voxels:
unsigned char inval = 255;
unsigned char outval = 0;
vtkIdType count = whiteImage->GetNumberOfPoints();
for (vtkIdType i = 0; i < count; ++i)
{
whiteImage->GetPointData()->GetScalars()->SetTuple1(i, inval);
}
// polygonal data --> image stencil:
vtkSmartPointer pol2stenc =
vtkSmartPointer::New();
#if VTK_MAJOR_VERSION <= 5
pol2stenc->SetInput(pd);
#else
pol2stenc->SetInputData(pd);
#endif
pol2stenc->SetOutputOrigin(origin);
pol2stenc->SetOutputSpacing(spacing);
pol2stenc->SetOutputWholeExtent(whiteImage->GetExtent());
pol2stenc->Update();
// cut the corresponding white image and set the background:
vtkSmartPointer imgstenc =
vtkSmartPointer::New();
#if VTK_MAJOR_VERSION <= 5
imgstenc->SetInput(whiteImage);
imgstenc->SetStencil(pol2stenc->GetOutput());
#else
imgstenc->SetInputData(whiteImage);
imgstenc->SetStencilConnection(pol2stenc->GetOutputPort());
#endif
imgstenc->ReverseStencilOff();
imgstenc->SetBackgroundValue(outval);
imgstenc->Update();
// vtkSmartPointer writer =
// vtkSmartPointer::New();
// writer->SetFileName("SphereVolume.mhd");
// #if VTK_MAJOR_VERSION <= 5
// writer->SetInput(imgstenc->GetOutput());
// #else
// writer->SetInputData(imgstenc->GetOutput());
// #endif
// writer->Write();
//
//流水线
vtkImageGaussianSmooth *gaosi=vtkImageGaussianSmooth::New();
//高斯平滑处理
gaosi->SetInputData(imgstenc->GetOutput());
gaosi->SetDimensionality(3);
gaosi->SetRadiusFactors(2,2,1);
gaosi->Update();
vtkImageCast *cast=vtkImageCast::New();
//转换成vtk数据格式
cast->SetInputConnection(gaosi->GetOutputPort());
cast->SetOutputScalarTypeToUnsignedChar();;
cast->ClampOverflowOn();
cast->Update();
vtkOpenGLVolumeTextureMapper3D *glmapper=vtkOpenGLVolumeTextureMapper3D::New();
//3D纹理映射法
glmapper->SetInputConnection(cast->GetOutputPort());
vtkPiecewiseFunction * psfunction=vtkPiecewiseFunction::New();
//透明度传递函数
psfunction->AddPoint( 0.0 , 0.0);
psfunction->AddPoint( 40.0 , 0.0);
psfunction->AddPoint( 50.0 , 0.3);
psfunction->AddPoint( 110.0, 0.4);
psfunction->AddPoint( 120, 0.5);
psfunction->AddPoint( 130, 0.6);
psfunction->AddPoint( 190, 0.8);
psfunction->AddPoint( 255, 1.0);
vtkColorTransferFunction * colfunction=vtkColorTransferFunction::New();
//颜色传递函数
colfunction->AddRGBPoint(0.0,0.5, 0.3, 0.2);
colfunction->AddRGBPoint(50.0, 0.8, 0.5, 0.5);
colfunction->AddRGBPoint(110.0, 0.6, 0.2, 0.3);
colfunction->AddRGBPoint(190.0, 0.81, 0.27, 0.1);
colfunction->AddRGBPoint(255.0, 0.5, 0.9, 0.9);
vtkVolumeProperty * volpro=vtkVolumeProperty::New();
//体数据属性
volpro->SetColor(colfunction);
volpro->SetScalarOpacity(psfunction);
volpro->ShadeOn();
volpro->SetInterpolationTypeToLinear();
volpro->SetAmbient(0.2);
volpro->SetDiffuse(0.9);
volpro->SetSpecular(0.2);
volpro->SetSpecularPower(10);
vtkVolume * data=vtkVolume::New();
//体数据映射器和属性
data->SetMapper(glmapper);
data->SetProperty(volpro);
vtkRenderer *render=vtkRenderer::New();
vtkRenderWindow *renwin=vtkRenderWindow::New();
vtkRenderWindowInteractor *iren=vtkRenderWindowInteractor::New();
renwin->AddRenderer(render);
renwin->SetInteractor(iren);
//绘制器
render->AddVolume(data);
render->SetBackground(0,0,0);
render->ResetCameraClippingRange();
//绘制窗口
renwin->AddRenderer(render);
renwin->Render();
// vtkMetaImageWriter* writer = vtkMetaImageWriter::New();
// writer->SetInputData(resultImage);
//writer->SetFileName("E:\\PolyDataToImageStencil\\bin2\\Debug\\polyDataToImage_liver.mha");
// writer->SetFileName("D:\\liver.mha");
// writer->Write();
}