读和写操作是VTK可视化管线两端相关的类--Reader和Writer类
Reader:将外部数据读入可视化管线,主要步骤如下
s1:实例化Reader对象
s2:指定所要读取的文件名
s3:调用Update()促使管线执行
对应的Writer:
s1:实例化Writer对象
s2输入要写的数据以及指定写入的文件名
s3调用Write()方法促使Write类进行写入操作
根据不同的数据类型衍生出不同类型的Reader和Writer类型
读取一张png和保存为一张jpg图像如下所示
#include <vtkSmartPointer.h>
#include <vtkPNGReader.h>
#include <vtkImageViewer2.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkJPEGWriter.h>
#include <vtkRenderWindow.h>int main(int, char *[])
{// 读取PNG图像vtkSmartPointer<vtkPNGReader> reader = vtkSmartPointer<vtkPNGReader>::New();reader->SetFileName("../1.png");// 显示读取的单幅PNG图像vtkSmartPointer<vtkImageViewer2> imageViewer = vtkSmartPointer<vtkImageViewer2>::New();imageViewer->SetInputConnection(reader->GetOutputPort());imageViewer->SetupInteractor(vtkSmartPointer<vtkRenderWindowInteractor>::New());imageViewer->Render();imageViewer->ResetCamera();imageViewer->Render();// 保存成JPG图像vtkSmartPointer<vtkJPEGWriter> writer = vtkSmartPointer<vtkJPEGWriter>::New();writer->setFileName("../1.jpg");writer->SetInputConnection(reader->GetOutputPort());writer->Write();// 通常不需要调用Start()方法,因为SetupInteractor已经创建并启动了RenderWindowInteractor// renderWindowInteractor->Start();return EXIT_SUCCESS;
}