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 <vtkImageThreshold.h> 14 #include <vtkImageCast.h> 15 16 //测试图像:../data/lena-gray.jpg 17 int main(int argc, char* argv[]) 18 { 19 vtkSmartPointer<vtkJPEGReader> reader = 20 vtkSmartPointer<vtkJPEGReader>::New(); 21 reader->SetFileName("lena-gray.jpg"); 22 reader->Update(); 23 24 vtkSmartPointer<vtkImageThreshold> thresholdFilter = 25 vtkSmartPointer<vtkImageThreshold>::New(); 26 thresholdFilter->SetInputConnection(reader->GetOutputPort()); 27 thresholdFilter->ThresholdByUpper(100);//设置阈值为100 28 thresholdFilter->SetInValue(255);//大于100的设置为255 29 thresholdFilter->SetOutValue(0);//小于100的设置为0 30 thresholdFilter->Update();//算法执行后必须添加更新消息 31 32 vtkSmartPointer<vtkImageActor> originalActor = 33 vtkSmartPointer<vtkImageActor>::New(); 34 originalActor->SetInputData(reader->GetOutput()); 35 36 vtkSmartPointer<vtkImageActor> binaryActor = 37 vtkSmartPointer<vtkImageActor>::New(); 38 binaryActor->SetInputData(thresholdFilter->GetOutput()); 39 40 double originalViewport[4] = {0.0, 0.0, 0.5, 1.0}; 41 double binaryviewport[4] = {0.5, 0.0, 1.0, 1.0}; 42 43 vtkSmartPointer<vtkRenderer> originalRenderer = 44 vtkSmartPointer<vtkRenderer>::New(); 45 originalRenderer->SetViewport(originalViewport); 46 originalRenderer->AddActor(originalActor); 47 originalRenderer->ResetCamera(); 48 originalRenderer->SetBackground(1.0, 1.0, 1.0); 49 50 vtkSmartPointer<vtkRenderer> binaryRenderer = 51 vtkSmartPointer<vtkRenderer>::New(); 52 binaryRenderer->SetViewport(binaryviewport); 53 binaryRenderer->AddActor(binaryActor); 54 binaryRenderer->ResetCamera(); 55 binaryRenderer->SetBackground(1.0, 1.0, 1.0); 56 57 vtkSmartPointer<vtkRenderWindow> renderWindow = 58 vtkSmartPointer<vtkRenderWindow>::New(); 59 renderWindow->AddRenderer(originalRenderer); 60 renderWindow->AddRenderer(binaryRenderer); 61 renderWindow->SetSize(640, 320); 62 renderWindow->Render(); 63 renderWindow->SetWindowName("ImageBinaryExample"); 64 65 vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = 66 vtkSmartPointer<vtkRenderWindowInteractor>::New(); 67 vtkSmartPointer<vtkInteractorStyleImage> style = 68 vtkSmartPointer<vtkInteractorStyleImage>::New(); 69 70 renderWindowInteractor->SetInteractorStyle(style); 71 renderWindowInteractor->SetRenderWindow(renderWindow); 72 renderWindowInteractor->Initialize(); 73 renderWindowInteractor->Start(); 74 75 return EXIT_SUCCESS; 76 }标签:thresholdFilter,1.0,VTK,36,vtkSmartPointer,renderWindow,New,include,二值化 From: https://www.cnblogs.com/ybqjymy/p/17630954.html