阈值

算子列表 ↓

本章描述阈值算子。

阈值算子的概念

对图像进行分割的一种方法是使用阈值算子。通过这种方式,可以在图像中确定满足特定阈值条件的区域,该条件取决于灰度值。

为适应不同任务和图像特性,提供了一组阈值算子。

下文将通过区分基于直方图和局部的方法来概述这些算子,并重点探讨其中最重要的几种。

基于直方图的阈值算子

基于直方图的阈值分割仅考虑像素值而非位置信息。因此阈值是通过调整图像直方图来确定的。

阈值

通过使用 thresholdthresholdThresholdThresholdThresholdthreshold,您可以选取用户设定的灰度值区间内的所有像素。

( 1) ( 2) ( 3)
(1) 输入图像,(2) 采用人工确定的阈值 MinGrayMinGrayMinGrayMinGrayminGraymin_grayMaxGrayMaxGrayMaxGrayMaxGraymaxGraymax_gray 处理后的输入图像直方图,(3) 输入图像的最终分割结果。

算子 fast_thresholdfast_thresholdFastThresholdFastThresholdFastThresholdfast_threshold 同样采用两个手动设定的阈值,但使用了另一种计算算法。

二值阈值

为将图像划分为明暗区域,binary_thresholdbinary_thresholdBinaryThresholdBinaryThresholdBinaryThresholdbinary_threshold 会自动计算阈值以分离前景与背景。

( 1) ( 2) ( 3)
(1) 输入图像,(2) 采用自动确定的二值阈值对输入图像进行的直方图分析,(3) 输入图像的最终分割结果。

使用 threshold_sub_pixthreshold_sub_pixThresholdSubPixThresholdSubPixThresholdSubPixthreshold_sub_pix 分割法同样将图像划分为前景与背景,但能以亚像素精度输出分离边界。阈值需手动设定。

自动阈值

auto_thresholdauto_thresholdAutoThresholdAutoThresholdAutoThresholdauto_threshold 通过计算图像直方图中的局部极小值来确定阈值。通过平滑直方图,您可以影响输入图像中检测到的类别数量。

( 1) ( 2) ( 3) ( 4)
(1) 略微平滑的直方图,(2) 导致局部极小值数量增加,(3) 随着进一步平滑,(4) 局部极小值数量减少。
( 1) ( 2) ( 3)
(1) 输入图像,(2) 从略微平滑的直方图中提取四个类别,(3) 而在此情况下,进一步平滑的直方图则产生两个区域。

使用 histo_to_threshhisto_to_threshHistoToThreshHistoToThreshHistoToThreshhisto_to_thresh 获取直方图中局部极小值的灰度值。

字符阈值

要对浅色背景上的深色文本进行分割,char_thresholdchar_thresholdCharThresholdCharThresholdCharThresholdchar_threshold 是一个有用的工具。直方图中的最大峰值对应浅色背景。假设文本比背景更暗,则需检查平滑直方图中最大峰值左侧的部分。参数 PercentPercentPercentPercentpercentpercent 决定阈值设置在最大峰值多远的位置,同时考虑灰度值的频率分布。

( 1) ( 2)
( 3) ( 4)
(1) 输入图像,(2) 对直方图进行平滑处理并识别全局最大值,(3) 参数 PercentPercentPercentPercentpercentpercent 百分比决定最大值左侧的阈值,(4) 生成图像。
双阈值

从一张图像中减去另一张图像,或使用 laplace_of_gausslaplace_of_gaussLaplaceOfGaussLaplaceOfGaussLaplaceOfGausslaplace_of_gauss 等边缘检测算子,通常会在结果图像中产生负值。算子 dual_thresholddual_thresholdDualThresholdDualThresholdDualThresholddual_threshold 既适用于带符号图像的分割,又能同时考虑最小区域尺寸。

ThresholdThresholdThresholdThresholdthresholdthreshold (内部边界)和 MinGrayMinGrayMinGrayMinGrayminGraymin_gray 的有符号图像直方图。仅当像素满足灰度值和区域大小条件时才会被选中。
( 1) ( 2)
(1) 有符号输入图像(使用 sub_imagesub_imageSubImageSubImageSubImagesub_image 创建),(2) 应用双阈值后的结果。

局部阈值算子

与基于直方图的阈值算子不同,局部阈值还会考虑像素的位置或邻域关系,从而将其分配到相应的区域。相较于对每个像素应用全局阈值,根据图像局部特征调整阈值有时更为有效。

局部阈值

local_thresholdlocal_thresholdLocalThresholdLocalThresholdLocalThresholdlocal_threshold 会考虑局部均值和标准差,为每个像素计算独立阈值。邻域大小由用户设定。该算法特别适用于文本分割场景,尤其在光照条件或背景不均匀的情况下效果显著。

( 1) ( 2)
( 3) ( 4)
(1) 输入图像,(2) 全局阈值分割无法单独选取目标文本,(3) 对每个像素的邻域单独调整阈值,(4) 实现文本分割。

算子 var_thresholdvar_thresholdVarThresholdVarThresholdVarThresholdvar_threshold 的工作原理与此类似,但它会选择那些满足特定局部标准差和亮度条件的图像点。

动态阈值

使用 dyn_thresholddyn_thresholdDynThresholdDynThresholdDynThresholddyn_threshold 可检测图像间的差异。通常将输入图像与其滤波版本(例如图像均值)进行逐像素比较。参数 LightDarkLightDarkLightDarkLightDarklightDarklight_dark 用于确定图像中哪些变化具有相关性。算子的灵敏度由参数 OffsetOffsetOffsetOffsetoffsetoffset 控制。

( 1) ( 2)
( 3) ( 4)
(1) 输入图像,(2) 由于光照条件不均匀,无法通过全局阈值分割划痕,(3) 对原始图像(左)与图像均值(中)的灰度值差异进行逐像素检查,以分割超过 OffsetOffsetOffsetOffsetoffsetoffset 的区域(右)(图像细节),(4) 选取目标区域后的结果。更多细节请参阅示例 surface_scratch.hdev

类似的算子是 check_differencecheck_differenceCheckDifferenceCheckDifferenceCheckDifferencecheck_difference。该算子显示两幅图像之间的绝对差异,特别适用于连续采集图像的变化检测。

零交叉点阈值

用于边缘检测的运算符(如 laplace_of_gausslaplace_of_gaussLaplaceOfGaussLaplaceOfGaussLaplaceOfGausslaplace_of_gauss )会返回有符号图像,其中边缘位于零交叉点处。可通过 zero_crossingzero_crossingZeroCrossingZeroCrossingZeroCrossingzero_crossingzero_crossing_sub_pixzero_crossing_sub_pixZeroCrossingSubPixZeroCrossingSubPixZeroCrossingSubPixzero_crossing_sub_pix 提取这些边缘,同时考虑单个像素点的 4 邻域特征。

( 1) ( 2)
( 3) ( 4)
(1) 输入图像,(2) 缩小域并应用高斯拉普拉斯滤波器,(3) 零交叉点算子可实现像素级(红色)与亚像素级(绿色)的边缘检测,(4) 采用 zero_crossingzero_crossingZeroCrossingZeroCrossingZeroCrossingzero_crossing(红色)与 zero_crossing_sub_pixzero_crossing_sub_pixZeroCrossingSubPixZeroCrossingSubPixZeroCrossingSubPixzero_crossing_sub_pix(绿色)所得结果的图像细节。

算子列表

auto_thresholdAutoThresholdauto_thresholdAutoThresholdauto_threshold自动阈值
使用从直方图确定的阈值对图像进行分割。
binary_thresholdBinaryThresholdbinary_thresholdBinaryThresholdbinary_threshold二值阈值
使用二值阈值分割图像。
char_thresholdCharThresholdchar_thresholdCharThresholdchar_threshold字符阈值
执行阈值分割以提取字符。
check_differenceCheckDifferencecheck_differenceCheckDifferencecheck_difference检查差异
逐像素比较两幅图像。
dual_thresholdDualThresholddual_thresholdDualThresholddual_threshold双阈值
有符号图像的阈值算子。
dyn_thresholdDynThresholddyn_thresholdDynThresholddyn_threshold动态阈值
使用局部阈值分割图像。
fast_thresholdFastThresholdfast_thresholdFastThresholdfast_threshold快速阈值
使用全局阈值对图像进行快速阈值处理。
histo_to_threshHistoToThreshhisto_to_threshHistoToThreshhisto_to_thresh直方图至阈值
根据直方图确定灰度值阈值。
local_thresholdLocalThresholdlocal_thresholdLocalThresholdlocal_threshold局部阈值
使用局部阈值分割图像。
thresholdThresholdthresholdThresholdthreshold阈值
使用全局阈值分割图像。
threshold_sub_pixThresholdSubPixthreshold_sub_pixThresholdSubPixthreshold_sub_pix阈值亚像素
以亚像素精度从图像中提取级交叉点。
var_thresholdVarThresholdvar_thresholdVarThresholdvar_threshold可变阈值
通过局部均值和标准差分析对图像进行阈值处理。
zero_crossingZeroCrossingzero_crossingZeroCrossingzero_crossing零交叉点
从图像中提取零交叉点。
zero_crossing_sub_pixZeroCrossingSubPixzero_crossing_sub_pixZeroCrossingSubPixzero_crossing_sub_pix零交叉点亚像素
以亚像素精度从图像中提取零交叉点。