1.中值滤波
vtkImageHybridMedian2D实现了对二维图像的中值滤波。其实现原理是,采用一个5x5的模板,逐次将模板中心对应于图像的每个像素上,将模板图像覆盖的像素的中值作为当前像素的输出值。
1 #include <vtkAutoInit.h> 2 VTK_MODULE_INIT(vtkRenderingOpenGL); 3 4 #include <vtkSmartPointer.h> 5 #include <vtkJPEGReader.h> 6 #include <vtkImageData.h> 7 #include <vtkImageCast.h> 8 #include <vtkImageHybridMedian2D.h> 9 #include <vtkImageActor.h> 10 #include <vtkRenderer.h> 11 #include <vtkRenderWindow.h> 12 #include <vtkRenderWindowInteractor.h> 13 #include <vtkInteractorStyleImage.h> 14 15 int main(int argc, char* argv[]) 16 { 17 vtkSmartPointer<vtkJPEGReader> reader = 18 vtkSmartPointer<vtkJPEGReader>::New(); 19 reader->SetFileName("lena.jpg"); 20 reader->Update(); 21 22 vtkSmartPointer<vtkImageHybridMedian2D> hybridMedian = 23 vtkSmartPointer<vtkImageHybridMedian2D>::New(); 24 hybridMedian->SetInputData(reader->GetOutput()); 25 hybridMedian->Update(); 26 /// 27 vtkSmartPointer<vtkImageActor> originalActor = 28 vtkSmartPointer<vtkImageActor>::New(); 29 originalActor->SetInputData(reader->GetOutput()); 30 31 vtkSmartPointer<vtkImageActor> hybridMedianActor = 32 vtkSmartPointer<vtkImageActor>::New(); 33 hybridMedianActor->SetInputData(hybridMedian->GetOutput()); 34 / 35 double originalViewport[4] = { 0.0, 0.0, 0.5, 1.0 }; 36 double hybridMedianViewport[4] = { 0.5, 0.0, 1.0, 1.0 }; 37 38 vtkSmartPointer<vtkRenderer> originalRenderer = 39 vtkSmartPointer<vtkRenderer>::New(); 40 originalRenderer->SetViewport(originalViewport); 41 originalRenderer->AddActor(originalActor); 42 originalRenderer->ResetCamera(); 43 originalRenderer->SetBackground(1.0,0,0); 44 45 vtkSmartPointer<vtkRenderer> hybridMedianRenderer = 46 vtkSmartPointer<vtkRenderer>::New(); 47 hybridMedianRenderer->SetViewport(hybridMedianViewport); 48 hybridMedianRenderer->AddActor(hybridMedianActor); 49 hybridMedianRenderer->ResetCamera(); 50 hybridMedianRenderer->SetBackground(1.0, 1.0, 1.0); 51 // 52 vtkSmartPointer<vtkRenderWindow> rw = 53 vtkSmartPointer<vtkRenderWindow>::New(); 54 rw->AddRenderer(originalRenderer); 55 rw->AddRenderer(hybridMedianRenderer); 56 rw->SetSize(640, 320); 57 rw->Render(); 58 rw->SetWindowName("MedianFilterExample"); 59 60 vtkSmartPointer<vtkRenderWindowInteractor> rwi = 61 vtkSmartPointer<vtkRenderWindowInteractor>::New(); 62 vtkSmartPointer<vtkInteractorStyleImage> style = 63 vtkSmartPointer<vtkInteractorStyleImage>::New(); 64 rwi->SetInteractorStyle(style); 65 rwi->SetRenderWindow(rw); 66 rwi->Initialize(); 67 rwi->Start(); 68 69 return 0; 70 }
该类使用非常简单,不需要用户设置任何参数。该方法能够有效的保持图像边缘,并对于椒盐噪声有较好的抑制作用。对于三维图像,则使用vtkImageHybridMedian3D类。
执行结果如下: