图像彩色映射的原理是首先生成一个颜色查找表,然后根据图像的一个标量值向颜色查找表中查找对应的颜色,并用新颜色值替代原来的像素值。VTK中vtkImageMapToColors负责图像彩色映射,vtkLookUpTable负责生成颜色查找表。
下面代码说明了怎样进行彩色映射:
1 #include <vtkAutoInit.h>
2 VTK_MODULE_INIT(vtkRenderingOpenGL);
3
4 #include <vtkSmartPointer.h>
5 #include <vtkJPEGReader.h>
6 #include <vtkLookupTable.h>
7 #include <vtkImageMapToColors.h>
8 #include <vtkImageActor.h>
9 #include <vtkRenderer.h>
10 #include <vtkRenderWindow.h>
11 #include <vtkRenderWindowInteractor.h>
12 #include <vtkInteractorStyleImage.h>
13 int main()
14 {
15 vtkSmartPointer<vtkJPEGReader> reader =
16 vtkSmartPointer<vtkJPEGReader>::New();
17 reader->SetFileName("lena-gray.jpg");
18 //建立颜色映射查找表
19 vtkSmartPointer<vtkLookupTable> colorTable =
20 vtkSmartPointer<vtkLookupTable>::New();
21 colorTable->SetRange(0.0, 255.0);
22 colorTable->SetHueRange(0.1, 0.5);
23 colorTable->SetValueRange(0.6, 1.0);
24 colorTable->Build();
25 //建立颜色映射
26 vtkSmartPointer<vtkImageMapToColors> colorMap =
27 vtkSmartPointer<vtkImageMapToColors>::New();
28 colorMap->SetInputConnection(reader->GetOutputPort());
29 colorMap->SetLookupTable(colorTable);
30 colorMap->Update();
31 //建立演员
32 vtkSmartPointer<vtkImageActor> origActor =
33 vtkSmartPointer<vtkImageActor>::New();
34 origActor->SetInputData(reader->GetOutput());
35
36 vtkSmartPointer<vtkImageActor> colorActor =
37 vtkSmartPointer<vtkImageActor>::New();
38 colorActor->SetInputData(colorMap->GetOutput());
39 //化妆
40 double origView[4] = { 0.0, 0.0, 0.5, 1.0 };
41 double colorView[4] = { 0.5, 0.0, 1.0, 1.0 };
42 vtkSmartPointer<vtkRenderer> origRender =
43 vtkSmartPointer<vtkRenderer>::New();
44 origRender->SetViewport(origView);
45 origRender->AddActor(origActor);
46 origRender->ResetCamera();
47 origRender->SetBackground(1.0, 0.0, 0.0);
48
49 vtkSmartPointer<vtkRenderer> colorRender =
50 vtkSmartPointer<vtkRenderer>::New();
51 colorRender->SetViewport(colorView);
52 colorRender->AddActor(colorActor);
53 colorRender->ResetCamera();
54 colorRender->SetBackground(0.0, 0.0, 0.0);
55 //舞台
56 vtkSmartPointer<vtkRenderWindow> renderwindow =
57 vtkSmartPointer<vtkRenderWindow>::New();
58 renderwindow->AddRenderer(origRender);
59 renderwindow->AddRenderer(colorRender);
60 renderwindow->SetSize(640, 320);
61 renderwindow->SetWindowName("GrayToColor");
62 //设置交互
63 vtkSmartPointer<vtkRenderWindowInteractor> rwi =
64 vtkSmartPointer<vtkRenderWindowInteractor>::New();
65 vtkSmartPointer<vtkInteractorStyleImage> style =
66 vtkSmartPointer<vtkInteractorStyleImage>::New();
67 rwi->SetInteractorStyle(style);
68 rwi->SetRenderWindow(renderwindow);
69 rwi->Initialize();
70
71 rwi->Start();
72 return 0;
73 }
输出图像: