threshold()函数源码

double cv::threshold( InputArray _src, OutputArray _dst, double thresh, double maxval, int type )
{

// 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 */
//};

    CV_INSTRUMENT_REGION();

    CV_OCL_RUN_(_src.dims() <=  && _dst.isUMat(),
ocl_threshold(_src, _dst, thresh, maxval, type), thresh) Mat src = _src.getMat();
int automatic_thresh = (type & ~CV_THRESH_MASK);// 排除前五种可能,判断是否是CV_THRESH_OTSU \ CV_THRESH_TRIANGLE(8,16)
type &= THRESH_MASK; // THRESH_MASK(7) 得到当前二值化的类型(前五种),0,1,2,3,4 CV_Assert( automatic_thresh != (CV_THRESH_OTSU | CV_THRESH_TRIANGLE) );
if( automatic_thresh == CV_THRESH_OTSU )// 判断是否是CV_THRESH_OTSU(8)
{
    //  使用算法选择最佳阈值;将标志与上述cv_thresh_*值之一相结合 计算最佳阈值
CV_Assert( src.type() == CV_8UC1 );
thresh = getThreshVal_Otsu_8u( src );
}
else if( automatic_thresh == CV_THRESH_TRIANGLE )// 判断是否是CV_THRESH_TRIANGLE(16)
{
    // 使用三角算法选择最优阈值;将标志与上述cv_thresh_*值之一组合,但不使用cv_thresh_otsu 计算最佳阈值
CV_Assert( src.type() == CV_8UC1 );
thresh = getThreshVal_Triangle_8u( src );
} _dst.create( src.size(), src.type() );// 创建目标图像
Mat dst = _dst.getMat(); if( src.depth() == CV_8U )// 如果原始图像的深度为8位无符号
{
int ithresh = cvFloor(thresh);// 将thresh向下取整
thresh = ithresh;
int imaxval = cvRound(maxval); // 将maxval向最接近的整数取整
if( type == THRESH_TRUNC )
imaxval = ithresh;
imaxval = saturate_cast<uchar>(imaxval); if( ithresh < || ithresh >= )
{
if( type == THRESH_BINARY || type == THRESH_BINARY_INV ||
((type == THRESH_TRUNC || type == THRESH_TOZERO_INV) && ithresh < ) ||
(type == THRESH_TOZERO && ithresh >= ) )
{
int v = type == THRESH_BINARY ? (ithresh >= ? : imaxval) :
type == THRESH_BINARY_INV ? (ithresh >= ? imaxval : ) :
/*type == THRESH_TRUNC ? imaxval :*/ ;
dst.setTo(v);
}
else
src.copyTo(dst);
return thresh;
}
CV_OVX_RUN(!ovx::skipSmallImages<VX_KERNEL_THRESHOLD>(src.cols, src.rows),
openvx_threshold(src, dst, ithresh, imaxval, type), (double)ithresh) thresh = ithresh;
maxval = imaxval;
}
else if( src.depth() == CV_16S )// 如果原始图像的深度为16位short类型
{
int ithresh = cvFloor(thresh);
thresh = ithresh;
int imaxval = cvRound(maxval);
if( type == THRESH_TRUNC )
imaxval = ithresh;
imaxval = saturate_cast<short>(imaxval); if( ithresh < SHRT_MIN || ithresh >= SHRT_MAX )
{
if( type == THRESH_BINARY || type == THRESH_BINARY_INV ||
((type == THRESH_TRUNC || type == THRESH_TOZERO_INV) && ithresh < SHRT_MIN) ||
(type == THRESH_TOZERO && ithresh >= SHRT_MAX) )
{
int v = type == THRESH_BINARY ? (ithresh >= SHRT_MAX ? : imaxval) :
type == THRESH_BINARY_INV ? (ithresh >= SHRT_MAX ? imaxval : ) :
/*type == THRESH_TRUNC ? imaxval :*/ ;
dst.setTo(v);
}
else
src.copyTo(dst);
return thresh;
}
thresh = ithresh;
maxval = imaxval;
}
else if (src.depth() == CV_16U )// 如果原始图像的深度为16位无符号
{
int ithresh = cvFloor(thresh);
thresh = ithresh;
int imaxval = cvRound(maxval);
if (type == THRESH_TRUNC)
imaxval = ithresh;
imaxval = saturate_cast<ushort>(imaxval); int ushrt_min = ;
if (ithresh < ushrt_min || ithresh >= (int)USHRT_MAX)
{
if (type == THRESH_BINARY || type == THRESH_BINARY_INV ||
((type == THRESH_TRUNC || type == THRESH_TOZERO_INV) && ithresh < ushrt_min) ||
(type == THRESH_TOZERO && ithresh >= (int)USHRT_MAX))
{
int v = type == THRESH_BINARY ? (ithresh >= (int)USHRT_MAX ? : imaxval) :
type == THRESH_BINARY_INV ? (ithresh >= (int)USHRT_MAX ? imaxval : ) :
/*type == THRESH_TRUNC ? imaxval :*/ ;
dst.setTo(v);
}
else
src.copyTo(dst);
return thresh;
}
thresh = ithresh;
maxval = imaxval;
}
else if( src.depth() == CV_32F )// 如果原始图像的深度为32位浮点型
;
else if( src.depth() == CV_64F )// 如果原始图像的深度为64位浮点型
;
else
CV_Error( CV_StsUnsupportedFormat, "" ); // 不能识别的图像格式 parallel_for_(Range(, dst.rows),
ThresholdRunner(src, dst, thresh, maxval, type),
dst.total()/(double)(<<));
return thresh;
}

threshold()函数二值化的方法(types)/** Threshold types */

enum
{
CV_THRESH_BINARY =, /**< value = value > threshold ? max_value : 0 正向二值化*/
CV_THRESH_BINARY_INV =, /**< value = value > threshold ? 0 : max_value 反向二值化*/
CV_THRESH_TRUNC =, /**< value = value > threshold ? threshold : value */
CV_THRESH_TOZERO =, /**< value = value > threshold ? value : 0 */
CV_THRESH_TOZERO_INV =, /**< value = value > threshold ? 0 : value */
CV_THRESH_MASK =,  // 掩码
CV_THRESH_OTSU =, /**< use Otsu algorithm to choose the optimal threshold value;
combine the flag with one of the above CV_THRESH_* values
                    使用算法选择最佳阈值;将标志与上述cv_thresh_*值之一相结合*/
CV_THRESH_TRIANGLE = /**< 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
                    使用三角算法选择最优阈值;将标志与上述cv_thresh_*值之一组合,但不使用cv_thresh_otsu*/
};

【OpenCV3】threshold()函数详解的更多相关文章

  1. opencv-python图像二值化函数cv2.threshold函数详解及参数cv2.THRESH_OTSU使用

    cv2.threshold()函数的作用是将一幅灰度图二值化,基本用法如下: #ret:暂时就认为是设定的thresh阈值,mask:二值化的图像 ret,mask = cv2.threshold(i ...

  2. php缓存技术——memcache常用函数详解

    php缓存技术——memcache常用函数详解 2016-04-07 aileen PHP编程 Memcache函数库是在PECL(PHP Extension Community Library)中, ...

  3. malloc 与 free函数详解<转载>

    malloc和free函数详解   本文介绍malloc和free函数的内容. 在C中,对内存的管理是相当重要.下面开始介绍这两个函数: 一.malloc()和free()的基本概念以及基本用法: 1 ...

  4. NSSearchPathForDirectoriesInDomains函数详解

    NSSearchPathForDirectoriesInDomains函数详解     #import "NSString+FilePath.h" @implementation ...

  5. JavaScript正则表达式详解(二)JavaScript中正则表达式函数详解

    二.JavaScript中正则表达式函数详解(exec, test, match, replace, search, split) 1.使用正则表达式的方法去匹配查找字符串 1.1. exec方法详解 ...

  6. Linux C popen()函数详解

    表头文件 #include<stdio.h> 定义函数 FILE * popen( const char * command,const char * type); 函数说明 popen( ...

  7. kzalloc 函数详解(转载)

    用kzalloc申请内存的时候, 效果等同于先是用 kmalloc() 申请空间 , 然后用 memset() 来初始化 ,所有申请的元素都被初始化为 0. view plain /** * kzal ...

  8. Netsuite Formula > Oracle函数列表速查(PL/SQL单行函数和组函数详解).txt

    PL/SQL单行函数和组函数详解 函数是一种有零个或多个参数并且有一个返回值的程序.在SQL中Oracle内建了一系列函数,这些函数都可被称为SQL或PL/SQL语句,函数主要分为两大类: 单行函数 ...

  9. jQuery.attr() 函数详解

    一,jQuery.attr()  函数详解: http://www.365mini.com/page/jquery-attr.htm 二,jQuery函数attr()和prop()的区别: http: ...

随机推荐

  1. BZOJ 1935 Tree 园丁的烦恼 (树状数组)

    题意:中文题. 析:按x排序,然后用树状数组维护 y 即可. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000" ...

  2. Git客户端命令总结

    一:常用命令 1.先进入项目目录,然后git init:则会为此 项目/目录 创建一个本地仓库(或重新初始化这个本地仓库),可以用ls -a ./看到多了.git目录: 或者git init /hom ...

  3. python 判断是否是空行或注释行

    #coding:utf-8 '''''cdays-4-exercise-6.py 文件基本操作 @note: 文件读取写入, 列表排序, 字符串操作 @see: 字符串各方法可参考hekp(str)或 ...

  4. 关于iOS的自动弹出键盘问题

    -(void)viewWillAppear:(BOOL)animated {     [super viewWillAppear:animated];     [self.textField beco ...

  5. Linux中的LVM和软RAID

        在实际工作中,会经常碰到所给的服务器硬盘容量太小,而实际的应用软件中却需要一个容量较大的分区进行数据存储等,除了通过硬件RAID卡来实现合并多硬盘外,其实我们也可以通过软件的方式来实现. 实验 ...

  6. [翻译]Writing Custom Wizards 编写自定义的向导

    Writing Custom Wizards  编写自定义的向导   You can extend FastReport's functionality with the help of custom ...

  7. Android 了解1G 2G 3G 知识

    了解1G 2G 3G 相关知识,对网络通讯制式进行了解即可 1.这种网络通讯制式是一步一步发展起来的,由最开始的1G(最典型的手机,例如:大哥大,1G这种制式只能语音通话). 2.后来出现的2G,2G ...

  8. cassandra集群缩容与剔除问题节点

    今天在操作cassandra集群数据迁移时发生了一些意料之外的事情,服务器迁移前与迁移后同样为5台,但是不知道是什么原因导致的,迁移过后的节点居然多出了一台cassandra节点,个人瞬间感觉莫名其妙 ...

  9. abp+angular+bootstrap-table的使用

    问题 materialize与bootstrap框架样式冲突 问题描述 在abp模板项目中引入bootstrap-table,列设置为checkbox,checkbox无法显示. 使用firefox浏 ...

  10. JavaScript正则表达式匹配中英文以及常用标点符号白名单写法

    我们在编程中经常会遇到特殊字符过滤的问题,今天我们提供一种白名单方式过滤 直接上代码 function RegEXP(s) { var rs = ""; for (var i = ...