1 #include <vtkAutoInit.h>
2 VTK_MODULE_INIT(vtkRenderingOpenGL);
3
4 #include <vtkSmartPointer.h>
5 #include <vtkBMPReader.h>
6 #include <vtkImageLuminance.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<vtkImageLuminance> luminanceFilter =
20 vtkSmartPointer<vtkImageLuminance>::New();
21 luminanceFilter->SetInputConnection(reader->GetOutputPort());
22 luminanceFilter->Update();
23 //原始演员
24 vtkSmartPointer<vtkImageActor> origActor =
25 vtkSmartPointer<vtkImageActor>::New();
26 origActor->SetInputData(reader->GetOutput());
27 //灰度演员
28 vtkSmartPointer<vtkImageActor> shiftscaleActor =
29 vtkSmartPointer<vtkImageActor>::New();
30 shiftscaleActor->SetInputData(luminanceFilter->GetOutput());
31 double origView[4] = { 0.0, 0.0, 0.5, 1.0 };
32 double shiftscaleView[4] = { 0.5, 0.0, 1.0, 1.0 };
33 //原始演员化妆
34 vtkSmartPointer<vtkRenderer> origRender =
35 vtkSmartPointer<vtkRenderer>::New();
36 origRender->SetViewport(origView);
37 origRender->AddActor(origActor);
38 origRender->ResetCamera();
39 origRender->SetBackground(1.0, 1.0, 1.0);
40 //灰度演员化妆
41 vtkSmartPointer<vtkRenderer> shiftscaleRender =
42 vtkSmartPointer<vtkRenderer>::New();
43 shiftscaleRender->SetViewport(shiftscaleView);
44 shiftscaleRender->AddActor(shiftscaleActor);
45 shiftscaleRender->ResetCamera();
46 shiftscaleRender->SetBackground(1.0, 1.0, 1.0);
47
48 vtkSmartPointer<vtkRenderWindow> renderwindow =
49 vtkSmartPointer<vtkRenderWindow>::New();
50 renderwindow->AddRenderer(origRender);
51 renderwindow->AddRenderer(shiftscaleRender);
52 renderwindow->SetSize(720, 480);
53 renderwindow->Render();
54 renderwindow->SetWindowName("RGB to Gray");
55
56 vtkSmartPointer<vtkRenderWindowInteractor> rwi =
57 vtkSmartPointer<vtkRenderWindowInteractor>::New();
58 vtkSmartPointer<vtkInteractorStyleImage> style =
59 vtkSmartPointer<vtkInteractorStyleImage>::New();
60 rwi->SetInteractorStyle(style);
61 rwi->SetRenderWindow(renderwindow);
62 rwi->Initialize();
63
64 rwi->Start();
65 return 0;
66 }