1 #include <vtkAutoInit.h>
2 VTK_MODULE_INIT(vtkRenderingOpenGL);
3
4 #include <vtkSmartPointer.h>
5 #include <vtkJPEGReader.h>
6 #include <vtkImageFFT.h>
7 #include <vtkImageExtractComponents.h>
8 #include <vtkImageData.h>
9 #include <vtkImageShiftScale.h>
10 #include <vtkImageRFFT.h>
11 #include <vtkImageActor.h>
12 #include <vtkrenderer.h>
13 #include <vtkRenderWindow.h>
14 #include <vtkRenderWindowInteractor.h>
15 #include <vtkInteractorStyleImage.h>
16
17 int main()
18 {
19 vtkSmartPointer<vtkJPEGReader> reader =
20 vtkSmartPointer<vtkJPEGReader>::New();
21 reader->SetFileName("lena.jpg");
22 reader->Update();
23 //FFT//
24 vtkSmartPointer<vtkImageFFT> imgFFT =
25 vtkSmartPointer<vtkImageFFT>::New();
26 imgFFT->SetInputConnection(reader->GetOutputPort());
27 imgFFT->SetDimensionality(2);
28 imgFFT->Update();
29 // the resualts are complex, we shoule extract one component to display
30 vtkSmartPointer<vtkImageExtractComponents> imgComponent =
31 vtkSmartPointer<vtkImageExtractComponents>::New();
32 imgComponent->SetInputConnection(imgFFT->GetOutputPort());
33 imgComponent->SetComponents(0);
34 // norm image range
35 double Range[2];
36 imgComponent->GetOutput()->GetScalarRange(Range);
37
38 vtkSmartPointer<vtkImageShiftScale> imgfftShiftScale =
39 vtkSmartPointer<vtkImageShiftScale>::New();
40 imgfftShiftScale->SetOutputScalarTypeToUnsignedChar();
41 imgfftShiftScale->SetScale(255.0 / (Range[1] - Range[0]));
42 imgfftShiftScale->SetShift(-Range[0]);
43 imgfftShiftScale->SetInputConnection(imgComponent->GetOutputPort());
44 imgfftShiftScale->Update();
45 //RFFT/
46 vtkSmartPointer<vtkImageRFFT> imgRFFT =
47 vtkSmartPointer<vtkImageRFFT>::New();
48 imgRFFT->SetInputConnection(imgFFT->GetOutputPort());
49 imgRFFT->SetDimensionality(2);
50 imgRFFT->Update();
51
52 vtkSmartPointer<vtkImageExtractComponents> imgComponetNew =
53 vtkSmartPointer<vtkImageExtractComponents>::New();
54 imgComponetNew->SetInputConnection(imgRFFT->GetOutputPort());
55 imgComponetNew->SetComponents(0);
56
57 double RangeNew[2];
58 imgComponetNew->GetOutput()->GetScalarRange(RangeNew);
59 vtkSmartPointer<vtkImageShiftScale> imgrfftShiftScale =
60 vtkSmartPointer<vtkImageShiftScale>::New();
61 imgrfftShiftScale->SetOutputScalarTypeToUnsignedChar();
62 imgrfftShiftScale->SetScale(255.0 / (RangeNew[1] - RangeNew[0]));
63 imgrfftShiftScale->SetInputConnection(imgComponetNew->GetOutputPort());
64 imgrfftShiftScale->Update();
65 ///
66 //谨记vtkImageActor仅能够显示UnsignedChar类型数据
67 vtkSmartPointer<vtkImageActor> origActor =
68 vtkSmartPointer<vtkImageActor>::New();
69 origActor->SetInputData(reader->GetOutput());
70 vtkSmartPointer<vtkImageActor> fftActor =
71 vtkSmartPointer<vtkImageActor>::New();
72 fftActor->SetInputData(imgFFT->GetOutput());
73 vtkSmartPointer<vtkImageActor> rfftActor =
74 vtkSmartPointer<vtkImageActor>::New();
75 rfftActor->SetInputData(imgrfftShiftScale->GetOutput());
76 ///
77 double origView[4] = { 0, 0, 0.33, 1 };
78 double fftView[4] = { 0.33, 0, 0.66, 1 };
79 double rfftView[4] = { 0.66, 0, 1, 1 };
80 vtkSmartPointer<vtkRenderer> origRender =
81 vtkSmartPointer<vtkRenderer>::New();
82 origRender->SetViewport(origView);
83 origRender->AddActor(origActor);
84 origRender->ResetCamera();
85 origRender->SetBackground(1, 0, 0);
86
87 vtkSmartPointer<vtkRenderer> fftRender =
88 vtkSmartPointer<vtkRenderer>::New();
89 fftRender->SetViewport(fftView);
90 fftRender->AddActor(fftActor);
91 fftRender->ResetCamera();
92 fftRender->SetBackground(0, 1, 0);
93
94 vtkSmartPointer<vtkRenderer> rfftRender =
95 vtkSmartPointer<vtkRenderer>::New();
96 rfftRender->SetViewport(rfftView);
97 rfftRender->AddActor(rfftActor);
98 rfftRender->ResetCamera();
99 rfftRender->SetBackground(0, 0, 1);
100 /
101 vtkSmartPointer<vtkRenderWindow> rw =
102 vtkSmartPointer<vtkRenderWindow>::New();
103 rw->AddRenderer(origRender);
104 rw->AddRenderer(fftRender);
105 rw->AddRenderer(rfftRender);
106 rw->SetWindowName("Frequncy_FFT_RFFT");
107 rw->SetSize(960, 320);
108
109 vtkSmartPointer<vtkRenderWindowInteractor> rwi =
110 vtkSmartPointer<vtkRenderWindowInteractor>::New();
111 vtkSmartPointer<vtkInteractorStyleImage> style =
112 vtkSmartPointer<vtkInteractorStyleImage>::New();
113 rwi->SetInteractorStyle(style);
114 rwi->SetRenderWindow(rw);
115 rwi->Initialize();
116 rwi->Start();
117
118 return 0;
119 }