opencv 阈值分割函数

发布于:2025-08-06 ⋅ 阅读:(14) ⋅ 点赞:(0)

CV_EXPORTS_W double threshold( InputArray src, OutputArray dst,
double thresh, double maxval, int type );

/**

@brief 对数组的每个元素应用固定阈值处理

该函数对多通道数组执行固定阈值分割,常用于从灰度图像生成二值图像(也可使用#compare实现类似功能),

或通过过滤过小/过大的像素值来去除噪声。函数支持多种阈值分割类型,由type参数指定。

特殊值#THRESH_OTSU或#THRESH_TRIANGLE可与其他类型组合使用。此时函数将采用大津算法或三角算法

自动确定最优阈值,并忽略手动指定的thresh值。

@注意 目前大津法和三角法仅支持8位单通道图像。

@param src 输入数组(多通道,8位或32位浮点类型)

@param dst 输出数组(与src尺寸、类型及通道数相同)

@param thresh 阈值

@param maxval 与#THRESH_BINARY和#THRESH_BINARY_INV类型搭配使用的最大值

@param type 阈值分割类型(参见#ThresholdTypes枚举)

@return 若使用大津法或三角法,返回计算得到的阈值

@另见 adaptiveThreshold, findContours, compare, min, max
*/

THRESH_BINARY 或 THRESH_BINARY_INV,会将大于thresh的像素值设置为maxval,其余设置为0(二值化)
/** Threshold types */
enum
{
CV_THRESH_BINARY =0, /< value = value > threshold ? max_value : 0 */
CV_THRESH_BINARY_INV =1, /
< value = value > threshold ? 0 : max_value */
CV_THRESH_TRUNC =2, /< value = value > threshold ? threshold : value */
CV_THRESH_TOZERO =3, /
< value = value > threshold ? value : 0 /
CV_THRESH_TOZERO_INV =4, /< value = value > threshold ? 0 : value */
CV_THRESH_MASK =7,
CV_THRESH_OTSU =8, /
< use Otsu algorithm to choose the optimal threshold value;
combine the flag with one of the above CV_THRESH_
values /
CV_THRESH_TRIANGLE =16 /**< use Triangle algorithm to choose the optimal threshold value;
combine the flag with one of the above CV_THRESH_
values, but not
with CV_THRESH_OTSU */
};


网站公告

今日签到

点亮在社区的每一天
去签到