vtk 转换视角_【vtk】数据类型转换vtkpolydata转vtkImagedata

该博客介绍了如何使用VTK库将vtkPolyData类型的3D模型转换为vtkImageData,通过vtkPolyDataToImageStencil实现转换,并展示了如何进行视角转换,包括高斯平滑处理、3D纹理映射、透明度和颜色传递函数的设置,以及最终的渲染和显示。
摘要由CSDN通过智能技术生成

用到的类为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();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值