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

  1 #include "vtkAutoInit.h" 
  2 VTK_MODULE_INIT(vtkRenderingOpenGL2);
  3 VTK_MODULE_INIT(vtkInteractionStyle);
  4 
  5 #include <vtkSmartPointer.h>
  6 #include <vtkImageMathematics.h>
  7 #include <vtkImageData.h>
  8 #include <vtkImageSobel2D.h>
  9 #include <vtkImageMagnitude.h>
 10 #include <vtkImageExtractComponents.h>
 11 #include <vtkImageShiftScale.h>
 12 #include <vtkRenderWindow.h>
 13 #include <vtkRenderWindowInteractor.h>
 14 #include <vtkInteractorStyleImage.h>
 15 #include <vtkRenderer.h>
 16 #include <vtkImageActor.h>
 17 #include <vtkJPEGReader.h>
 18 
 19 //测试图像:../data/lena-gray.jpg
 20 int main(int argc, char* argv[])
 21 {
 22     vtkSmartPointer<vtkJPEGReader> reader =
 23         vtkSmartPointer<vtkJPEGReader>::New();
 24     reader->SetFileName("lena-gray.jpg");
 25     reader->Update();
 26 
 27     vtkSmartPointer<vtkImageSobel2D> sobelFilter =
 28         vtkSmartPointer<vtkImageSobel2D>::New();
 29     sobelFilter->SetInputConnection(reader->GetOutputPort());
 30 
 31     vtkSmartPointer<vtkImageExtractComponents> extractXFilter = 
 32         vtkSmartPointer<vtkImageExtractComponents>::New();
 33     extractXFilter->SetComponents(0);
 34     extractXFilter->SetInputConnection(sobelFilter->GetOutputPort());
 35     extractXFilter->Update();
 36 
 37     double xRange[2];
 38     extractXFilter->GetOutput()->GetScalarRange(xRange);
 39 
 40     vtkSmartPointer<vtkImageMathematics> xImageAbs =
 41         vtkSmartPointer<vtkImageMathematics>::New();
 42     xImageAbs->SetOperationToAbsoluteValue();
 43     xImageAbs->SetInputConnection(extractXFilter->GetOutputPort());
 44     xImageAbs->Update();
 45 
 46     vtkSmartPointer<vtkImageShiftScale> xShiftScale =
 47         vtkSmartPointer<vtkImageShiftScale>::New();
 48     xShiftScale->SetOutputScalarTypeToUnsignedChar();
 49     xShiftScale->SetScale( 255 / xRange[1] );
 50     xShiftScale->SetInputConnection(xImageAbs->GetOutputPort());
 51     xShiftScale->Update();
 52 
 53     vtkSmartPointer<vtkImageExtractComponents> extractYFilter = 
 54         vtkSmartPointer<vtkImageExtractComponents>::New();
 55     extractYFilter->SetComponents(1);
 56     extractYFilter->SetInputConnection(sobelFilter->GetOutputPort());
 57     extractYFilter->Update();
 58 
 59     double yRange[2];
 60     extractYFilter->GetOutput()->GetScalarRange(yRange);
 61 
 62     vtkSmartPointer<vtkImageMathematics> yImageAbs =
 63         vtkSmartPointer<vtkImageMathematics>::New();
 64     yImageAbs->SetOperationToAbsoluteValue();
 65     yImageAbs->SetInputConnection(extractYFilter->GetOutputPort());
 66     yImageAbs->Update();
 67 
 68     vtkSmartPointer<vtkImageShiftScale> yShiftScale =
 69         vtkSmartPointer<vtkImageShiftScale>::New();
 70     yShiftScale->SetOutputScalarTypeToUnsignedChar();
 71     yShiftScale->SetScale( 255 / yRange[1] );
 72     yShiftScale->SetInputConnection(yImageAbs->GetOutputPort());
 73     yShiftScale->Update();
 74 
 75     vtkSmartPointer<vtkImageActor> originalActor =
 76         vtkSmartPointer<vtkImageActor>::New();
 77     originalActor->SetInputData(reader->GetOutput());
 78 
 79     vtkSmartPointer<vtkImageActor> xActor =
 80         vtkSmartPointer<vtkImageActor>::New();
 81     xActor->SetInputData(xShiftScale->GetOutput());
 82 
 83     vtkSmartPointer<vtkImageActor> yActor =
 84         vtkSmartPointer<vtkImageActor>::New();
 85     yActor->SetInputData(yShiftScale->GetOutput());
 86 
 87     double originalViewport[4] = {0.0, 0.0, 0.33, 1.0};
 88     double xViewport[4] = {0.33, 0.0, 0.66, 1.0};
 89     double yViewport[4] = {0.66, 0.0, 1.0, 1.0};
 90 
 91     vtkSmartPointer<vtkRenderer> originalRenderer =
 92         vtkSmartPointer<vtkRenderer>::New();
 93     originalRenderer->SetViewport(originalViewport);
 94     originalRenderer->AddActor(originalActor);
 95     originalRenderer->ResetCamera();
 96     originalRenderer->SetBackground(1.0, 1.0, 1.0);
 97 
 98     vtkSmartPointer<vtkRenderer> xRenderer =
 99         vtkSmartPointer<vtkRenderer>::New();
100     xRenderer->SetViewport(xViewport);
101     xRenderer->AddActor(xActor);
102     xRenderer->ResetCamera();
103     xRenderer->SetBackground(1.0, 1.0, 1.0);
104 
105     vtkSmartPointer<vtkRenderer> yRenderer =
106         vtkSmartPointer<vtkRenderer>::New();
107     yRenderer->SetViewport(yViewport);
108     yRenderer->AddActor(yActor);
109     yRenderer->ResetCamera();
110     yRenderer->SetBackground(1.0, 1.0, 1.0);
111 
112     vtkSmartPointer<vtkRenderWindow> renderWindow =
113         vtkSmartPointer<vtkRenderWindow>::New();
114     renderWindow->SetSize(1200, 300);
115     renderWindow->AddRenderer(originalRenderer);
116     renderWindow->AddRenderer(xRenderer);
117     renderWindow->AddRenderer(yRenderer);
118     renderWindow->Render();
119     renderWindow->SetWindowName("SobelExample");
120 
121     vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
122         vtkSmartPointer<vtkRenderWindowInteractor>::New();
123     vtkSmartPointer<vtkInteractorStyleImage> style =
124         vtkSmartPointer<vtkInteractorStyleImage>::New();
125 
126     renderWindowInteractor->SetInteractorStyle(style);
127     renderWindowInteractor->SetRenderWindow(renderWindow);
128     renderWindowInteractor->Initialize();
129     renderWindowInteractor->Start();
130 
131     return EXIT_SUCCESS;
132 }

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