1 #include <vtkAutoInit.h>
2 VTK_MODULE_INIT(vtkRenderingOpenGL);
3
4 #include <vtkSmartPointer.h>
5 #include <vtkJPEGReader.h>
6 #include <vtkImageThreshold.h>
7 #include <vtkImageActor.h>
8 #include <vtkRenderer.h>
9 #include <vtkRenderWindow.h>
10 #include <vtkRenderWindowInteractor.h>
11 #include <vtkInteractorStyleImage.h>
12
13 int main()
14 {
15 vtkSmartPointer<vtkJPEGReader> reader =
16 vtkSmartPointer<vtkJPEGReader>::New();
17 reader->SetFileName("lena.jpg");
18 reader->Update();
19
20 vtkSmartPointer<vtkImageThreshold> threshould =
21 vtkSmartPointer<vtkImageThreshold>::New();
22 threshould->SetInputConnection(reader->GetOutputPort());
23 threshould->ThresholdByUpper(90);
24 threshould->SetInValue(255);
25 threshould->SetOutValue(0);
26 threshould->Update(); //算法执行后必须添加更新消息!!!
27
28 vtkSmartPointer<vtkImageActor> origActor =
29 vtkSmartPointer<vtkImageActor>::New();
30 origActor->SetInputData(reader->GetOutput());
31 vtkSmartPointer<vtkImageActor> binaryActor =
32 vtkSmartPointer<vtkImageActor>::New();
33 binaryActor->SetInputData(threshould->GetOutput());
34
35 double origView[4] = { 0, 0, 0.5, 1 };
36 double binaryView[4] = { 0.5, 0, 1, 1 };
37 vtkSmartPointer<vtkRenderer> origRender =
38 vtkSmartPointer<vtkRenderer>::New();
39 origRender->SetViewport(origView);
40 origRender->AddActor(origActor);
41 origRender->ResetCamera();
42 origRender->SetBackground(1, 0, 0);
43
44 vtkSmartPointer<vtkRenderer> binaryRender =
45 vtkSmartPointer<vtkRenderer>::New();
46 binaryRender->SetViewport(binaryView);
47 binaryRender->AddActor(binaryActor);
48 binaryRender->ResetCamera();
49 binaryRender->SetBackground(1, 1, 1);
50
51 /
52 vtkSmartPointer<vtkRenderWindow> rw =
53 vtkSmartPointer<vtkRenderWindow>::New();
54 rw->AddRenderer(origRender);
55 rw->AddRenderer(binaryRender);
56 rw->SetSize(640, 240);
57 rw->SetWindowName("Binary Image");
58
59 vtkSmartPointer<vtkRenderWindowInteractor> rwi =
60 vtkSmartPointer<vtkRenderWindowInteractor>::New();
61 vtkSmartPointer<vtkInteractorStyleImage> style =
62 vtkSmartPointer<vtkInteractorStyleImage>::New();
63 rwi->SetInteractorStyle(style);
64 rwi->SetRenderWindow(rw);
65 rwi->Initialize();
66 rwi->Start();
67 return 0;
68 }