Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

adaptthresh

使用局部一阶统计量的自适应图像阈值

说明

示例

T = adaptthresh(I) 计算二维灰度图像或三维灰度体 I 的局部自适应阈值。adaptthresh 函数基于每个像素邻域的局部均值强度(一阶统计量)选择阈值。阈值 T 可与 imbinarize 函数结合使用以将灰度图像转换为二值图像。

示例

T = adaptthresh(I,sensitivity) 使用 sensitivity 指定的敏感度因子计算局部自适应阈值。sensitivity 是范围 [0,1] 内的标量,表示通过阈值化处理将更多像素归为前景的敏感度。

示例

T = adaptthresh(___,Name,Value) 使用名称-值对组计算局部自适应阈值,以控制阈值的各个方面。

示例

全部折叠

将图像读入工作区。

I = imread('rice.png');

使用 adaptthresh 确定在二值化运算中使用的阈值。

T = adaptthresh(I, 0.4);

将图像转换为二值图像,以指定阈值。

BW = imbinarize(I,T);

并排显示原始图像及其二值图像版本。

figure
imshowpair(I, BW, 'montage')

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

将图像读入工作区。

I = imread('printedtext.png');

使用 adaptthresh 计算自适应阈值并显示局部阈值图像。这表示平均背景照度的估计值。

T = adaptthresh(I,0.4,'ForegroundPolarity','dark');
figure
imshow(T)

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

使用局部自适应阈值对图像进行二值化

BW = imbinarize(I,T);
figure
imshow(BW)

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

将三维体加载到工作区中。

load mristack;
V = mristack;

显示数据。

figure
slice(double(V),size(V,2)/2,size(V,1)/2,size(V,3)/2)
colormap gray
shading interp

Figure contains an axes object. The axes object contains 3 objects of type surface.

计算阈值。

J = adaptthresh(V,'neigh',[3 3 3],'Fore','bright');

显示阈值。

figure
slice(double(J),size(J,2)/2,size(J,1)/2,size(J,3)/2)
colormap gray
shading interp

Figure contains an axes object. The axes object contains 3 objects of type surface.

输入参数

全部折叠

灰度图像或图像体,指定为二维数值矩阵或三维数值数组。

如果图像包含 InfNaN,则表示 adaptthresh 的行为未定义。InfNaN 的传播可能不会局限于 InfNaN 像素周围的邻域。

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

确定哪些像素被阈值化为前景像素,指定为 [0, 1] 范围内的数字。高敏感度值导致更多的像素通过阈值化处理归为前景,但这会存在将一些背景像素也归为前景的风险。

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

名称-值参数

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

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

示例: T = adaptthresh(I,0.4,"ForegroundPolarity","dark");

用于计算每个像素周围局部统计量的邻域大小,指定为正奇数或由正奇数组成的二元素向量。

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

确定哪些像素被视为前景像素,使用以下项之一进行指定:

意义

"bright"

前景比背景亮。

"dark"

前景比背景暗

数据类型: char | string

用于计算每个像素的局部阈值的统计量,指定为下列值之一:

意义

"mean"

邻域中的局部均值强度。这种方法也称为 Bradley 方法 [1]

"median"

邻域中的局部中位数。此统计量的计算可能会很慢。请考虑使用较小的邻域大小来更快地获得结果。

"gaussian"

邻域中的高斯加权均值。

数据类型: char | string

输出参数

全部折叠

归一化的强度值,以与输入图像或图像体 I 大小相同的数值矩阵或数值数组形式返回。值归一化为范围 [0, 1]。

数据类型: double

参考

[1] Bradley, D., G. Roth, "Adapting Thresholding Using the Integral Image," Journal of Graphics Tools. Vol. 12, No. 2, 2007, pp.13–21.

扩展功能

版本历史记录

在 R2016a 中推出

全部展开