其他分享
首页 > 其他分享> > VTK_Learning_频域处理_低通滤波(理想+巴特沃兹)

VTK_Learning_频域处理_低通滤波(理想+巴特沃兹)

作者:互联网

1.低通滤波器

低通滤波是将频域图像中的高频部分滤除而通过低频部分。图像的边缘和噪声对应于频域图像中的高频部分,而低通滤波的作用即是减弱这部分的能量,从而达到图像平滑去噪的目的。

 2.理想低通滤波器

最简单的低通滤波器是理想低通滤波器,基本思想是给定一个频率阈值,将高于该阈值的所有部分设置为0,而低于该频率的部分保持不变。

理想是指该滤波器不能用电子元器件来实现,但是可以通过计算机来模拟。

在VTK中定义了理想低通滤波器,下面我们来看下怎么使用该滤波器来对图像进行低通滤波:

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
 
#include <vtkSmartPointer.h>
#include <vtkJPEGReader.h>
#include <vtkImageFFT.h>
#include <vtkImageIdealLowPass.h>
#include <vtkImageData.h>
#include <vtkImageRFFT.h>
#include <vtkImageCast.h>
#include <vtkImageExtractComponents.h>
#include <vtkImageActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
 
int main()
{
	vtkSmartPointer<vtkJPEGReader> reader =
		vtkSmartPointer<vtkJPEGReader>::New();
	reader->SetFileName("lena.jpg");
	reader->Update();
 
	vtkSmartPointer<vtkImageFFT> fftFilter =
		vtkSmartPointer<vtkImageFFT>::New();
	fftFilter->SetInputConnection(reader->GetOutputPort());
	fftFilter->Update();
 
	vtkSmartPointer<vtkImageIdealLowPass> lowPassFilter =
		vtkSmartPointer<vtkImageIdealLowPass>::New();
	lowPassFilter->SetInputConnection(fftFilter->GetOutputPort());
	lowPassFilter->SetXCutOff(0.05);
	lowPassFilter->SetYCutOff(0.05);
	lowPassFilter->Update();
 
	vtkSmartPointer<vtkImageRFFT> rfftFilter =
		vtkSmartPointer<vtkImageRFFT>::New();
	rfftFilter->SetInputConnection(lowPassFilter->GetOutputPort());
	rfftFilter->Update();
 
	vtkSmartPointer<vtkImageExtractComponents> ifftExtractReal =
		vtkSmartPointer<vtkImageExtractComponents>::New();
	ifftExtractReal->SetInputConnection(rfftFilter->GetOutputPort());
	ifftExtractReal->SetComponents(0);
 
	vtkSmartPointer<vtkImageCast> castFilter =
		vtkSmartPointer<vtkImageCast>::New();
	castFilter->SetInputConnection(ifftExtractReal->GetOutputPort());
	castFilter->SetOutputScalarTypeToUnsignedChar();
	castFilter->Update();
 
	vtkSmartPointer<vtkImageActor> originalActor =
		vtkSmartPointer<vtkImageActor>::New();
	originalActor->SetInputData(reader->GetOutput());
 
	vtkSmartPointer<vtkImageActor> erodedActor =
		vtkSmartPointer<vtkImageActor>::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<vtkRenderer> leftRenderer =
		vtkSmartPointer<vtkRenderer>::New();
	leftRenderer->AddActor(originalActor);
	leftRenderer->ResetCamera();
	leftRenderer->SetViewport(leftViewport);
	leftRenderer->SetBackground(1.0, 0, 0);
 
	vtkSmartPointer<vtkRenderer> rightRenderer =
		vtkSmartPointer<vtkRenderer>::New();
	rightRenderer->AddActor(erodedActor);
	rightRenderer->SetViewport(rightViewport);
	rightRenderer->ResetCamera();
	rightRenderer->SetBackground(1.0, 1.0, 1.0);
 
	vtkSmartPointer<vtkRenderWindow> renderWindow =
		vtkSmartPointer<vtkRenderWindow>::New();
	renderWindow->AddRenderer(rightRenderer);
	renderWindow->AddRenderer(leftRenderer);
	renderWindow->SetSize(640, 320);
	renderWindow->SetWindowName("Frequency_IdealLowPassFilter");
 
	vtkSmartPointer<vtkRenderWindowInteractor> interactor =
		vtkSmartPointer<vtkRenderWindowInteractor>::New();
	interactor->SetRenderWindow(renderWindow);
	renderWindow->Render();
	interactor->Start();
 
	return 0;
}

首先读入一副图像,通过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.

其使用过程如下:
 

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
 
#include <vtkSmartPointer.h>
#include <vtkJPEGReader.h>
#include <vtkImageFFT.h>
#include <vtkImageButterworthLowPass.h>
#include <vtkImageRFFT.h>
#include <vtkImageExtractComponents.h>
#include <vtkImageCast.h>
#include <vtkImageActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
 
int main()
{
	vtkSmartPointer<vtkJPEGReader> reader =
		vtkSmartPointer<vtkJPEGReader>::New();
	reader->SetFileName("lena.jpg");
	reader->Update();
 
	vtkSmartPointer<vtkImageFFT> fftFilter =
		vtkSmartPointer<vtkImageFFT>::New();
	fftFilter->SetInputConnection(reader->GetOutputPort());
	fftFilter->Update();
 
	vtkSmartPointer<vtkImageButterworthLowPass> lowPassFilter =
		vtkSmartPointer<vtkImageButterworthLowPass>::New();
	lowPassFilter->SetInputConnection(fftFilter->GetOutputPort());
	lowPassFilter->SetXCutOff(0.05);
	lowPassFilter->SetYCutOff(0.05);
	lowPassFilter->Update();
 
	vtkSmartPointer<vtkImageRFFT> rfftFilter =
		vtkSmartPointer<vtkImageRFFT>::New();
	rfftFilter->SetInputConnection(lowPassFilter->GetOutputPort());
	rfftFilter->Update();
 
	vtkSmartPointer<vtkImageExtractComponents> ifftExtractReal =
		vtkSmartPointer<vtkImageExtractComponents>::New();
	ifftExtractReal->SetInputConnection(rfftFilter->GetOutputPort());
	ifftExtractReal->SetComponents(0);
 
	vtkSmartPointer<vtkImageCast> castFilter =
		vtkSmartPointer<vtkImageCast>::New();
	castFilter->SetInputConnection(ifftExtractReal->GetOutputPort());
	castFilter->SetOutputScalarTypeToUnsignedChar();
	castFilter->Update();
	////////////////////////////////////////////////////
	vtkSmartPointer<vtkImageActor> originalActor =
		vtkSmartPointer<vtkImageActor>::New();
	originalActor->SetInputData(reader->GetOutput());
 
	vtkSmartPointer<vtkImageActor> erodedActor =
		vtkSmartPointer<vtkImageActor>::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<vtkRenderer> leftRenderer =
		vtkSmartPointer<vtkRenderer>::New();
	leftRenderer->AddActor(originalActor);
	leftRenderer->SetViewport(leftViewport);
	leftRenderer->SetBackground(1.0, 0, 0);
	leftRenderer->ResetCamera();
 
	vtkSmartPointer<vtkRenderer> rightRenderer =
		vtkSmartPointer<vtkRenderer>::New();
	rightRenderer->AddActor(erodedActor);
	rightRenderer->SetViewport(rightViewport);
	rightRenderer->SetBackground(1.0, 1.0, 1.0);
	rightRenderer->ResetCamera();
	/////////
	vtkSmartPointer<vtkRenderWindow> rw =
		vtkSmartPointer<vtkRenderWindow>::New();
	rw->AddRenderer(leftRenderer);
	rw->AddRenderer(rightRenderer);
	rw->SetSize(640, 320);
	rw->SetWindowName("ButterworthLowPassExample");
 
	vtkSmartPointer<vtkRenderWindowInteractor> rwi =
		vtkSmartPointer<vtkRenderWindowInteractor>::New();
	vtkSmartPointer<vtkInteractorStyleImage> style =
		vtkSmartPointer<vtkInteractorStyleImage>::New();
	rwi->SetInteractorStyle(style);
	rwi->SetRenderWindow(rw);
	rwi->Start();
 
	return 0;
}

vtkImageButterworthLowPass与理想低通滤波器的使用一样。为了便于比较,这里设置X和Y方向的截止频率时,与理想低通滤波器设置一致,下面是相应的执行结果:

从结果来看,巴特沃斯低通滤波器产生的图像更为平滑,不会出现振铃现象。

 


 

 

江南又旧雨 发布了164 篇原创文章 · 获赞 13 · 访问量 1万+ 私信 关注

标签:1.0,VTK,vtkSmartPointer,沃兹,图像,New,include,leftRenderer,通滤波
来源: https://blog.csdn.net/weixin_38293453/article/details/104612570