主要内容

ordfilt2

R2026b

二维顺序统计量滤波

描述

B = ordfilt2(A,order,domain) 将 A 中的每个元素替换为由 domain 定义的邻域中的非零元素指定的相邻元素的有序集中的第 order 个元素。

示例

B = ordfilt2(A,order,domain,S) 对 A 进行滤波,其中 ordfilt2 使用与 domain 的非零值对应的 S 的值作为加性偏移量。您可以使用此语法实现灰度形态学运算,包括灰度膨胀和腐蚀。

B = ordfilt2(___,padopt) 对 A 进行滤波,其中 padopt 指定 ordfilt2 如何填充矩阵边界。

示例

全部折叠

将图像读入工作区并显示它。

A = imread("snowflakes.png");
figure
imshow(A)

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

使用 5×5 最大值滤波器对图像进行滤波并显示结果。

B = ordfilt2(A,25,true(5));
figure
imshow(B)

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

将一个图像读入工作区并显示它。

A = imread("snowflakes.png");
A = im2double(A);
figure
imshow(A,[])

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

定义一个偏斜的 5×5 结构元素,其右列强度高于左列。

SE = repmat([1 1 1 5 5],5,1)
SE = 5×5

     1     1     1     5     5
     1     1     1     5     5
     1     1     1     5     5
     1     1     1     5     5
     1     1     1     5     5

ordfilt2 函数的域仅需要结构元素中非零元素的位置,而不需要值。将域定义为结构元素的逻辑值。

domain = SE > 0
domain = 5×5 logical array

   1   1   1   1   1
   1   1   1   1   1
   1   1   1   1   1
   1   1   1   1   1
   1   1   1   1   1

要执行形态学膨胀,必须将 ordfilt2 函数的阶数指定为最大值。将阶数指定为域中非零元素的数量。

order = nnz(domain)
order = 
25

使用最大值滤波器以结构元素作为加性偏移量对图像进行滤波。加性偏移量控制形态学效果发生的方向。

B = ordfilt2(A,order,domain,SE);

显示滤波后的图像。注意观察,由于结构元素的强度,雪花在左侧和右侧以不同比例膨胀。

figure
imshow(B,[])

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

输入参数

全部折叠

要滤波的数据,指定为二维逻辑矩阵或二维数值矩阵。

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

用来替换目标像素的元素,指定为实整数标量。函数将 A 中的每个元素替换为由 domain 中的非零元素指定的相邻元素的有序集中的第 order 个元素。

数据类型: double

邻域,指定为包含 1 和 0 的数值或逻辑矩阵。domain 等效于用于二值图像运算的结构元素。1 值元素定义滤波运算的邻域。下表列出一些常见滤波器的示例。

滤波运算类型示例代码邻域示例图像数据,指示选定元素
中位数滤波器B = ordfilt2(A,5,ones(3,3))

3-by-3 matrix of ones

3-by-3 matrix of numbers. The element with the fifth highest value in the neighborhood is circled.

最小值滤波器B = ordfilt2(A,1,ones(3,3))

3-by-3 matrix of ones

3-by-3 matrix of numbers. The element with the lowest value in the neighborhood is circled.

最大值滤波器B = ordfilt2(A,9,ones(3,3))

3-by-3 matrix of ones

3-by-3 matrix of numbers. The element with the highest value in the neighborhood is circled.

北、东、南、西邻点的最小值B = ordfilt2(A,1,[0 1 0; 1 0 1; 0 1 0])

3-by-3 neighborhood in which the north, south, east, and west pixels are true (1) and the center and corner pixels are false (0)

3-by-3 matrix of numbers. The element with the lowest value in the specified neighborhood is circled. Elements excluded from the neighborhood are grayed out.

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

加性偏移量,指定为与 domain 大小相同的数值矩阵。该函数在排序之前添加与 domain 的非零值对应的 S 的值。您可以使用加性偏移量来实现灰度形态学运算,包括灰度膨胀和腐蚀。加性偏移量控制形态学效果发生的方向。

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

填充选项,指定为下列值之一。填充的性质控制函数如何计算图像边缘处输出像素的值。

选项描述示例
"zeros"用值 0 填充数组边界。

[314159265]→[0000000000000000314000015900002650000000000000000]

"symmetric"

用自身的镜像翻转填充数组。

[314159265]→[5115995133144113314415115995622655662265565115995]

数据类型: char | string

输出参量

全部折叠

滤波后的数据,以与输入数据 A 具有相同数据类型的二维数值矩阵或二维逻辑矩阵形式返回。

提示

  • 当处理不包含任何零值元素的大型域矩阵时,如果 A 采用整数数据格式(uint8、int8、uint16 和 int16),则 ordfilt2 可以获得更高的性能。uint8 和 int8 的速度增益高于 16 位数据类型。对于 8 位数据格式,域矩阵必须包含 7 行或更多行。对于 16 位数据格式,域矩阵必须包含三行或更多行以及 520 个或更多元素。

参考

[1] Haralick, Robert M., and Linda G. Shapiro, Computer and Robot Vision, Volume I, Addison-Wesley, 1992.

[2] Huang, T. S., G. J. Yang, and G. Y. Tang. "A fast two-dimensional median filtering algorithm.", IEEE transactions on Acoustics, Speech and Signal Processing, Vol ASSP 27, No. 1, February 1979.

扩展功能

全部展开

版本历史记录

在 R2006a 之前推出

全部展开