VTK读取并显示一张图片的两种方法,最后写成一张图片

#include 
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkRenderingFreeType)
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2)


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
//测试文件:data/VTK-logo.bmp

#pragma comment(lib, "vtkRenderingCore-7.1.lib")
#pragma comment(lib, "vtkInteractionStyle-7.1.lib")
#pragma comment(lib, "vtkCommonCore-7.1.lib")
#pragma comment(lib, "vtkCommonExecutionModel-7.1.lib")
#pragma comment(lib, "vtkRenderingFreeType-7.1.lib")
#pragma comment(lib, "vtkRenderingOpenGL2-7.1.lib")
#pragma comment(lib, "vtkRenderingVolume-7.1.lib")
#pragma comment(lib, "vtkRenderingVolumeOpenGL2-7.1.lib")
#pragma comment(lib, "vtkIOImage-7.1.lib")
#pragma comment(lib, "vtkInteractionImage-7.1.lib")
#pragma comment(lib, "vtkImagingCore-7.1.lib")

int main(int argc, char* argv[])
{
    vtkSmartPointer reader = vtkSmartPointer::New();
    reader->SetFileName("E:\\vtk\\VTKData\\Data\\vtk.png");

    /* 显示一张图像的第一种方法
    vtkSmartPointer imageViewer = vtkSmartPointer::New();
    imageViewer->SetInputConnection(reader->GetOutputPort());
    vtkSmartPointer renderWindowInteractor = vtkSmartPointer::New();
    imageViewer->SetupInteractor(renderWindowInteractor);
    imageViewer->Render();
    imageViewer->GetRenderer()->ResetCamera();
    imageViewer->Render();
    renderWindowInteractor

    */
    /*显示一张图像的第二种方法
    vtkSmartPointer mapper = vtkSmartPointer::New();
    mapper->SetInputConnection(reader->GetOutputPort());
    mapper->SetColorLevel(128); mapper->SetColorWindow(256);
    vtkSmartPointer imgActor = vtkSmartPointer::New();
    imgActor->SetMapper(mapper);

    vtkSmartPointer renderer = vtkSmartPointer::New();
    renderer->AddActor2D(imgActor);
    renderer->SetBackground(0.1, 0.1, 0.1);
    

    vtkSmartPointer renWin = vtkSmartPointer::New();
    renWin->AddRenderer(renderer);
    vtkSmartPointer iren = vtkSmartPointer::New();
    iren->SetRenderWindow(renWin);
    vtkSmartPointer style = vtkSmartPointer::New();
    iren->SetInteractorStyle(style);
    iren->Initialize();
    iren->Start();
    */
    /*此方法行不通
    vtkSmartPointer actor = vtkSmartPointer::New();
    actor->SetInputData(reader->GetOutput());
    vtkSmartPointer renderer = vtkRenderer::New();
    renderer->AddActor(actor);
    
    vtkSmartPointer renWin = vtkSmartPointer::New();
    renWin->AddRenderer(renderer);
    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
    iren->SetRenderWindow(renWin);
    vtkInteractorStyleImage *interactor = vtkInteractorStyleImage::New();
    iren->SetInteractorStyle(interactor);
    renderer->SetBackground(0.1, 0.2, 0.4);
    renWin->Render();
    iren->Initialize();
    iren->Start();
    */
    //保存成png
    vtkSmartPointer writer = vtkSmartPointer::New();
    writer->SetFileName("E:\\vtk\\VTKData\\Data\\vtk.jpg");
    writer->SetInputConnection(reader->GetOutputPort());
    writer->Write();

    

    return 0;
}

 

你可能感兴趣的:(vtk学习)