1 #include <vtkAutoInit.h>
2 VTK_MODULE_INIT(vtkRenderingOpenGL);
3
4 #include <vtkSmartPointer.h>
5 #include <vtkJPEGReader.h>
6 #include <vtkImageGradient.h>
7 #include <vtkImageMagnitude.h>
8 #include <vtkImageData.h>
9 #include <vtkImageShiftScale.h>
10 #include <vtkImageActor.h>
11 #include <vtkRenderer.h>
12 #include <vtkRenderWindow.h>
13 #include <vtkRenderWindowInteractor.h>
14 #include <vtkInteractorStyleImage.h>
15
16 int main()
17 {
18 vtkSmartPointer<vtkJPEGReader> reader =
19 vtkSmartPointer<vtkJPEGReader>::New();
20 reader->SetFileName("lena.jpg");
21 reader->Update();
22
23 vtkSmartPointer<vtkImageGradient> imgGradient =
24 vtkSmartPointer<vtkImageGradient>::New();
25 imgGradient->SetInputConnection(reader->GetOutputPort());
26 imgGradient->SetDimensionality(2);//?????
27
28 vtkSmartPointer<vtkImageMagnitude> imgMagnitude =
29 vtkSmartPointer<vtkImageMagnitude>::New();
30 imgMagnitude->SetInputConnection(imgGradient->GetOutputPort());
31 imgMagnitude->Update();
32
33 double Range[2];
34 vtkSmartPointer<vtkImageData> getRange =
35 vtkSmartPointer<vtkImageData>::New();
36 imgMagnitude->GetOutput()->GetScalarRange(Range);//图像灰度范围最小值、最大值
37
38 vtkSmartPointer<vtkImageShiftScale> imgShiftScale =
39 vtkSmartPointer<vtkImageShiftScale>::New();
40 imgShiftScale->SetOutputScalarTypeToUnsignedChar(); //强制类型转换 0~255
41 imgShiftScale->SetScale(255 / Range[1]); //灰度映射间距
42 imgShiftScale->SetInputConnection(imgMagnitude->GetOutputPort());
43 imgShiftScale->Update();
44
45 /
46 vtkSmartPointer<vtkImageActor> origActor =
47 vtkSmartPointer<vtkImageActor>::New();
48 origActor->SetInputData(reader->GetOutput());
49
50 vtkSmartPointer<vtkImageActor> GradientActor =
51 vtkSmartPointer<vtkImageActor>::New();
52 GradientActor->SetInputData(imgShiftScale->GetOutput());
53
54 double origView[4] = { 0, 0, 0.5, 1 };
55 double gradientView[4] = { 0.5, 0, 1, 1 };
56 vtkSmartPointer<vtkRenderer> origRender =
57 vtkSmartPointer<vtkRenderer>::New();
58 origRender->SetViewport(origView);
59 origRender->AddActor(origActor);
60 origRender->ResetCamera();
61 origRender->SetBackground(1.0, 0, 0);
62
63 vtkSmartPointer<vtkRenderer> gradientRender =
64 vtkSmartPointer<vtkRenderer>::New();
65 gradientRender->SetViewport(gradientView);
66 gradientRender->AddActor(GradientActor);
67 gradientRender->ResetCamera();
68 gradientRender->SetBackground(1, 1, 1);
69
70 vtkSmartPointer<vtkRenderWindow> rw =
71 vtkSmartPointer<vtkRenderWindow>::New();
72 rw->AddRenderer(origRender);
73 rw->AddRenderer(gradientRender);
74 rw->SetSize(640, 320);
75 rw->SetWindowName("Image Gradient");
76
77 vtkSmartPointer<vtkRenderWindowInteractor> rwi =
78 vtkSmartPointer<vtkRenderWindowInteractor>::New();
79 vtkSmartPointer<vtkInteractorStyleImage> style =
80 vtkSmartPointer<vtkInteractorStyleImage>::New();
81 rwi->SetRenderWindow(rw);
82 rwi->SetInteractorStyle(style);
83 rwi->Initialize();
84 rwi->Start();
85
86 return 0;
87 }