Main Content

将高斯平滑滤波器应用于图像

此示例说明如何使用 imgaussfilt 对图像应用不同高斯平滑滤波器。高斯平滑滤波器通常用于降低噪声。

将图像读入工作区。

I = imread('cameraman.tif');

用标准差递增的各向同性高斯平滑核对图像进行滤波。高斯滤波器通常为各向同性,也就是说,它们在两个维度上具有相同的标准差。通过为 sigma 指定标量值,可以用各向同性高斯滤波器对图像进行滤波。

Iblur1 = imgaussfilt(I,2);
Iblur2 = imgaussfilt(I,4);
Iblur3 = imgaussfilt(I,8);

显示原始图像和所有滤波后的图像。

figure
imshow(I)
title('Original image')

Figure contains an axes object. The axes object with title Original image contains an object of type image.

figure
imshow(Iblur1)
title('Smoothed image, \sigma = 2')

Figure contains an axes object. The axes object with title S m o o t h e d blank i m a g e , blank sigma blank = blank 2 contains an object of type image.

figure
imshow(Iblur2)
title('Smoothed image, \sigma = 4')

Figure contains an axes object. The axes object with title S m o o t h e d blank i m a g e , blank sigma blank = blank 4 contains an object of type image.

figure
imshow(Iblur3)
title('Smoothed image, \sigma = 8')

Figure contains an axes object. The axes object with title S m o o t h e d blank i m a g e , blank sigma blank = blank 8 contains an object of type image.

用各向异性高斯平滑核对图像进行滤波。imgaussfilt 允许高斯核在行和列维度上有不同标准差。这些称为轴对齐的各向异性高斯滤波器。使用各向异性滤波器时,请为 sigma 指定二元素向量。

IblurX1 = imgaussfilt(I,[4 1]);
IblurX2 = imgaussfilt(I,[8 1]);
IblurY1 = imgaussfilt(I,[1 4]);
IblurY2 = imgaussfilt(I,[1 8]);

显示滤波后的图像。

figure
imshow(IblurX1)
title('Smoothed image, \sigma_x = 4, \sigma_y = 1')

Figure contains an axes object. The axes object with title S m o o t h e d blank i m a g e , blank sigma indexOf x baseline blank = blank 4 , blank sigma indexOf y baseline blank = blank 1 contains an object of type image.

figure
imshow(IblurX2)
title('Smoothed image, \sigma_x = 8, \sigma_y = 1')

Figure contains an axes object. The axes object with title S m o o t h e d blank i m a g e , blank sigma indexOf x baseline blank = blank 8 , blank sigma indexOf y baseline blank = blank 1 contains an object of type image.

figure
imshow(IblurY1)
title('Smoothed image, \sigma_x = 1, \sigma_y = 4')

Figure contains an axes object. The axes object with title S m o o t h e d blank i m a g e , blank sigma indexOf x baseline blank = blank 1 , blank sigma indexOf y baseline blank = blank 4 contains an object of type image.

figure
imshow(IblurY2)
title('Smoothed image, \sigma_x = 1, \sigma_y = 8')

Figure contains an axes object. The axes object with title S m o o t h e d blank i m a g e , blank sigma indexOf x baseline blank = blank 1 , blank sigma indexOf y baseline blank = blank 8 contains an object of type image.

消除原始图像的天空区域中可见的水平条带。各向异性高斯滤波器可以消除图像中的水平或垂直特征。提取图像的一部分天空区域,并使用沿 X 轴(递增列的方向)具有较高标准差的高斯滤波器。

I_sky = imadjust(I(20:50,10:70));
IblurX1_sky = imadjust(IblurX1(20:50,10:70));

显示原始的天空区块的以及经过滤波的版本。

figure
imshow(I_sky), title('Sky in original image')

Figure contains an axes object. The axes object with title Sky in original image contains an object of type image.

figure
imshow(IblurX1_sky), title('Sky in filtered image')

Figure contains an axes object. The axes object with title Sky in filtered image contains an object of type image.