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 <vtkRenderWindow.h> 8 #include <vtkRenderWindowInteractor.h> 9 #include <vtkInteractorStyleImage.h> 10 #include <vtkRenderer.h> 11 #include <vtkImageActor.h> 12 #include <vtkJPEGReader.h> 13 #include <vtkImageGradient.h> 14 #include <vtkImageMagnitude.h> 15 #include <vtkImageCast.h> 16 #include <vtkImageShiftScale.h> 17 18 //测试图像:../data/lena-gray.jpg 19 int main(int argc, char* argv[]) 20 { 21 vtkSmartPointer<vtkJPEGReader> reader = 22 vtkSmartPointer<vtkJPEGReader>::New(); 23 reader->SetFileName("lena-gray.jpg"); 24 reader->Update(); 25 26 vtkSmartPointer<vtkImageGradient> gradientFilter = 27 vtkSmartPointer<vtkImageGradient>::New(); 28 gradientFilter->SetInputConnection(reader->GetOutputPort()); 29 gradientFilter->SetDimensionality(2); 30 31 vtkSmartPointer<vtkImageMagnitude> magnitudeFilter = 32 vtkSmartPointer<vtkImageMagnitude>::New(); 33 magnitudeFilter->SetInputConnection(gradientFilter->GetOutputPort()); 34 magnitudeFilter->Update(); 35 36 double range[2]; 37 magnitudeFilter->GetOutput()->GetScalarRange(range); 38 39 vtkSmartPointer<vtkImageShiftScale> ShiftScale = 40 vtkSmartPointer<vtkImageShiftScale>::New(); 41 ShiftScale->SetOutputScalarTypeToUnsignedChar(); 42 ShiftScale->SetScale( 255 / range[1] ); 43 ShiftScale->SetInputConnection(magnitudeFilter->GetOutputPort()); 44 ShiftScale->Update(); 45 46 vtkSmartPointer<vtkImageActor> originalActor = 47 vtkSmartPointer<vtkImageActor>::New(); 48 originalActor->SetInputData(reader->GetOutput()); 49 50 vtkSmartPointer<vtkImageActor> gradActor = 51 vtkSmartPointer<vtkImageActor>::New(); 52 gradActor->SetInputData(ShiftScale->GetOutput()); 53 54 double originalViewport[4] = {0.0, 0.0, 0.5, 1.0}; 55 double gradviewport[4] = {0.5, 0.0, 1.0, 1.0}; 56 57 vtkSmartPointer<vtkRenderer> originalRenderer = 58 vtkSmartPointer<vtkRenderer>::New(); 59 originalRenderer->SetViewport(originalViewport); 60 originalRenderer->AddActor(originalActor); 61 originalRenderer->ResetCamera(); 62 originalRenderer->SetBackground(1.0, 1.0, 1.0); 63 64 vtkSmartPointer<vtkRenderer> gradRenderer = 65 vtkSmartPointer<vtkRenderer>::New(); 66 gradRenderer->SetViewport(gradviewport); 67 gradRenderer->AddActor(gradActor); 68 gradRenderer->ResetCamera(); 69 gradRenderer->SetBackground(1.0, 1.0, 1.0); 70 71 vtkSmartPointer<vtkRenderWindow> renderWindow = 72 vtkSmartPointer<vtkRenderWindow>::New(); 73 renderWindow->AddRenderer(originalRenderer); 74 renderWindow->AddRenderer(gradRenderer); 75 renderWindow->SetSize( 640, 320 ); 76 renderWindow->Render(); 77 renderWindow->SetWindowName("ImageGradientExample"); 78 79 vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = 80 vtkSmartPointer<vtkRenderWindowInteractor>::New(); 81 vtkSmartPointer<vtkInteractorStyleImage> style = 82 vtkSmartPointer<vtkInteractorStyleImage>::New(); 83 84 renderWindowInteractor->SetInteractorStyle(style); 85 renderWindowInteractor->SetRenderWindow(renderWindow); 86 renderWindowInteractor->Initialize(); 87 renderWindowInteractor->Start(); 88 89 return EXIT_SUCCESS; 90 }标签:ShiftScale,1.0,VTK,37,vtkSmartPointer,算子,renderWindow,New,include From: https://www.cnblogs.com/ybqjymy/p/17630958.html