VTK:读取raw图片格式进行体绘制

本文介绍了一个使用VTK库实现三维体绘制的例子。通过设置不同的颜色和不透明度传递函数,实现对三维数据的不同可视化效果。代码展示了从读取原始数据到最终绘制的完整流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include "vtkDICOMImageReader.h"
#include "vtkPiecewiseFunction.h"
#include "vtkColorTransferFunction.h"
#include "vtkVolumeProperty.h"
#include "vtkVolumeRayCastCompositeFunction.h"
#include "vtkVolumeRayCastMapper.h"
#include "vtkVolume.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkImageCast.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkBMPReader.h"
#include "vtkVolume16Reader.h"
#include "vtkPNGReader.h"
#include "vtkJPEGReader.h"
#include "vtkRayCastImageDisplayHelper.h"
#include <vtkSmartPointer.h>
void main()

{
	vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New();//设置绘制者(绘制对象指针)
	vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();//设置绘制窗口
	renWin->AddRenderer(ren);//将绘制者加入绘制窗口
	vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();//设置绘制交互操作窗口的
	iren->SetRenderWindow(renWin);//将绘制窗口添加到交互窗口

	vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();//交互摄像机
	iren->SetInteractorStyle(style);//style为交互模式
	vtkSmartPointer<vtkImageReader> reader = vtkSmartPointer<vtkImageReader>::New();

	reader->SetFileName("C:\\Users\\HuangWang\\Desktop\\Head_256x256x256.raw");
	reader->SetFileDimensionality(3);//设置显示图像的维数
	reader->SetDataScalarType(VTK_UNSIGNED_CHAR);//VTK_UNSIGNED_short将数据转换为unsigned char型
	reader->SetDataExtent(0, 255, 0, 255, 0, 255);
	reader->SetDataSpacing(0.9, 0.9, 0.9); //设置像素间间距
	reader->SetDataOrigin(0.0, 0.0, 0.0);//设置基准点,(一般没有用)做虚拟切片时可能会用的上
	reader->Update();

	vtkSmartPointer<vtkImageCast> readerImageCast = vtkSmartPointer<vtkImageCast>::New();//数据类型转换
	readerImageCast->SetInputConnection(reader->GetOutputPort());
	readerImageCast->SetOutputScalarTypeToUnsignedChar();
	readerImageCast->ClampOverflowOn();//阀值
	//reader->Delete();
	
	//设置不透明度传递函数//该函数确定各体绘像素或单位长度值的不透明度
	vtkSmartPointer<vtkPiecewiseFunction> opacityTransferFunction = vtkSmartPointer<vtkPiecewiseFunction>::New();//一维分段函数变换
	opacityTransferFunction->AddPoint(20, 0.0);
	opacityTransferFunction->AddPoint(255, 0.2);

	//设置颜色传递函数//该函数确定体绘像素的颜色值或者灰度值
	vtkSmartPointer<vtkColorTransferFunction> colorTransferFunction = vtkSmartPointer<vtkColorTransferFunction>::New();
	colorTransferFunction->AddRGBPoint(0.0, 0.0, 0.5, 0.0);//添加色彩点(第一个参数索引)
	colorTransferFunction->AddRGBPoint(60.0, 1.0, 0.0, 0.0);
	colorTransferFunction->AddRGBPoint(128.0, 0.2, 0.1, 0.9);
	colorTransferFunction->AddRGBPoint(196.0, 0.27, 0.21, 0.1);
	colorTransferFunction->AddRGBPoint(255.0, 0.8, 0.8, 0.8);

	vtkSmartPointer<vtkVolumeProperty> volumeProperty = vtkSmartPointer<vtkVolumeProperty>::New();
	//设定一个体绘容器的属性
	volumeProperty->SetColor(colorTransferFunction);//设置颜色
	volumeProperty->SetScalarOpacity(opacityTransferFunction);//不透明度
	volumeProperty->ShadeOn();//影阴
	volumeProperty->SetInterpolationTypeToLinear();//直线与样条插值之间逐发函数
	volumeProperty->SetAmbient(0.2);//环境光系数
	volumeProperty->SetDiffuse(0.9);//漫反射
	volumeProperty->SetSpecular(0.2);//高光系数
	volumeProperty->SetSpecularPower(10); //高光强度 

	//定义光线投射方法为MIP体绘制方法,MIP为体绘制经典算法
	// vtkVolumeRayCastMIPFunction*mipRaycastFunction = vtkVolumeRayCastMIPFunction::New();
	// mipRaycastFunction->SetMaximizeMethodToOpacity();
	vtkSmartPointer<vtkVolumeRayCastCompositeFunction> compositeFunction = vtkSmartPointer<vtkVolumeRayCastCompositeFunction>::New();
	//运行沿着光线合成
	//定义绘制者
	vtkVolumeRayCastMapper *volumeMapper = vtkVolumeRayCastMapper::New(); //体绘制器
	volumeMapper->SetVolumeRayCastFunction(compositeFunction); //载入绘制方法
	volumeMapper->SetInputConnection(readerImageCast->GetOutputPort());//图像数据输入
	volumeMapper->SetNumberOfThreads(3);
	//定义Volume
	vtkSmartPointer<vtkVolume> volume = vtkSmartPointer<vtkVolume>::New();//表示透示图中的一组三维数据
	volume->SetMapper(volumeMapper);
	volume->SetProperty(volumeProperty);//设置体属性
	
	ren->AddVolume(volume);//将Volume装载到绘制类中
	ren->SetBackground(1, 1, 1);
	renWin->SetSize(600, 600);//设置背景颜色和绘制窗口大小
	renWin->Render();窗口进行绘制
	iren->Initialize();
	iren->Start();//初始化并进行交互绘制
	ren->ResetCameraClippingRange();
}

 

如果您觉得这篇博文有用,请访问我的个人站:http://www.stubbornhuang.com/,更多博文干货等着您。

评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HW140701

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值