一杯清酒邀明月
天下本无事,庸人扰之而烦耳。

 1 #include "vtkAutoInit.h" 
 2 VTK_MODULE_INIT(vtkRenderingOpenGL2);
 3 VTK_MODULE_INIT(vtkInteractionStyle);
 4 
 5 #include <vtkSmartPointer.h>
 6 #include <vtkImageData.h>
 7 #include <vtkImageShiftScale.h>
 8 #include <vtkRenderWindow.h>
 9 #include <vtkRenderWindowInteractor.h>
10 #include <vtkInteractorStyleImage.h>
11 #include <vtkRenderer.h>
12 #include <vtkImageActor.h>
13 #include <vtkBMPReader.h>
14 #include <vtkImageShrink3D.h>
15 
16 //测试图像:../data/lena.bmp
17 int main(int argc, char* argv[])
18 {
19     vtkSmartPointer<vtkBMPReader> reader =
20         vtkSmartPointer<vtkBMPReader>::New();
21     reader->SetFileName("lena.bmp");
22     reader->Update();
23 
24     vtkSmartPointer<vtkImageShrink3D> shrinkFilter = 
25         vtkSmartPointer<vtkImageShrink3D>::New();
26     shrinkFilter->SetInputConnection(reader->GetOutputPort());
27     shrinkFilter->SetShrinkFactors(20,20,1);
28     shrinkFilter->Update();
29 
30     int originalDims[3];
31     reader->GetOutput()->GetDimensions(originalDims);
32 
33     double originalSpace[3];
34     reader->GetOutput()->GetSpacing(originalSpace);
35 
36     int shrinkDims[3];
37     shrinkFilter->GetOutput()->GetDimensions(shrinkDims);
38 
39     double shrinkSpace[3];
40     shrinkFilter->GetOutput()->GetSpacing(shrinkSpace);
41 
42     std::cout<<"原图图像维数      :"<<originalDims[0] << " "<<originalDims[1]<<" "<<originalDims[2]<<std::endl;
43     std::cout<<"原图图像像素间隔  :"<<originalSpace[0] << " "<<originalSpace[1]<<" "<<originalSpace[2]<<std::endl;
44     std::cout<<"重采样图像维数    :"<<shrinkDims[0] << " "<<shrinkDims[1]<<" "<<shrinkDims[2]<<std::endl;
45     std::cout<<"重采样图像像素间隔:"<<shrinkSpace[0] << " "<<shrinkSpace[1]<<" "<<shrinkSpace[2]<<std::endl;
46 
47     vtkSmartPointer<vtkImageActor> originalActor =
48         vtkSmartPointer<vtkImageActor>::New();
49     originalActor->SetInputData( reader->GetOutput());
50 
51     vtkSmartPointer<vtkImageActor> shrinkActor =
52         vtkSmartPointer<vtkImageActor>::New();
53     shrinkActor->SetInputData(shrinkFilter->GetOutput());
54 
55     double originalViewport[4] = {0.0, 0.0, 0.5, 1.0};
56     double shrinkViewport[4] = {0.5, 0.0, 1.0, 1.0};
57 
58     vtkSmartPointer<vtkRenderer> originalRenderer =
59         vtkSmartPointer<vtkRenderer>::New();
60     originalRenderer->SetViewport(originalViewport);
61     originalRenderer->AddActor(originalActor);
62     originalRenderer->ResetCamera();
63     originalRenderer->SetBackground(1.0, 1.0, 1.0);
64 
65     vtkSmartPointer<vtkRenderer> shrinkRenderer =
66         vtkSmartPointer<vtkRenderer>::New();
67     shrinkRenderer->SetViewport(shrinkViewport);
68     shrinkRenderer->AddActor(shrinkActor);
69     shrinkRenderer->ResetCamera();
70     shrinkRenderer->SetBackground(1.0, 1.0, 1.0);
71 
72     vtkSmartPointer<vtkRenderWindow> renderWindow =
73         vtkSmartPointer<vtkRenderWindow>::New();
74     renderWindow->AddRenderer(originalRenderer);
75     renderWindow->AddRenderer(shrinkRenderer);
76     renderWindow->SetSize(640, 480);
77     renderWindow->Render();
78     renderWindow->SetWindowName("ImageShrink3DExample");
79 
80     vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
81         vtkSmartPointer<vtkRenderWindowInteractor>::New();
82     vtkSmartPointer<vtkInteractorStyleImage> style =
83         vtkSmartPointer<vtkInteractorStyleImage>::New();
84 
85     renderWindowInteractor->SetInteractorStyle(style);
86     renderWindowInteractor->SetRenderWindow(renderWindow);
87     renderWindowInteractor->Initialize();
88     renderWindowInteractor->Start();
89 
90     return EXIT_SUCCESS;
91 }

posted on 2023-08-15 11:35  一杯清酒邀明月  阅读(61)  评论(0编辑  收藏  举报