VTK6.1.0 闲来无事,敲段小代码
#include "stdafx.h"
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);
#include <vtkRenderer.h>
#include <vtkJPEGReader.h>
#include <vtkLookupTable.h>
#include <vtkSmartPointer.h>
#include <vtkImageViewer2.h>
#include <vtkRenderWindow.h>
#include <vtkImageLuminance.h>
#include <vtkImageMapToColors.h>
#include <vtkRenderWindowInteractor.h>
int _tmain(int argc, _TCHAR* argv[])
{
vtkSmartPointer<vtkJPEGReader> jpegReader = vtkSmartPointer<vtkJPEGReader>::New();
jpegReader->SetFileName("E:\\workspace\\001.jpg");
// 将彩色图转换为灰度图
vtkSmartPointer<vtkImageLuminance> lumFilter = vtkSmartPointer<vtkImageLuminance>::New();
lumFilter->SetInputConnection(jpegReader->GetOutputPort());
lumFilter->Update();
// 图像彩色映射
vtkSmartPointer<vtkLookupTable> colorTable = vtkSmartPointer<vtkLookupTable>::New();
colorTable->SetRange(.0, 255.0);// 设置要映射的标量数据的范围
colorTable->SetHueRange(.3, .7); // 设置HSV颜色空间的hue值范围, 最大范围为[0, 1]
colorTable->SetValueRange(.5, 1.0); // 设置HSV中Value值的范围,最大范围为[0, 1]
colorTable->Build(); // 生成颜色查找表
vtkSmartPointer<vtkImageMapToColors> colorMap = vtkSmartPointer<vtkImageMapToColors>::New();
colorMap->SetInputConnection(lumFilter->GetOutputPort());
colorMap->SetLookupTable(colorTable);
colorMap->Update();
vtkSmartPointer<vtkImageViewer2> imgView = vtkSmartPointer<vtkImageViewer2>::New();
imgView->SetInputConnection(jpegReader->GetOutputPort());
vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
imgView->SetupInteractor(iren);
imgView->Render();
imgView->GetRenderer()->ResetCamera();
imgView->GetRenderWindow()->SetSize(800, 800);
imgView->Render();
iren->Start();
return 0;
}
原图:
灰度化:
再映射到彩色中: