主要内容

imbilatfilt

使用高斯核的图像双边滤波

说明

J = imbilatfilt(I) 对灰度或 RGB 图像 I 应用边缘保持高斯双边滤波器。

J = imbilatfilt(I,degreeOfSmoothing) 指定平滑量。当 degreeOfSmoothing 是小值时,imbilatfilt 会对小方差邻域(均匀区域)进行平滑处理,但不会对大方差邻域进行平滑处理,例如强边缘。当 degreeOfSmoothing 的值增大时,imbilatfilt 同时对均匀区域和具有较大方差的邻域进行平滑处理。

示例

J = imbilatfilt(I,degreeOfSmoothing,spatialSigma) 还指定空间高斯平滑核的标准差 spatialSigmaspatialSigma 的值越大,更远相邻像素的权重就越大,从而有效地增大了邻域大小。

J = imbilatfilt(___,Name=Value) 使用名称-值参量来更改双边滤波器的行为。

示例

全部折叠

读取并显示灰度图像。观察天空区域中的水平条带伪影。

I = imread('cameraman.tif');
imshow(I)

Figure contains an axes object. The hidden axes object contains an object of type image.

检查天空区域的一个图像块。计算该图像块的方差,该方差逼近噪声的方差。

patch = imcrop(I,[170, 35, 50 50]);
imshow(patch)

Figure contains an axes object. The hidden axes object contains an object of type image.

patchVar = std2(patch)^2;

使用双边滤波对图像进行滤波。将平滑度设置为大于噪声的方差。

DoS = 2*patchVar;
J = imbilatfilt(I,DoS);
imshow(J)
title(['Degree of Smoothing: ',num2str(DoS)])

Figure contains an axes object. The hidden axes object with title Degree of Smoothing: 51.9395 contains an object of type image.

条带伪影减少,但并未消除。为了改进平滑效果,将 spatialSigma 的值增加到 2,以便较远的相邻像素对高斯平滑核的权重更大。这会有效增大双边滤波器的空间范围。

K = imbilatfilt(I,DoS,2);
imshow(K)
title(['Degree of Smoothing: ',num2str(DoS),', Spatial Sigma: 2'])

Figure contains an axes object. The hidden axes object with title Degree of Smoothing: 51.9395, Spatial Sigma: 2 contains an object of type image.

天空中的条带伪影已成功去除。强边缘(如男人的轮廓)和纹理区域(如图像前景中的草地)的锐度得以保留。

读取一个 RGB 图像。

imRGB = imread("coloredChips.png");
imshow(imRGB)

Figure contains an axes object. The hidden axes object contains an object of type image.

将图像转换为 L*a*b* 颜色空间,以便双边滤波器对感知上相似的颜色进行平滑处理。

imLAB = rgb2lab(imRGB);

提取不含锐边的图像块。计算 L*a*b* 颜色空间中距原点的欧几里德距离的方差。

patch = imcrop(imLAB,[34,71,60,55]);
patchSq = patch.^2;
edist = sqrt(sum(patchSq,3));
patchVar = std2(edist).^2;

在 L*a*b* 颜色空间中使用双边滤波对图像进行滤波。将 DegreeOfSmoothing 设置为高于图像块的方差的值。

DoS = 2*patchVar;
smoothedLAB = imbilatfilt(imLAB,DoS);

将图像转换回 RGB 颜色空间,并显示平滑处理后的图像。

smoothedRBG = lab2rgb(smoothedLAB,"Out","uint8");
montage({imRGB,smoothedRBG})
title("Original Image vs. Filtered Image with Degree of Smoothing: "+num2str(DoS))

Figure contains an axes object. The hidden axes object with title Original Image vs. Filtered Image with Degree of Smoothing: 7.9771 contains an object of type image.

塑料片和黑色笔的颜色看起来更均匀,但桌子上的水平纹理仍可见。增大滤波器的空间范围,使滤波器的有效邻域跨水平纹理之间的空间(此距离约为七个像素)。同时增大 DegreeOfSmoothing 以对这些区域进行更强的平滑处理。

DoS2 = 4*patchVar;
sigma = 7;
smoothedLAB2 = imbilatfilt(imLAB,DoS2,sigma);
smoothedRBG2 = lab2rgb(smoothedLAB2,"Out","uint8");
montage({imRGB,smoothedRBG2})
title("Original Image vs. Filtered Image with Degree of Smoothing: "+num2str(DoS)+ ...
    " and Spatial Sigma: "+sigma)

Figure contains an axes object. The hidden axes object with title Original Image vs. Filtered Image with Degree of Smoothing: 7.9771 and Spatial Sigma: 7 contains an object of type image.

木桌的颜色在使用更大邻域和更大平滑度后更加均匀。塑料片和笔的边缘锐度得以保留。

输入参数

全部折叠

要滤波的图像,指定为大小为 m×n 的二维灰度图像,或大小为 m×n×3 的二维彩色图像。

数据类型: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32

平滑度,指定为正数。degreeOfSmoothing 的默认值取决于图像 I 的数据类型,并计算为 0.01*diff(getrangefromclass(I)).^2。例如,对于数据类型为 uint8 的图像,默认平滑度为 650.25,对于像素值在 [0, 1] 范围内的数据类型为 double 的图像,默认值为 0.01

空间高斯平滑核的标准差,指定为正数。

名称-值参数

全部折叠

将可选参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但对各个参量对组的顺序没有要求。

示例: imbilatfilt(I,NeighborhoodSize=7) 使用 7×7 像素邻域对图像 I 执行双边滤波。

如果使用的是 R2021a 之前的版本,请使用逗号分隔每个名称和值,并用引号将 Name 引起来。

示例: imbilatfilt(I,"NeighborhoodSize",7);

邻域大小,指定为奇数值正整数。默认情况下,邻域大小为 2*ceil(2*SpatialSigma)+1 像素。

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

填充,指定为以下值之一。

描述
"replicate"数组边界之外的输入数组值假定为等于最近的数组边界值。
"symmetric"

数组边界之外的输入数组值是通过沿数组边界对数组进行镜面反射得到。

数值标量,x对图像边界外的输入图像值赋予值 x

示例: Padding="symmetric"

示例: Padding=128

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char | string

输出参量

全部折叠

滤波后的图像,以与输入图像 I 大小和数据类型相同的数值数组形式返回。

提示

  • degreeOfSmoothing 的值对应于双边滤波器的值域高斯核的方差 [1]。值域高斯应用于一个像素值与其相邻像素值的欧几里德距离。

  • 为了平滑处理 RGB 图像的感知上接近的颜色,在应用双边滤波器之前,使用 rgb2lab 将图像转换为 CIE L*a*b* 空间。要查看结果,请使用 lab2rgb 将滤波后的图像转换为 RGB。

  • 增大 spatialSigma 会增大 NeighborhoodSize,从而增大滤波器执行时间。您可以指定较小的 NeighborhoodSize 以牺牲准确性换取更快的执行时间。

参考

[1] Tomasi, C., and R. Manduchi. "Bilateral Filtering for Gray and Color Images". Proceedings of the 1998 IEEE® International Conference on Computer Vision. Bombay, India. Jan 1998, pp. 836–846.

扩展功能

全部展开

版本历史记录

在 R2018a 中推出

全部展开