VTK图像显示

总结:本博文是vtkImageViewer2和vtkImageActor用法及程序实现结果,属于学习笔记
#include"vtkSmartPointer.h"
#include"vtkMetaImageReader.h"
#include"vtkImageViewer2.h"
#include"vtkRenderer.h"
#include"vtkRenderWindow.h"
#include"vtkRenderWindowInteractor.h"
//vtkImageViewer2封装了vtk图像显示的可视化渲染引擎
int main(int argc,char* argv[]){
	vtkSmartPointer reader = vtkSmartPointer::New();
	reader->SetFileName("head.mhd");
	reader->Update();
	vtkSmartPointer viewer = vtkSmartPointer::New();
	viewer->SetInputConnection(reader->GetOutputPort());
	//设置基本属性
	viewer->SetSize(640,480);
	viewer->SetColorLevel(500);  //设置窗宽
	viewer->SetColorWindow(2000);  //窗维
	viewer->SetSlice(20);        //切片索引
	viewer->SetSliceOrientationToXY(); //切片的方向设置为XY平面方向
	viewer->Render();
	viewer->GetRenderer()->SetBackground(1, 1, 1);
	viewer->GetRenderWindow()->SetWindowName("Image view 2D");

	vtkSmartPointer rwi = vtkSmartPointer::New();
	//设置交互
	viewer->SetupInteractor(rwi);
	rwi->Start();
	
	return 0;
}


运行结果:

VTK图像显示_第1张图片

//vtkImageActor用法及显示
#include"vtkSmartPointer.h"
#include"vtkBMPReader.h"
#include"vtkImageActor.h"
#include"vtkRenderer.h"
#include"vtkRenderWindow.h"
#include"vtkRenderWindowInteractor.h"
#include"vtkInteractorStyleImage.h"
//读入图像后,依次建立vtkImageActor,vtkRender,vtkRenderWindow,vtkRenderWindowInteractor,并组装为管线。
//为了屏蔽旋转操作,建立vtkInteractorStyleImage对象,并通过rwi->SetInteractorStyle(style)设置交互对象。
int main(){
	vtkSmartPointer reader = vtkSmartPointer::New();
	reader->SetFileName("test.bmp");
	reader->Update();
	vtkSmartPointer actor = vtkSmartPointer::New();
	actor->SetInputData(reader->GetOutput());
	
	vtkSmartPointer render = vtkSmartPointer::New();
	render->AddActor2D(actor);
	render->ResetCamera();
	render->SetBackground(1, 1, 1);
	vtkSmartPointer window = vtkSmartPointer::New();
	window->AddRenderer(render);
	window->SetSize(640, 480);
	window->SetWindowName("Imageviewer3D");
	window->Render();   //调用的是vtkrenderwindow的render函数
	vtkSmartPointer rwi = vtkSmartPointer::New();
	vtkSmartPointer style = vtkSmartPointer::New();
	rwi->SetInteractorStyle(style);
	rwi->SetRenderWindow(window);
	rwi->Initialize();
	rwi->Start();

	return 0;
}

//需要注意的是,vtkImageActor接收的图像vtkImageData数据类型必须为unsigned char类型,因此在显示之前,
//必要的时候需要利用vtkImageCast将图像数据类型转换为unsigned char。

运行结果:

VTK图像显示_第2张图片


参考链接:

https://blog.csdn.net/www_doling_net/article/details/8541534

https://blog.csdn.net/shenziheng1/article/details/54565337

你可能感兴趣的:(VTK)