主要内容

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

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

将图像读入工作区。

I = imread("cameraman.tif");

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

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

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

imshow(I)
title("Original image")

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

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

Figure contains an axes object. The hidden axes object with title Smoothed image, sigma blank = blank 2 contains an object of type image.

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

Figure contains an axes object. The hidden axes object with title Smoothed image, sigma blank = blank 4 contains an object of type image.

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

Figure contains an axes object. The hidden axes object with title Smoothed image, 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]);

显示滤波后的图像。

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

Figure contains an axes object. The hidden axes object with title Smoothed image, sigma indexOf x baseline blank = blank 4 , blank sigma indexOf y baseline blank = blank 1 contains an object of type image.

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

Figure contains an axes object. The hidden axes object with title Smoothed image, sigma indexOf x baseline blank = blank 8 , blank sigma indexOf y baseline blank = blank 1 contains an object of type image.

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

Figure contains an axes object. The hidden axes object with title Smoothed image, sigma indexOf x baseline blank = blank 1 , blank sigma indexOf y baseline blank = blank 4 contains an object of type image.

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

Figure contains an axes object. The hidden axes object with title Smoothed image, 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));

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

imshow(I_sky)
title("Sky in original image")

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

imshow(IblurX1_sky)
title("Sky in filtered image")

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