1 #include <vtkAutoInit.h>
2 VTK_MODULE_INIT(vtkRenderingOpenGL);
3
4 #include <vtkSmartPointer.h>
5 #include <vtkBMPReader.h>
6 #include <vtkImageData.h>//提取图像内部信息,如维度等
7 #include <vtkExtractVOI.h>
8 #include <vtkImageActor.h>
9 #include <vtkRenderer.h>
10 #include <vtkRenderWindow.h>
11 #include <vtkRenderWindowInteractor.h>
12 #include <vtkInteractorStyleImage.h>
13
14 int main()
15 {
16 vtkSmartPointer<vtkBMPReader> reader =
17 vtkSmartPointer<vtkBMPReader>::New();
18 reader->SetFileName("lena.bmp");
19 reader->Update();
20
21 int dim[3];
22 reader->GetOutput()->GetDimensions(dim);
23 //提取图像子模块
24 vtkSmartPointer<vtkExtractVOI> extractVOI =
25 vtkSmartPointer<vtkExtractVOI>::New();
26 extractVOI->SetInputConnection(reader->GetOutputPort());
27 extractVOI->SetVOI(dim[0] / 4., 3.*dim[0] / 4., dim[1] / 4., 3.*dim[1] / 4, 0, 0);
28 extractVOI->Update();
29 //创建演员
30 vtkSmartPointer<vtkImageActor> origActor =
31 vtkSmartPointer<vtkImageActor>::New();
32 origActor->SetInputData(reader->GetOutput());
33
34 vtkSmartPointer<vtkImageActor> voiActor =
35 vtkSmartPointer<vtkImageActor>::New();
36 voiActor->SetInputData(extractVOI->GetOutput());
37 //化妆
38 double origView[4] = { 0, 0, 0.5, 1.0 };
39 double voiView[4] = { 0.5, 0, 1.0, 1.0 };
40 vtkSmartPointer<vtkRenderer> origRender =
41 vtkSmartPointer<vtkRenderer>::New();
42 origRender->SetViewport(origView);
43 origRender->AddActor(origActor);
44 origRender->ResetCamera();
45 origRender->SetBackground(1.0, 0, 0);
46
47 vtkSmartPointer<vtkRenderer> voiRender =
48 vtkSmartPointer<vtkRenderer>::New();
49 voiRender->SetViewport(voiView);
50 voiRender->AddActor(voiActor);
51 voiRender->ResetCamera();
52 voiRender->SetBackground(0.0, 0.0, 0.0);
53 //舞台
54 vtkSmartPointer<vtkRenderWindow> renderwindow =
55 vtkSmartPointer<vtkRenderWindow>::New();
56 renderwindow->AddRenderer(origRender);
57 renderwindow->AddRenderer(voiRender);
58 renderwindow->SetSize(640, 320);
59 renderwindow->SetWindowName("ExtractVolumeOfInterestFromImage");
60 //设置交互
61 vtkSmartPointer<vtkRenderWindowInteractor> rwi =
62 vtkSmartPointer<vtkRenderWindowInteractor>::New();
63 vtkSmartPointer<vtkInteractorStyleImage> style =
64 vtkSmartPointer<vtkInteractorStyleImage>::New();
65 rwi->SetInteractorStyle(style);
66 rwi->SetRenderWindow(renderwindow);
67 rwi->Initialize();
68
69 rwi->Start();
70 return 0;
71 }