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

  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 <vtkImageFFT.h>
  8 #include <vtkImageRFFT.h>
  9 #include <vtkRenderWindow.h>
 10 #include <vtkRenderWindowInteractor.h>
 11 #include <vtkInteractorStyleImage.h>
 12 #include <vtkRenderer.h>
 13 #include <vtkImageActor.h>
 14 #include <vtkImageCast.h>
 15 #include <vtkJPEGReader.h>
 16 #include <vtkImageExtractComponents.h>
 17 #include <vtkImageFourierCenter.h>
 18 #include <vtkImageMagnitude.h>
 19 #include <vtkImageShiftScale.h>
 20 
 21 //测试图像:../data/lena-gray.jpg
 22 int main(int argc, char* argv[])
 23 {
 24     vtkSmartPointer<vtkJPEGReader> reader =
 25         vtkSmartPointer<vtkJPEGReader>::New();
 26     reader->SetFileName ("lena-gray.jpg");
 27     reader->Update();
 28 
 29     vtkSmartPointer<vtkImageFFT> fftFilter = 
 30         vtkSmartPointer<vtkImageFFT>::New();
 31     fftFilter->SetInputConnection(reader->GetOutputPort());
 32     fftFilter->SetDimensionality(2);
 33     fftFilter->Update();
 34 
 35     vtkSmartPointer<vtkImageExtractComponents> fftExtractReal =
 36         vtkSmartPointer<vtkImageExtractComponents>::New();
 37     fftExtractReal->SetInputConnection(fftFilter->GetOutputPort());
 38     fftExtractReal->SetComponents(0);
 39 
 40     double range[2];
 41     fftExtractReal->GetOutput()->GetScalarRange(range);
 42 
 43     vtkSmartPointer<vtkImageShiftScale> ShiftScale =
 44         vtkSmartPointer<vtkImageShiftScale>::New();
 45     ShiftScale->SetOutputScalarTypeToUnsignedChar();
 46     ShiftScale->SetScale( 255.0 / (range[1]-range[0]) );
 47     ShiftScale->SetShift(-range[0]);
 48     ShiftScale->SetInputConnection(fftExtractReal->GetOutputPort());
 49     ShiftScale->Update();
 50 
 51     vtkSmartPointer<vtkImageRFFT> rfftFilter =
 52         vtkSmartPointer<vtkImageRFFT>::New();
 53     rfftFilter->SetInputConnection(fftFilter->GetOutputPort());
 54     rfftFilter->SetDimensionality(2);
 55     rfftFilter->Update();
 56 
 57     vtkSmartPointer<vtkImageExtractComponents> ifftExtractReal =
 58         vtkSmartPointer<vtkImageExtractComponents>::New();
 59     ifftExtractReal->SetInputConnection(rfftFilter->GetOutputPort());
 60     ifftExtractReal->SetComponents(0);
 61 
 62     vtkSmartPointer<vtkImageCast> rfftCastFilter =
 63         vtkSmartPointer<vtkImageCast>::New();
 64     rfftCastFilter->SetInputConnection(ifftExtractReal->GetOutputPort());
 65     rfftCastFilter->SetOutputScalarTypeToUnsignedChar();
 66     rfftCastFilter->Update();
 67 
 68     vtkSmartPointer<vtkImageActor> originalActor =
 69         vtkSmartPointer<vtkImageActor>::New();
 70     originalActor->SetInputData(reader->GetOutput());
 71 
 72     vtkSmartPointer<vtkImageActor> fftActor =
 73         vtkSmartPointer<vtkImageActor>::New();
 74     fftActor->SetInputData(ShiftScale->GetOutput());
 75 
 76     vtkSmartPointer<vtkImageActor> rfftActor =
 77         vtkSmartPointer<vtkImageActor>::New();
 78     rfftActor->SetInputData(rfftCastFilter->GetOutput());
 79 
 80     double originalViewport[4] = {0.0, 0.0, 0.33, 1.0};
 81     double fftViewport[4] = {0.33, 0.0, 0.66, 1.0};
 82     double rfftViewport[4] = {0.66, 0.0, 1.0, 1.0};
 83 
 84     vtkSmartPointer<vtkRenderer> originalRenderer =
 85         vtkSmartPointer<vtkRenderer>::New();
 86     originalRenderer->SetViewport(originalViewport);
 87     originalRenderer->AddActor(originalActor);
 88     originalRenderer->ResetCamera();
 89     originalRenderer->SetBackground(1.0, 1.0, 1.0);
 90 
 91     vtkSmartPointer<vtkRenderer> fftRenderer =
 92         vtkSmartPointer<vtkRenderer>::New();
 93     fftRenderer->SetViewport(fftViewport);
 94     fftRenderer->AddActor(fftActor);
 95     fftRenderer->ResetCamera();
 96     fftRenderer->SetBackground(1.0, 1.0, 1.0);
 97 
 98     vtkSmartPointer<vtkRenderer> rfftRenderer =
 99         vtkSmartPointer<vtkRenderer>::New();
100     rfftRenderer->SetViewport(rfftViewport);
101     rfftRenderer->AddActor(rfftActor);
102     rfftRenderer->ResetCamera();
103     rfftRenderer->SetBackground(1.0, 1.0, 1.0);
104 
105     vtkSmartPointer<vtkRenderWindow> renderWindow =
106         vtkSmartPointer<vtkRenderWindow>::New();
107     renderWindow->AddRenderer(originalRenderer);
108     renderWindow->AddRenderer(fftRenderer);
109     renderWindow->AddRenderer(rfftRenderer);
110     renderWindow->SetSize(640, 320);
111     renderWindow->Render();
112     renderWindow->SetWindowName("FFTAndRFFTExample");
113 
114     vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
115         vtkSmartPointer<vtkRenderWindowInteractor>::New();
116     vtkSmartPointer<vtkInteractorStyleImage> style =
117         vtkSmartPointer<vtkInteractorStyleImage>::New();
118 
119     renderWindowInteractor->SetInteractorStyle(style);
120     renderWindowInteractor->SetRenderWindow(renderWindow);
121     renderWindowInteractor->Initialize();
122     renderWindowInteractor->Start();
123 
124     return EXIT_SUCCESS;
125 }

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