1 Median Filtering 中值滤波
The function medfilt1 implements one-dimensional median filtering, a nonlinear technique that applies a sliding window to a sequence.
medfilt1是中值滤波的一维实现,是一种对序列进行滑动窗口处理类的非线性技术。
The median filter replaces the center value in the window with the median value of all the points within the window [5].
在窗口内,中值滤波将中心点数值用所有点的中值来替代。
In computing this median, medfilt1 assumes zeros beyond the input points.
计算中值时,medfilt1假设输入点以外都是0.
When the number of elements n in the window is even, medfilt1 sorts the numbers, then takes the average of the n/2 and n/2 + 1 elements.
当序列的窗口内点数n为偶数个时,medfilt1首先对点号进行排序,然后取n/2和n/2+1个元素的平均。
Two simple examples with fourth- and third-order median filters are
下面给出四阶和三阶中值滤波的例子
medfilt1([4 3 5 2 8 9 1],4)
ans =
1.500 3.500 3.500 4.000 6.500 5.000 4.500
首先看四阶的,序列为[4 3 5 2 8 9 1]
i=1时,窗口=[0 0 4 3] 第n/2个=0 第n/2+1个=3 均值为1.5
i=2时,窗口=[0 4 3 5] 第n/2个=3 第n/2+1个=4 均值为3.5
i=3时,窗口=[4 3 5 2] 第n/2个=3 第n/2+1个=4 均值为3.5
i=4时,窗口=[3 5 2 8] 第n/2个=3 第n/2+1个=5 均值为4
medfilt1([4 3 5 2 8 9 1],3)
ans =
3 4 3 5 8 8 1
接下来看三阶的,序列为[4 3 5 2 8 9 1]
当i=1时,窗口=[0 4 3]中值为3
当i=2时,窗口=[4 3 5]中值为4
当i=3时,窗口=[3 5 2]中值为3
看完这个解释,我终于理解了中值滤波和均值滤波的区别
中值滤波就是取中数,均值滤波则完全是取均值。
你学费了吗?
Matlab甚至还为你准备好了参考文献:
[1] Kay, S.M. Modern Spectral Estimation. Englewood Cliffs, NJ: Prentice Hall, 1988.
[2] Oppenheim, A.V., and R.W. Schafer. Discrete-Time Signal Processing. Englewood Cliffs, NJ: Prentice Hall, 1989.
[3] Oppenheim, A.V., and R.W. Schafer. Discrete-Time Signal Processing. Englewood Cliffs, NJ: Prentice Hall, 1975, Section 10.5.3.
[4] Parks, T.W., and C.S. Burrus. Digital Filter Design. New York: John Wiley & Sons, 1987.
[5] Pratt,W.K. Digital Image Processing. New York: John Wiley & Sons, 1991.
-------分隔线--------
上述为一维中值滤波的实现,看完算法后,我甚至都不想在举例说明了,因为确实是懂了。
就是因为取得是中值,所以就会消除尖刺噪声,但同时也会有保留边界的优点,且幅值不会下降(均值滤波就会有这个问题,因为是平均嘛)。
-------分隔线--------
下面来看看二维中值滤波
medfilt2
2 2-D median filtering 二维中值滤波
Note The syntax medfilt2(A,[M N],(Mb Nb],...) has been removed.
注意:上述语法已经被移除,不再支持了。要使用下面的语法格式。
Syntax
B = medfilt2(A, [m n])
B = medfilt2(A)
B = medfilt2(A, 'indexed', ...)
B = medfilt2(..., padopt)
Description 使用指南
Median filtering is a nonlinear operation often used in image processing to reduce "salt and pepper" noise. A median filter is more effective than convolution when the goal is to simultaneously reduce noise and preserve edges.
中值滤波是一种非线性滤波器,在图像处理中常用于消除“盐和胡椒粉”这样的噪声。
如果你不知道啥是盐和胡椒粉,请看下图:
哈哈,说白了就是尖刺噪声。
B = medfilt2(A, [m n]) performs median filtering of the matrix A in two dimensions.
B = medfilt2(A, [m n]) 为对二维矩阵A进行中值滤波。
Each output pixel contains the median value in the m-by-n neighborhood around the corresponding pixel in the input image.
输出的每个像素点都处理为在其周围m行n列的邻近点阵的中值。
medfilt2 pads the image with 0s on the edges, so the median values for the points within [m n]/2 of the edges might appear distorted.
medfilt2在边界处用0进行扩边,因此对边界处[m n]/2内的点可能会产生畸变。
[编者多嘴]这点值得引起注意,其实完全可以用复制的方法扩边。这样更容易避免畸变。
B = medfilt2(A) performs median filtering of the matrix A using the default 3-by-3 neighborhood.
B = medfilt2(A) 为对矩阵A进行默认的3×3点中值滤波。
B = medfilt2(A, 'indexed', ...) processes A as an indexed image, padding with 0s if the class of A is uint8, or 1s if the class of A is double.
B = medfilt2(A, 'indexed', ...) 用来处理被索引的图像A,如果A的类型是uint8就用0扩边,是双精度浮点数就用1扩边。
B = medfilt2(..., padopt) controls how the matrix boundaries are padded. padopt may be 'zeros' (the default), 'symmetric', or 'indexed'.
B = medfilt2(..., padopt) 用来控制矩阵边界的扩边方式,参数padopt默认为'zeros'也就是0,还可以是'symmetric'对称,或者'indexed'被索引的。
If padopt is 'symmetric', A is symmetrically extended at the boundaries. If padopt is 'indexed', A is padded with ones if it is double; otherwise it is padded with zeros.
如果padopt设置为'symmetric',矩阵A就会被以对称的方式扩边,如果是'indexed',如果A是双精度实数,就会扩边为1,否则就是扩边为0。
综合来看,medfilt2也就是使用方式更灵活,更有针对性。
Class Support 类的支持
The input image A can be of class logical or numeric (unless the 'indexed' syntax is used, in which case A cannot be of class uint16). The output image B is of the same class as A.
输入图像A可以是逻辑的或者数值的(如果没用'indexed'语法,那么A不能为uint16类型),输出图像B和A的类型保持一致。
Note For information about performance considerations, see ordfilt2.
注意:想了解更多关于运算性能及条件,清参见ordfilt2函数。
Tips 官方小贴士
If the input image A is of an integer class, all the output values are returned as integers.
如果输入图像A是整数类型,输出格式也会是整数类型。
If the number of pixels in the neighborhood (i.e., m*n) is even, some of the median values might not be integers. In these cases, the fractional parts are discarded. Logical input is treated similarly.
如果窗口内(即, m*n)像素点为偶数,那么像素点可能不是整数。这时候,小数部分会舍去。逻辑类型的数据(只有0和1)也是类似的处理办法。
For example, suppose you call medfilt2 using 2-by-2 neighborhoods, and the input image is a uint8 array that includes this neighborhood.
1 5
4 8
例如,假设你用2×2的窗口来处理,输入图像A是uint8类型,且窗口内为
1 5
4 8
medfilt2 returns an output value of 4 for this neighborhood, although the true median is 4.5.
其中值就是4和5,均值就是4.5,舍去小数就是4。
Examples 官方示例
Add salt and pepper noise to an image and then restore the image using medfilt2.
下面,我们使用盐和胡椒面来污染一个图像,然后再用medfilt2来提纯
% 读取原始图像
I = imread('eight.tif');
% 添加噪声
J = imnoise(I,'salt & pepper',0.02);
% 中值滤波
K = medfilt2(J);
% 绘图对比效果
imshow(J), figure, imshow(K)
效果还是棒棒的。
Algorithms 算法
medfilt2 uses ordfilt2 to perform the filtering.
medfilt2 使用 ordfilt2 函数来实现滤波。
References 官方参考文献
[1] Lim, Jae S., Two-Dimensional Signal and Image Processing, Englewood Cliffs, NJ, Prentice Hall, 1990, pp. 469-476.
See Also 更多内容...
filter2 | ordfilt2 | wiener2