1.低通滤波器
低通滤波是将频域图像中的高频部分滤除而通过低频部分。图像的边缘和噪声对应于频域图像中的高频部分,而低通滤波的作用即是减弱这部分的能量,从而达到图像平滑去噪的目的。
2.理想低通滤波器
最简单的低通滤波器是理想低通滤波器,基本思想是给定一个频率阈值,将高于该阈值的所有部分设置为0,而低于该频率的部分保持不变。
理想是指该滤波器不能用电子元器件来实现,但是可以通过计算机来模拟。
在VTK中定义了理想低通滤波器,下面我们来看下怎么使用该滤波器来对图像进行低通滤波:
1 #include <vtkAutoInit.h> 2 VTK_MODULE_INIT(vtkRenderingOpenGL); 3 4 #include <vtkSmartPointer.h> 5 #include <vtkJPEGReader.h> 6 #include <vtkImageFFT.h> 7 #include <vtkImageIdealLowPass.h> 8 #include <vtkImageData.h> 9 #include <vtkImageRFFT.h> 10 #include <vtkImageCast.h> 11 #include <vtkImageExtractComponents.h> 12 #include <vtkImageActor.h> 13 #include <vtkRenderer.h> 14 #include <vtkRenderWindow.h> 15 #include <vtkRenderWindowInteractor.h> 16 17 int main() 18 { 19 vtkSmartPointer<vtkJPEGReader> reader = 20 vtkSmartPointer<vtkJPEGReader>::New(); 21 reader->SetFileName("lena.jpg"); 22 reader->Update(); 23 24 vtkSmartPointer<vtkImageFFT> fftFilter = 25 vtkSmartPointer<vtkImageFFT>::New(); 26 fftFilter->SetInputConnection(reader->GetOutputPort()); 27 fftFilter->Update(); 28 29 vtkSmartPointer<vtkImageIdealLowPass> lowPassFilter = 30 vtkSmartPointer<vtkImageIdealLowPass>::New(); 31 lowPassFilter->SetInputConnection(fftFilter->GetOutputPort()); 32 lowPassFilter->SetXCutOff(0.05); 33 lowPassFilter->SetYCutOff(0.05); 34 lowPassFilter->Update(); 35 36 vtkSmartPointer<vtkImageRFFT> rfftFilter = 37 vtkSmartPointer<vtkImageRFFT>::New(); 38 rfftFilter->SetInputConnection(lowPassFilter->GetOutputPort()); 39 rfftFilter->Update(); 40 41 vtkSmartPointer<vtkImageExtractComponents> ifftExtractReal = 42 vtkSmartPointer<vtkImageExtractComponents>::New(); 43 ifftExtractReal->SetInputConnection(rfftFilter->GetOutputPort()); 44 ifftExtractReal->SetComponents(0); 45 46 vtkSmartPointer<vtkImageCast> castFilter = 47 vtkSmartPointer<vtkImageCast>::New(); 48 castFilter->SetInputConnection(ifftExtractReal->GetOutputPort()); 49 castFilter->SetOutputScalarTypeToUnsignedChar(); 50 castFilter->Update(); 51 52 vtkSmartPointer<vtkImageActor> originalActor = 53 vtkSmartPointer<vtkImageActor>::New(); 54 originalActor->SetInputData(reader->GetOutput()); 55 56 vtkSmartPointer<vtkImageActor> erodedActor = 57 vtkSmartPointer<vtkImageActor>::New(); 58 erodedActor->SetInputData(castFilter->GetOutput()); 59 60 double leftViewport[4] = { 0.0, 0.0, 0.5, 1.0 }; 61 double rightViewport[4] = { 0.5, 0.0, 1.0, 1.0 }; 62 vtkSmartPointer<vtkRenderer> leftRenderer = 63 vtkSmartPointer<vtkRenderer>::New(); 64 leftRenderer->AddActor(originalActor); 65 leftRenderer->ResetCamera(); 66 leftRenderer->SetViewport(leftViewport); 67 leftRenderer->SetBackground(1.0, 0, 0); 68 69 vtkSmartPointer<vtkRenderer> rightRenderer = 70 vtkSmartPointer<vtkRenderer>::New(); 71 rightRenderer->AddActor(erodedActor); 72 rightRenderer->SetViewport(rightViewport); 73 rightRenderer->ResetCamera(); 74 rightRenderer->SetBackground(1.0, 1.0, 1.0); 75 76 vtkSmartPointer<vtkRenderWindow> renderWindow = 77 vtkSmartPointer<vtkRenderWindow>::New(); 78 renderWindow->AddRenderer(rightRenderer); 79 renderWindow->AddRenderer(leftRenderer); 80 renderWindow->SetSize(640, 320); 81 renderWindow->SetWindowName("Frequency_IdealLowPassFilter"); 82 83 vtkSmartPointer<vtkRenderWindowInteractor> interactor = 84 vtkSmartPointer<vtkRenderWindowInteractor>::New(); 85 interactor->SetRenderWindow(renderWindow); 86 renderWindow->Render(); 87 interactor->Start(); 88 89 return 0; 90 }
首先读入一副图像,通过vtkImageFFT将图像转换到频域空间。vtkImageIdealLowPass对频域图像做理想低通滤波,需要用户设置每个方向的截断频率,相应的设置函数SetXCutOff()和SetYCutOff()。执行完毕后,需要通过vtkImageRFFT将处理后的频域图像转换至空域图像。 需要注意的是,转换后的图像每个像素都是一个复数,需要vtkImageExtractComponents将该图像的第一个分量提出出来显示,否则图像不能正确显示。由于傅里叶变换输入输出的数据类型都是double,为了方便显示,还需要将其转换为Unsigned char类型,这里vtkImageCast负责类型转换。
下面是对图像做低通滤波的效果:
从结果看,在过滤掉图像的高频部分后,图像变得模糊,丢失了许多细节,另外还可以看到图像会存在一定的振铃效应,这也是理想低通滤波的特点。
3.巴特沃兹低通滤波器
在实际中经常使用的是巴特沃斯滤波器。巴特沃斯滤波器对应的转移函数(可以看做是一个系数矩阵)是:
其中D(u,v)表示频域点(u,v)到频域图像原点的距离,称为截止频率,当D(u,v) = 时,H(u,v)=0.5,即对应的频域能量将为原来的一半。因为巴特沃斯低通滤波器在高低频间的过渡平滑,因此不会出现明显的振铃效应。VTK中实现巴特沃斯低通滤波器的类是vtkImageButterworthLowPass.
其使用过程如下:
1 #include <vtkAutoInit.h> 2 VTK_MODULE_INIT(vtkRenderingOpenGL); 3 4 #include <vtkSmartPointer.h> 5 #include <vtkJPEGReader.h> 6 #include <vtkImageFFT.h> 7 #include <vtkImageButterworthLowPass.h> 8 #include <vtkImageRFFT.h> 9 #include <vtkImageExtractComponents.h> 10 #include <vtkImageCast.h> 11 #include <vtkImageActor.h> 12 #include <vtkRenderer.h> 13 #include <vtkRenderWindow.h> 14 #include <vtkRenderWindowInteractor.h> 15 #include <vtkInteractorStyleImage.h> 16 17 int main() 18 { 19 vtkSmartPointer<vtkJPEGReader> reader = 20 vtkSmartPointer<vtkJPEGReader>::New(); 21 reader->SetFileName("lena.jpg"); 22 reader->Update(); 23 24 vtkSmartPointer<vtkImageFFT> fftFilter = 25 vtkSmartPointer<vtkImageFFT>::New(); 26 fftFilter->SetInputConnection(reader->GetOutputPort()); 27 fftFilter->Update(); 28 29 vtkSmartPointer<vtkImageButterworthLowPass> lowPassFilter = 30 vtkSmartPointer<vtkImageButterworthLowPass>::New(); 31 lowPassFilter->SetInputConnection(fftFilter->GetOutputPort()); 32 lowPassFilter->SetXCutOff(0.05); 33 lowPassFilter->SetYCutOff(0.05); 34 lowPassFilter->Update(); 35 36 vtkSmartPointer<vtkImageRFFT> rfftFilter = 37 vtkSmartPointer<vtkImageRFFT>::New(); 38 rfftFilter->SetInputConnection(lowPassFilter->GetOutputPort()); 39 rfftFilter->Update(); 40 41 vtkSmartPointer<vtkImageExtractComponents> ifftExtractReal = 42 vtkSmartPointer<vtkImageExtractComponents>::New(); 43 ifftExtractReal->SetInputConnection(rfftFilter->GetOutputPort()); 44 ifftExtractReal->SetComponents(0); 45 46 vtkSmartPointer<vtkImageCast> castFilter = 47 vtkSmartPointer<vtkImageCast>::New(); 48 castFilter->SetInputConnection(ifftExtractReal->GetOutputPort()); 49 castFilter->SetOutputScalarTypeToUnsignedChar(); 50 castFilter->Update(); 51 52 vtkSmartPointer<vtkImageActor> originalActor = 53 vtkSmartPointer<vtkImageActor>::New(); 54 originalActor->SetInputData(reader->GetOutput()); 55 56 vtkSmartPointer<vtkImageActor> erodedActor = 57 vtkSmartPointer<vtkImageActor>::New(); 58 erodedActor->SetInputData(castFilter->GetOutput()); 59 /// 60 double leftViewport[4] = { 0.0, 0.0, 0.5, 1.0 }; 61 double rightViewport[4] = { 0.5, 0.0, 1.0, 1.0 }; 62 vtkSmartPointer<vtkRenderer> leftRenderer = 63 vtkSmartPointer<vtkRenderer>::New(); 64 leftRenderer->AddActor(originalActor); 65 leftRenderer->SetViewport(leftViewport); 66 leftRenderer->SetBackground(1.0, 0, 0); 67 leftRenderer->ResetCamera(); 68 69 vtkSmartPointer<vtkRenderer> rightRenderer = 70 vtkSmartPointer<vtkRenderer>::New(); 71 rightRenderer->AddActor(erodedActor); 72 rightRenderer->SetViewport(rightViewport); 73 rightRenderer->SetBackground(1.0, 1.0, 1.0); 74 rightRenderer->ResetCamera(); 75 / 76 vtkSmartPointer<vtkRenderWindow> rw = 77 vtkSmartPointer<vtkRenderWindow>::New(); 78 rw->AddRenderer(leftRenderer); 79 rw->AddRenderer(rightRenderer); 80 rw->SetSize(640, 320); 81 rw->SetWindowName("ButterworthLowPassExample"); 82 83 vtkSmartPointer<vtkRenderWindowInteractor> rwi = 84 vtkSmartPointer<vtkRenderWindowInteractor>::New(); 85 vtkSmartPointer<vtkInteractorStyleImage> style = 86 vtkSmartPointer<vtkInteractorStyleImage>::New(); 87 rwi->SetInteractorStyle(style); 88 rwi->SetRenderWindow(rw); 89 rwi->Start(); 90 91 return 0; 92 }
vtkImageButterworthLowPass与理想低通滤波器的使用一样。为了便于比较,这里设置X和Y方向的截止频率时,与理想低通滤波器设置一致,下面是相应的执行结果:
从结果来看,巴特沃斯低通滤波器产生的图像更为平滑,不会出现振铃现象。