1.彩色图像RGB成分提取
VTK中利用vtkImageExtractComponents可以方便的提取出彩色图像的各个颜色分量,该类的使用比较简单,只需要设置要提取的分量序号即可。下面代码说明了怎样提取彩色图像的某个颜色分量。
1 #include <vtkAutoInit.h>
2 VTK_MODULE_INIT(vtkRenderingOpenGL);
3
4 #include <vtkSmartPointer.h>
5 #include <vtkBMPReader.h>
6 #include <vtkImageExtractComponents.h>
7 #include <vtkImageActor.h>
8 #include <vtkRenderer.h>
9 #include <vtkRenderWindow.h>
10 #include <vtkRenderWindowInteractor.h>
11 #include <vtkInteractorStyleImage.h>
12
13 int main()
14 {
15 vtkSmartPointer<vtkBMPReader> reader =
16 vtkSmartPointer<vtkBMPReader>::New();
17 reader->SetFileName("lena.bmp");
18
19 vtkSmartPointer<vtkImageExtractComponents> redComponent =
20 vtkSmartPointer<vtkImageExtractComponents>::New();
21 redComponent->SetInputConnection(reader->GetOutputPort());
22 redComponent->SetComponents(0);
23 redComponent->Update();
24
25 vtkSmartPointer<vtkImageExtractComponents> greenComponent =
26 vtkSmartPointer<vtkImageExtractComponents>::New();
27 greenComponent->SetInputConnection(reader->GetOutputPort());
28 greenComponent->SetComponents(1);
29 greenComponent->Update();
30
31 vtkSmartPointer<vtkImageExtractComponents> blueComponent =
32 vtkSmartPointer<vtkImageExtractComponents>::New();
33 blueComponent->SetInputConnection(reader->GetOutputPort());
34 blueComponent->SetComponents(2);
35 blueComponent->Update();
36 //创建演员
37 vtkSmartPointer<vtkImageActor> origActor =
38 vtkSmartPointer<vtkImageActor>::New();
39 origActor->SetInputData(reader->GetOutput());
40
41 vtkSmartPointer<vtkImageActor> redActor =
42 vtkSmartPointer<vtkImageActor>::New();
43 redActor->SetInputData(redComponent->GetOutput());
44
45 vtkSmartPointer<vtkImageActor> greenActor =
46 vtkSmartPointer<vtkImageActor>::New();
47 greenActor->SetInputData(greenComponent->GetOutput());
48
49 vtkSmartPointer<vtkImageActor> blueActor =
50 vtkSmartPointer<vtkImageActor>::New();
51 blueActor->SetInputData(blueComponent->GetOutput());
52 //定义窗口 并化妆
53 double origView[4] = { 0.0, 0.0, 0.25, 1.0 };
54 double redView[4] = { 0.25, 0.0, 0.5, 1.0 };
55 double greenView[4] = { 0.5, 0.0, 0.75, 1.0 };
56 double blueView[4] = { 0.75, 0.0, 1.0, 1.0 };
57
58 vtkSmartPointer<vtkRenderer> origRender =
59 vtkSmartPointer<vtkRenderer>::New();
60 origRender->SetViewport(origView);
61 origRender->AddActor(origActor);
62 origRender->ResetCamera();
63 origRender->SetBackground(1.0, 1.0, 1.0);
64
65 vtkSmartPointer<vtkRenderer> redRender =
66 vtkSmartPointer<vtkRenderer>::New();
67 redRender->SetViewport(redView);
68 redRender->AddActor(redActor);
69 redRender->ResetCamera();
70 redRender->SetBackground(1.0, 1.0, 1.0);
71
72 vtkSmartPointer<vtkRenderer> greenRender =
73 vtkSmartPointer<vtkRenderer>::New();
74 greenRender->SetViewport(greenView);
75 greenRender->AddActor(greenActor);
76 greenRender->ResetCamera();
77 greenRender->SetBackground(1.0, 1.0, 1.0);
78
79 vtkSmartPointer<vtkRenderer> blueRender =
80 vtkSmartPointer<vtkRenderer>::New();
81 blueRender->SetViewport(blueView);
82 blueRender->AddActor(blueActor);
83 blueRender->ResetCamera();
84 blueRender->SetBackground(1.0, 1.0, 1.0);
85 //上舞台
86 vtkSmartPointer<vtkRenderWindow> renderwindow =
87 vtkSmartPointer<vtkRenderWindow>::New();
88 renderwindow->AddRenderer(origRender);
89 renderwindow->AddRenderer(redRender);
90 renderwindow->AddRenderer(greenRender);
91 renderwindow->AddRenderer(blueRender);
92 renderwindow->SetSize(960, 320);
93 renderwindow->Render();
94 renderwindow->SetWindowName("ExtraImageColorComponents");
95 //设置交互
96 vtkSmartPointer<vtkRenderWindowInteractor> rwi =
97 vtkSmartPointer<vtkRenderWindowInteractor>::New();
98 vtkSmartPointer<vtkInteractorStyleImage> style =
99 vtkSmartPointer<vtkInteractorStyleImage>::New();
100 rwi->SetInteractorStyle(style);
101 rwi->SetRenderWindow(renderwindow);
102 rwi->Initialize();
103
104 rwi->Start();
105 return 0;
106 }