VTK笔记-高斯滤波器-vtkImageGaussianSmooth类

高斯模糊

  高斯模糊(Gaussian Blur),也叫高斯平滑,是在Adobe Photoshop、GIMP以及Paint.NET等图像处理软件中广泛使用的处理效果,通常用它来减少图像噪声以及降低细节层次。这种模糊技术生成的图像,其视觉效果就像是经过一个半透明屏幕在观察图像,这与镜头焦外成像效果散景以及普通照明阴影中的效果都明显不同。高斯平滑也用于计算机视觉算法中的预先处理阶段,以增强图像在不同比例大小下的图像效果(参见尺度空间表示以及尺度空间实现)。 从数学的角度来看,图像的高斯模糊过程就是图像与正态分布做卷积。由于正态分布又叫作高斯分布,所以这项技术就叫作高斯模糊。图像与圆形方框模糊做卷积将会生成更加精确的焦外成像效果。由于高斯函数的傅立叶变换是另外一个高斯函数,所以高斯模糊对于图像来说就是一个低通滤波器

vtkImageGaussianSmooth

  vtkImageGaussianSmooth类实现输入图像与高斯函数的卷积,支持从一维到三维数据卷积处理。
在这里插入图片描述

接口

  设置/获取高斯分布的标准偏差(以像素为单位);

vtkSetVector3Macro(StandardDeviations, double);
void SetStandardDeviation(double std) {
    
     this->SetStandardDeviations(std, std, std); }
void SetStandardDeviations(double a, double b) {
    
     this->SetStandardDeviations(a, b, 0.0); }
vtkGetVector3Macro(StandardDeviations, double);  
void SetStandardDeviation(double a, double b) {
    
     this->SetStandardDeviations(a, b, 0.0); }
void SetStandardDeviation(double a, double b, double c) {
    
     this->SetStandardDeviations(a, b, c); }

  设置/获取高斯曲线的半径因子(无单位);

vtkSetVector3Macro(RadiusFactors, double);
void SetRadiusFactors(double f, double f2) {
    
     this->SetRadiusFactors(f, f2, 1.5); }
void SetRadiusFactor(double f) {
    
     this->SetRadiusFactors(f, f, f); }
vtkGetVector3Macro(RadiusFactors, double);

  设置/获取此过滤器的维度;

vtkSetMacro(Dimensionality, int);
vtkGetMacro(Dimensionality, int);

  简单示例:

vktNew<vtkImageGaussianSmooth> gaussianSmoothFilter;
gaussianSmoothFilter->SetInputData(data_ptr);
gaussianSmoothFilter->SetDimensionality(dim);
gaussianSmoothFilter->SetRadiusFactor(radius);
gaussianSmoothFilter->SetStandardDeviation(dev);
gaussianSmoothFilter->Update();

示例

二值椭圆图像的高斯平滑

vtkNew<vtkNamedColors> colors;

// Create an image
vtkNew<vtkImageEllipsoidSource> source;
source->SetWholeExtent(0, 20, 0, 20, 0, 0);
source->SetCenter(10, 10, 0);
source->SetRadius(3, 4, 0);
source->Update();

vtkNew<vtkImageGaussianSmooth> gaussianSmoothFilter;
gaussianSmoothFilter->SetInputConnection(source->GetOutputPort());
gaussianSmoothFilter->Update();

// Create actors
vtkNew<vtkImageActor> originalActor;
originalActor->GetMapper()->SetInputConnection(source->GetOutputPort());

vtkNew<vtkImageActor> smoothedActor;
smoothedActor->GetMapper()->SetInputConnection(gaussianSmoothFilter->GetOutputPort());

// Define viewport ranges
// (xmin, ymin, xmax, ymax)
double originalViewport[4] = {
    
     0.0, 0.0, 0.5, 1.0 };
double smoothedViewport[4] = {
    
     0.5, 0.0, 1.0, 1.0 };

// Setup renderers
vtkNew<vtkRenderer> originalRenderer;
originalRenderer->SetViewport(originalViewport);
originalRenderer->AddActor(originalActor);
originalRenderer->ResetCamera();
originalRenderer->SetBackground(colors->GetColor3d("CornflowerBlue").GetData());

vtkNew<vtkRenderer> gradientMagnitudeRenderer;
gradientMagnitudeRenderer->SetViewport(smoothedViewport);
gradientMagnitudeRenderer->AddActor(smoothedActor);
gradientMagnitudeRenderer->ResetCamera();
gradientMagnitudeRenderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());

vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetSize(600, 300);
renderWindow->AddRenderer(originalRenderer);
renderWindow->AddRenderer(gradientMagnitudeRenderer);
renderWindow->SetWindowName("ImageGaussianSmooth");

vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
vtkNew<vtkInteractorStyleImage> style;

renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->Render();
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();

在这里插入图片描述

二维png图像的图像模糊

// Read the image
vtkNew<vtkImageReader2Factory> readerFactory;
vtkSmartPointer<vtkImageReader2> reader;
reader.TakeReference(readerFactory->CreateImageReader2("G:\\Data\\Gourds.png"));
reader->SetFileName("G:\\Data\\Gourds.png");
reader->Update();

// Process the
vtkNew<vtkImageCast> cast;
cast->SetInputConnection(reader->GetOutputPort());
cast->SetOutputScalarTypeToFloat();

vtkNew<vtkImageGaussianSmooth> filter;
filter->SetDimensionality(2);
filter->SetInputConnection(cast->GetOutputPort());
filter->SetStandardDeviations(4.0, 4.0);
filter->SetRadiusFactors(2.0, 2.0);

// Create actors
vtkNew<vtkNamedColors> colors;

vtkNew<vtkImageActor> originalActor;
originalActor->GetMapper()->SetInputConnection(reader->GetOutputPort());

vtkNew<vtkImageActor> filteredActor;
filteredActor->GetMapper()->SetInputConnection(filter->GetOutputPort());

// Define viewport ranges
// (xmin, ymin, xmax, ymax)
double originalViewport[4] = {
    
     0.0, 0.0, 0.5, 1.0 };
double filteredViewport[4] = {
    
     0.5, 0.0, 1.0, 1.0 };

// Setup renderers
vtkNew<vtkRenderer> originalRenderer;
originalRenderer->SetViewport(originalViewport);
originalRenderer->AddActor(originalActor);
originalRenderer->ResetCamera();
originalRenderer->SetBackground(colors->GetColor3d("SlateGray").GetData());

vtkNew<vtkRenderer> filteredRenderer;
filteredRenderer->SetViewport(filteredViewport);
filteredRenderer->AddActor(filteredActor);
filteredRenderer->ResetCamera();
filteredRenderer->SetBackground(colors->GetColor3d("LightSlateGray").GetData());

vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetSize(600, 300);
renderWindow->SetWindowName("GaussianSmooth");
renderWindow->AddRenderer(originalRenderer);
renderWindow->AddRenderer(filteredRenderer);

vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
vtkNew<vtkInteractorStyleImage> style;

renderWindowInteractor->SetInteractorStyle(style);

renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->Render();
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();

在这里插入图片描述

参考资料

1.高斯模糊
2.高斯模糊的算法
3.ImageGaussianSmooth
4.GaussianSmooth
5.图像处理基础(4):高斯滤波器详解

猜你喜欢

转载自blog.csdn.net/liushao1031177/article/details/118912129