目录
部分项目中需要截取vtk图像进行显示
1.CmakeLists
#1.设置cmake的最小版本
cmake_minimum_required(VERSION 3.3...3.12 FATAL_ERROR)
#2.设置项目名称
project (Step2)
#3.查找vtk
find_package(VTK REQUIRED)
#4.vtk模块的设置
vtk_module_config(VTK
vtkCommonCore
vtkFiltersSources
vtkFiltersSources
vtkInteractionStyle
vtkRenderingOpenGL2
vtkIOImage
)
#5.包含头文件
include(${VTK_USE_FILE})
#6.添加可执行文件
add_executable(CubeAxesActor MACOSX_BUNDLE CubeAxesActor.cxx)
#7.为可执行文件链接库
target_link_libraries(CubeAxesActor ${VTK_LIBRARIES})
2.C++实现部分
#include "vtkActor.h"
#include "vtkCamera.h"
#include "vtkConeSource.h"
#include "vtkCellArray.h"
#include "vtkFloatArray.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include <vtkLookupTable.h>
#include <vtkWindowToImageFilter.h>
#include <vtkPNGWriter.h>
int main()
{
int i;
// 梯形的顶点坐标
static float x[8][3] = {{0, 0, 0}, {4, 0, 0}, {4, 4, 0}, {0, 4, 0}, {1, 1, 1}, {3, 1, 1}, {3, 3, 1}, {1, 3, 1}};
// 4个顶点构成一个单元,一共6个单元
static vtkIdType y[6][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {0, 1, 5, 4}, {1, 2, 6, 5}, {2, 3, 7, 6}, {3, 0, 4, 7}};
vtkPoints *points = vtkPoints::New();
for (i = 0; i < 8; i++)
points->InsertPoint(i, x[i]);
vtkCellArray *polys = vtkCellArray::New();
for (i = 0; i < 6; i++)
polys->InsertNextCell(4, y[i]);
// 存储标量值
vtkFloatArray *scalars = vtkFloatArray::New();
for (i = 0; i < 8; i++)
scalars->InsertTuple1(i, i);
// 构建多边形数据
vtkPolyData *cube = vtkPolyData::New();
cube->SetPoints(points);
// 设定单元的组成方式
cube->SetPolys(polys);
cube->GetPointData()->SetScalars(scalars);
// 定义颜色映射表
vtkLookupTable *pColorTable = vtkLookupTable::New();
pColorTable->SetNumberOfColors(6);
pColorTable->SetTableValue(0, 1.0, 0.0, 1.0, 1.0);
pColorTable->SetTableValue(1, 0.0, 1.0, 1.0, 1.0);
pColorTable->SetTableValue(2, 1.0, 1.0, 1.0, 1.0);
pColorTable->SetTableValue(3, 1.0, 0.0, 1.0, 1.0);
pColorTable->SetTableValue(4, 0.0, 0.0, 1.0, 1.0);
pColorTable->SetTableValue(5, 1.0, 1.0, 0.0, 1.0);
pColorTable->Build();
/*数据映射*/
vtkPolyDataMapper *cubeMapper = vtkPolyDataMapper::New();
cubeMapper->SetInputData(cube);
cubeMapper->SetScalarRange(0, 7);
cubeMapper->SetLookupTable(pColorTable);
vtkActor *cubeActor = vtkActor::New();//创建actor对象
cubeActor->SetMapper(cubeMapper);
vtkCamera *camera = vtkCamera::New();//vktcamera设置
camera->SetPosition(1, 1, 1); //相机位置设置
camera->SetFocalPoint(0, 0, 0);//相机焦点设置
vtkRenderer *renderer = vtkRenderer::New();//创建vtkRenderer对象
vtkRenderWindow *renWin = vtkRenderWindow::New();//创建vtkRenderer窗口对象
renWin->AddRenderer(renderer);//将vtkRenderer与vtkRenderer窗口关联
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();//创建interactor对象
iren->SetRenderWindow(renWin);//设置interactor的的renderwindow
renderer->AddActor(cubeActor);//将actor加入到render中
renderer->SetActiveCamera(camera);//renderer设置的的选择camera
renderer->ResetCamera();//renderer相机重置
renderer->SetBackground(1, 1, 1);//设置渲染的背景颜色
renWin->SetSize(800, 800);//设置渲染窗口大小
renWin->Render();
/* vtkWindowToImageFilter屏幕截图*/
vtkNew<vtkWindowToImageFilter> windowToImageFilter;//vtkWindowToImageFilter获取vtk窗体的图像
windowToImageFilter->SetInput(renWin); // 设定输入窗口
windowToImageFilter->SetInputBufferTypeToRGBA(); // 设置格式类型
#if VTK_MAJOR_VERSION >= 8 || VTK_MAJOR_VERSION == 8 && VTK_MINOR_VERSION >= 90
windowToImageFilter->SetScale(1);
#else
windowToImageFilter->SetMagnification(2);
#endif
windowToImageFilter->SetInputBufferTypeToRGB(); // 设置图像存储图像格式
windowToImageFilter->ReadFrontBufferOff();
windowToImageFilter->Update();
/* vtkPNGWriter保存屏幕截取图像*/
vtkNew<vtkPNGWriter> writer;
writer->SetFileName("screenshot2.png");
writer->SetInputConnection(windowToImageFilter->GetOutputPort());
writer->Write();
iren->Start(); //interactor如果开启无法截图
// 删除
points->Delete();
polys->Delete();
scalars->Delete();
cube->Delete();
cubeMapper->Delete();
cubeActor->Delete();
camera->Delete();
renderer->Delete();
renWin->Delete();
iren->Delete();
pColorTable->Delete();
return 0;
}
标签:1.0,pColorTable,vtk,windowToImageFilter,图像,New,include,截取,Delete
From: https://www.cnblogs.com/codeAndlearn/p/17568844.html