2.参看资料图像彩色映射的原理是首先生成一个颜色查找表,然后根据图像的一个标量值向颜色查找表中查找对应的颜色,并用新颜色值替代原来的像素值。VTK中vtkImageMapToColors负责图像彩色映射,vtkLookUpTable负责生成颜色查找表。
下面代码说明了怎样进行彩色映射:
- #include <vtkAutoInit.h>
- VTK_MODULE_INIT(vtkRenderingOpenGL);
- #include <vtkSmartPointer.h>
- #include <vtkJPEGReader.h>
- #include <vtkLookupTable.h>
- #include <vtkImageMapToColors.h>
- #include <vtkImageActor.h>
- #include <vtkRenderer.h>
- #include <vtkRenderWindow.h>
- #include <vtkRenderWindowInteractor.h>
- #include <vtkInteractorStyleImage.h>
- int main()
- {
- vtkSmartPointer<vtkJPEGReader> reader =
- vtkSmartPointer<vtkJPEGReader>::New();
- reader->SetFileName("lena-gray.jpg");
- //建立颜色映射查找表
- vtkSmartPointer<vtkLookupTable> colorTable =
- vtkSmartPointer<vtkLookupTable>::New();
- colorTable->SetRange(0.0, 255.0);
- colorTable->SetHueRange(0.1, 0.5);
- colorTable->SetValueRange(0.6, 1.0);
- colorTable->Build();
- //建立颜色映射
- vtkSmartPointer<vtkImageMapToColors> colorMap =
- vtkSmartPointer<vtkImageMapToColors>::New();
- colorMap->SetInputConnection(reader->GetOutputPort());
- colorMap->SetLookupTable(colorTable);
- colorMap->Update();
- //建立演员
- vtkSmartPointer<vtkImageActor> origActor =
- vtkSmartPointer<vtkImageActor>::New();
- origActor->SetInputData(reader->GetOutput());
- vtkSmartPointer<vtkImageActor> colorActor =
- vtkSmartPointer<vtkImageActor>::New();
- colorActor->SetInputData(colorMap->GetOutput());
- //化妆
- double origView[4] = { 0.0, 0.0, 0.5, 1.0 };
- double colorView[4] = { 0.5, 0.0, 1.0, 1.0 };
- vtkSmartPointer<vtkRenderer> origRender =
- vtkSmartPointer<vtkRenderer>::New();
- origRender->SetViewport(origView);
- origRender->AddActor(origActor);
- origRender->ResetCamera();
- origRender->SetBackground(1.0, 0.0, 0.0);
- vtkSmartPointer<vtkRenderer> colorRender =
- vtkSmartPointer<vtkRenderer>::New();
- colorRender->SetViewport(colorView);
- colorRender->AddActor(colorActor);
- colorRender->ResetCamera();
- colorRender->SetBackground(0.0, 0.0, 0.0);
- //舞台
- vtkSmartPointer<vtkRenderWindow> renderwindow =
- vtkSmartPointer<vtkRenderWindow>::New();
- renderwindow->AddRenderer(origRender);
- renderwindow->AddRenderer(colorRender);
- renderwindow->SetSize(640, 320);
- renderwindow->SetWindowName("GrayToColor");
- //设置交互
- vtkSmartPointer<vtkRenderWindowInteractor> rwi =
- vtkSmartPointer<vtkRenderWindowInteractor>::New();
- vtkSmartPointer<vtkInteractorStyleImage> style =
- vtkSmartPointer<vtkInteractorStyleImage>::New();
- rwi->SetInteractorStyle(style);
- rwi->SetRenderWindow(renderwindow);
- rwi->Initialize();
- rwi->Start();
- return 0;
- }
输出图像:
1.《C++ primer》
2.《The VTK User’s Guide – 11thEdition》
3.《The Visualization Toolkit – AnObject-Oriented Approach To 3D Graphics (4th Edition)》
4. 张晓东, 罗火灵. VTK图形图像开发进阶[M]. 机械工业出版社, 2015.