1.理想高通滤波器
高通滤波与低通滤波正好相反,是频域图像的高频部分通过而抑制低频部分。在图像中图像的边缘对应高频分量,因此高通滤波的效果是图像锐化。同样最简单的高通滤波器是理想高通滤波器。通过设置一个频率阈值,将高于该阈值的频率部分通过,而低于阈值的低频部分设置为0。
VTK中理想高通滤波的实例如下:
/* ******理想高通滤波********** */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
vtkSmartPointer reader =vtkSmartPointer::New();
reader->SetFileName("data\\lena-gray.jpg");
reader->Update();
vtkSmartPointer fftFilter = vtkSmartPointer::New();//将图像域转换到频域空间
fftFilter->SetInputConnection(reader->GetOutputPort());
fftFilter->Update();
vtkSmartPointer highPassFilter = vtkSmartPointer::New();
highPassFilter->SetInputConnection(fftFilter->GetOutputPort());//定义图像高通滤波对象
highPassFilter->SetXCutOff(0.1);//设置X和Y方向的截止频率
highPassFilter->SetYCutOff(0.1);
highPassFilter->Update();
vtkSmartPointer rfftFilter = vtkSmartPointer::New();//将处理后的图像转换到空域中
rfftFilter->SetInputConnection(highPassFilter->GetOutputPort());
rfftFilter->Update();
vtkSmartPointer ifftExtractReal = vtkSmartPointer::New();
ifftExtractReal->SetInputConnection(rfftFilter->GetOutputPort());
ifftExtractReal->SetComponents(0);//提取实部分量
vtkSmartPointer castFilter = vtkSmartPointer::New();//数据类型转换
castFilter->SetInputConnection(ifftExtractReal->GetOutputPort());
castFilter->SetOutputScalarTypeToUnsignedChar();
castFilter->Update();
/////////////////////////////////////////////////////////
vtkSmartPointer originalActor = vtkSmartPointer::New();
originalActor->SetInputData(reader->GetOutput());
vtkSmartPointer erodedActor = vtkSmartPointer::New();
erodedActor->SetInputData(castFilter->GetOutput());
/////////////////////////////
double leftViewport[4] = { 0.0, 0.0, 0.5, 1.0 };
double rightViewport[4] = { 0.5, 0.0, 1.0, 1.0 };
vtkSmartPointer leftRenderer = vtkSmartPointer::New();
leftRenderer->AddActor(originalActor);
leftRenderer->SetViewport(leftViewport);
leftRenderer->SetBackground(1.0, 1.0, 1.0);
leftRenderer->ResetCamera();
vtkSmartPointer rightRenderer = vtkSmartPointer::New();
rightRenderer->AddActor(erodedActor);
rightRenderer->SetViewport(rightViewport);
rightRenderer->SetBackground(1.0, 1.0, 1.0);
rightRenderer->ResetCamera();
/////////////////
vtkSmartPointer rw = vtkSmartPointer::New();
rw->SetSize(640, 320);
rw->AddRenderer(leftRenderer);
rw->AddRenderer(rightRenderer);
rw->SetWindowName("IdealHighPassExample");
vtkSmartPointer rwi = vtkSmartPointer::New();
vtkSmartPointer style = vtkSmartPointer::New();
rwi->SetInteractorStyle(style);
rwi->SetRenderWindow(rw);
rwi->Start();
return 0;
}
运行结果如下:
低通-滤波;高通-锐化(从结果看出高通滤波后图像得到锐化处理,图像中仅剩下边缘。)同低通滤波一样,首先将读入图像通过vtkImageFFT转换到频域空间,定义vtkImageIdealHighPass对象,并通过SetXCutOff ()和SetYCutOff() 设置X和Y方向的截止频率。然后通过vtkImageRFFT将处理后的图像转换到空域中,得到高通滤波图像。为了显示的需要,还需要提取图像分量和数据类型的转换。
2.巴特沃兹高通滤波
理想高通滤波器不能通过电子元器件来实现,而且存在振铃现象。在实际中最常使用的高通滤波器是巴特沃斯高通滤波器。该滤波器的转移函数是:
D(u,v)表示频域中点到频域平面的距离,是截止频率。当D(u,v)大于时,对应的H(u,v)逐渐接近1,从而使得高频部分得以通过;而当D(u,v)小于时,H(u,v)逐渐接近0,实现低频部分过滤。巴特沃斯高通滤波器在VTK中对应vtkImageButterworthHighPass类。
下面代码说明了vtkImageButterworthHighPass对图像进行高通滤波:
/*******************巴特沃特高斯高通滤波***********************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char* argv[])
{
vtkSmartPointer reader = vtkSmartPointer::New();
reader->SetFileName("lena.jpg");
reader->Update();
vtkSmartPointer fftFilter = vtkSmartPointer::New();
fftFilter->SetInputConnection(reader->GetOutputPort());
fftFilter->Update();
vtkSmartPointer highPassFilter = vtkSmartPointer::New();
highPassFilter->SetInputConnection(fftFilter->GetOutputPort());
highPassFilter->SetXCutOff(0.1);
highPassFilter->SetYCutOff(0.1);//x\y轴的截止频率
highPassFilter->Update();
vtkSmartPointer rfftFilter = vtkSmartPointer::New();// 转换回空域中
rfftFilter->SetInputConnection(highPassFilter->GetOutputPort());
rfftFilter->Update();
vtkSmartPointer ifftExtractReal = vtkSmartPointer::New();
ifftExtractReal->SetInputConnection(rfftFilter->GetOutputPort());
ifftExtractReal->SetComponents(0);//提取复数中的实部
vtkSmartPointer castFilter = vtkSmartPointer::New();//数据转换为unsigned char类型
castFilter->SetInputConnection(ifftExtractReal->GetOutputPort());
castFilter->SetOutputScalarTypeToUnsignedChar();
castFilter->Update();
////////////////////////////////////////////////////////////////////
vtkSmartPointer originalActor = vtkSmartPointer::New();
originalActor->SetInputData(reader->GetOutput());
vtkSmartPointer erodedActor = vtkSmartPointer::New();
erodedActor->SetInputData(castFilter->GetOutput());
//////////////////////
double leftViewport[4] = { 0.0, 0.0, 0.5, 1.0 };
double rightViewport[4] = { 0.5, 0.0, 1.0, 1.0 };
vtkSmartPointer leftRenderer = vtkSmartPointer::New();
leftRenderer->AddActor(originalActor);
leftRenderer->ResetCamera();
leftRenderer->SetViewport(leftViewport);
leftRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer rightRenderer = vtkSmartPointer::New();
rightRenderer->AddActor(erodedActor);
rightRenderer->SetViewport(rightViewport);
rightRenderer->SetBackground(1.0, 1.0, 1.0);
rightRenderer->ResetCamera();
////////////////////////
vtkSmartPointer rw = vtkSmartPointer::New();
rw->AddRenderer(leftRenderer);
rw->AddRenderer(rightRenderer);
rw->SetSize(640, 320);
rw->Render();
rw->SetWindowName("Frequency_ButterworthHighPass");
/////////////////////
vtkSmartPointer rwi = vtkSmartPointer::New();
vtkSmartPointer style = vtkSmartPointer::New();
rwi->SetInteractorStyle(style);
rwi->SetRenderWindow(rw);
rwi->Start();
return 0;
}
运行结果如下:
解释说明:vtkImageButterworthHighPass与理想高通滤波使用方法一致。需要设置X和Y轴的截止频率,为了便于比较,其截止频域与理想高通滤波设置一致。
参考资料:
1.《The Visualization Toolkit – AnObject-Oriented Approach To 3D Graphics (4th Edition)》
2. 张晓东, 罗火灵. VTK图形图像开发进阶[M]. 机械工业出版社, 2015.
所用软件:vtk7.0+visual studio 2013
注:此文知识学习笔记,仅记录完整程序和实现结果,具体原理参见:
https://blog.csdn.net/www_doling_net/article/details/8541534
https://blog.csdn.net/shenziheng1/article/category/6114053/4