#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkDICOMImageReader.h"
#include "vtkBMPReader.h"
#include "vtkJPEGReader.h"
#include "vtkMarchingCubes.h"
#include "vtkStripper.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkProperty.h"
#include "vtkCamera.h"
void main()
{
vtkRenderer *ren = vtkRenderer::New();//设置绘制者(绘制对象指针)
vtkRenderWindow *renWin = vtkRenderWindow::New();//设置绘制窗口
renWin->AddRenderer(ren);//将绘制者加入绘制窗口
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();//设置绘制交互操作窗口的
iren->SetRenderWindow(renWin);//将绘制窗口添加到交互窗口
vtkInteractorStyleTrackballCamera *style = vtkInteractorStyleTrackballCamera::New();//交互摄像机
iren->SetInteractorStyle(style);//style为交互模式
//读取图像序列
//Dicom文件
/* vtkDICOMImageReader *Reader=vtkDICOMImageReader::New();
Reader->SetDirectoryName("D:\\Head-Dicom");
// Reader->SetDataByteOrderToLittleEndian();
Reader->SetDataScalarTypeToUnsignedChar();
Reader->Update();*/
//bmp文件
/* vtkBMPReader *Reader = vtkBMPReader::New();
Reader->SetDataScalarTypeToUnsignedChar();
Reader->SetFilePrefix ("D://bmp/"); //设置所读取数据文件的路径
Reader->SetFilePattern("%s%d.bmp"); //设置图片数据名称格式
Reader->SetDataSpacing(0.450098, 0.450098, 3.0); //设置像素之间和帧图像之间的间距
Reader->SetDataExtent(0, 511, 0, 511, 1, 55); //BMP图像大小为512*512其中图像有55张
Reader->Update();*/
//JPEG图片
vtkJPEGReader *Reader = vtkJPEGReader::New();
Reader->SetDataScalarTypeToUnsignedChar();
// imageReader->SetDataScalarType(VTK_UNSIGNED_CHAR); //将数据转换为unsigned char型
// vtk只支持unsigned char型和 short型体绘制
// Reader->SetFileDimensionality(3);
Reader->SetFilePrefix("D://jpg1/"); //设置所读取数据文件的路径
Reader->SetFileNameSliceSpacing(1);
Reader->SetFilePattern("%s%d.jpg"); //设定图片数据文件名的格式,.jpg是后缀
Reader->SetDataExtent( 0, 255, 0, 255, 280, 300 ); //图像大小为512*512
Reader->SetDataSpacing( 0.5 ,0.5 , 4.0 );//设置像素之间和帧图像之间的间距
Reader->SetDataOrigin( 0.0, 0.0, 0.0 );
// reader->Allow8BitBMPOn();
Reader->Update();
// 从图像数据中提取出皮肤-----------------------------------------------------------------------------
vtkMarchingCubes *skinExtractor = vtkMarchingCubes::New(); //建立一个Marching Cubes 算法的对象
skinExtractor->SetInput((vtkDataSet *)Reader->GetOutput()); //获得所读取的数据
// skinExtractor->SetInputConnection(Reader->GetOutputPort());
skinExtractor->SetValue(0, 10); //提取出灰度值为10的皮肤
vtkStripper *skinStripper = vtkStripper::New(); //建立三角带对象
skinStripper->SetInput( skinExtractor->GetOutput() ); //将生成的三角片连接成三角带
vtkPolyDataMapper *skinMapper = vtkPolyDataMapper::New(); //建立一个数据映射对象
skinMapper->SetInput( skinStripper->GetOutput() ); //将三角带映射为几何数据
skinMapper->ScalarVisibilityOff();
vtkActor *skin = vtkActor::New(); //建立一个代表皮肤的演员
skin->SetMapper( skinMapper ); //获得皮肤几何数据的属性
skin->GetProperty()->SetDiffuseColor(1, .49, .25); //设置皮肤颜色的属性//(1, .49, .25)
// skin->GetProperty()->SetDiffuseColor(1, 1, .25)
skin->GetProperty()->SetSpecular(.3); //设置反射率
skin->GetProperty()->SetSpecularPower(20); //设置反射光强度
skin->GetProperty()->SetOpacity(1.0);
//提取骨骼--------------------------------------------------------------------------------------------
//extract the bone
/* vtkMarchingCubes *boneExtractor = vtkMarchingCubes::New();
boneExtractor->SetInputConnection(Reader->GetOutputPort());
boneExtractor->SetValue(0, 150);//
vtkStripper *boneStripper = vtkStripper::New();
boneStripper->SetInput(boneExtractor->GetOutput());
vtkPolyDataMapper *boneMapper = vtkPolyDataMapper::New();
boneMapper->SetInput(boneStripper->GetOutput());
boneMapper->ScalarVisibilityOff();
vtkActor *bone = vtkActor::New();
bone->SetMapper(boneMapper);
bone->GetProperty()->SetDiffuseColor(1, 1, .9412);*/
vtkCamera *aCamera = vtkCamera::New(); // 创建摄像机
aCamera->SetViewUp ( 0, 0, -1 ); //设置相机的“上”方向
aCamera->SetPosition ( 0, 1, 0 ); //位置:世界坐标系,设置相机位置
aCamera->SetFocalPoint( 0, 0, 0 ); //焦点:世界坐标系,控制相机方向
aCamera->ComputeViewPlaneNormal(); //重置视平面法向,基于当前的位置和焦点。否则会出现斜推剪切效果
// 告诉绘制者将要在绘制窗口中进行显示的演员
ren->AddActor(skin);
ren->SetActiveCamera(aCamera); ////设置渲染器的相机
ren->ResetCamera();
aCamera->Dolly(1.5); //使用Dolly()方法沿着视平面法向移动相机,实现放大或缩小可见角色物体
ren->SetBackground(1,1,1);
ren->ResetCameraClippingRange();
// renWin->SetSize(600, 600);
renWin->Render();
iren->Initialize();
iren->Start();
//清除对象
Reader->Delete();
skinExtractor->Delete();
skinStripper->Delete();
skinMapper->Delete();
skin->Delete();
ren->Delete();
renWin->Delete();
iren->Delete();
style->Delete();
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页