一、vtk中的vtkImageData
VTK提供相对应的类对图像文件进行读写操作
测试下效果:
int main()
{
//vtkSmartPointer<vtkBMPReader> reader = vtkSmartPointer<vtkBMPReader>::New();
//reader->SetFileName("**\\12.bmp");
vtkSmartPointer<vtkPNGReader> reader = vtkSmartPointer<vtkPNGReader>::New();
reader->SetFileName("**\\123.png");
reader->Update();
//注意vtkImageActor接收到的图像vtkImageData必须是unsigned char类型的
vtkSmartPointer<vtkImageActor> imageActor = vtkSmartPointer<vtkImageActor>::New();
imageActor->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkRenderer> render = vtkSmartPointer<vtkRenderer>::New();
render->AddActor(imageActor);
render->ResetCamera();
render->SetBackground(0, 0, 0);
vtkSmartPointer<vtkRenderWindow> window = vtkSmartPointer<vtkRenderWindow>::New();
window->AddRenderer(render);
window->SetSize(640, 480);
window->SetWindowName("3DViewer");
window->Render();
vtkSmartPointer<vtkRenderWindowInteractor> interactor =vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style = vtkSmartPointer<vtkInteractorStyleImage>::New();
interactor->SetInteractorStyle(style);
interactor->SetRenderWindow(window);
interactor->Initialize();
interactor->Start();
return 0;
}
看下效果:
可以实现鼠标左键按下,移动,鼠标中键移动效果,鼠标右键放大缩小
二、vtkImageViewer2
vtkImageViewer2是一个方便易用的2D图像显示类,它封装了vtkRenderWindow, vtkRender, vtkImageActor和vtkImageMapToWinowLevelColors。同时,包含了一个vtkInteractorStyleImage的交互方式,使得显示和交互变得更加简单。具体使用如下:
vtkSmartPointer<vtkImageViewer2> viewer2 = vtkSmartPointer<vtkImageViewer2>::New();
viewer2->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer<vtkRenderWindowInteractor> interactor =vtkSmartPointer<vtkRenderWindowInteractor>::New();
//默认选择第50张切片
viewer2->SetSlice(50);
//显示方向
viewer2->SetSliceOrientationToXY();
//加入交互
viewer2->SetupInteractor(interactor);
viewer2->Render();
interactor->Start();
return 0;