【OpenCV3】threshold()函数详解

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() <= 2 && _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 < 0 || ithresh >= 255 ) { if( type == THRESH_BINARY || type == THRESH_BINARY_INV || ((type == THRESH_TRUNC || type == THRESH_TOZERO_INV) && ithresh < 0) || (type == THRESH_TOZERO && ithresh >= 255) ) { int v = type == THRESH_BINARY ? (ithresh >= 255 ? 0 : imaxval) : type == THRESH_BINARY_INV ? (ithresh >= 255 ? imaxval : 0) : /*type == THRESH_TRUNC ? imaxval :*/ 0; 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 ? 0 : imaxval) : type == THRESH_BINARY_INV ? (ithresh >= SHRT_MAX ? imaxval : 0) : /*type == THRESH_TRUNC ? imaxval :*/ 0; 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 = 0; 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 ? 0 : imaxval) : type == THRESH_BINARY_INV ? (ithresh >= (int)USHRT_MAX ? imaxval : 0) : /*type == THRESH_TRUNC ? imaxval :*/ 0; 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(0, dst.rows), ThresholdRunner(src, dst, thresh, maxval, type), dst.total()/(double)(1<<16)); return thresh; }

 threshold()函数二值化的方法(types)/** 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_*值之一相结合*/ 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_thresh_*值之一组合,但不使用cv_thresh_otsu*/ };

 

转载于:https://www.cnblogs.com/hs-pingfan/p/10494264.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenCVthreshold函数是一个图像处理函数,用于将输入图像转换为二值图像。该函数有以下原型: retval, dst = cv.threshold(src, thresh, maxval, type[, dst]) 其中,参数src是输入图像,thresh是阈值,maxval是最大值,type是阈值类型,dst是输出图像。函数的返回值retval是阈值的实际值。 具体来说,函数将输入图像中的每个像素与阈值进行比较,如果大于阈值则设置为最大值,如果小于等于阈值则设置为0或最小值,这取决于所选择的类型。不同的类型可以根据需要选择,例如THRESH_BINARY、THRESH_BINARY_INV、THRESH_TRUNC、THRESH_TOZERO、THRESH_TOZERO_INV等。 要使用threshold函数,首先导入OpenCV库,并将图像加载为输入。然后,通过调用threshold函数,并传递所需的参数来进行二值化处理。最后,可以通过打印输出图像来查看结果。 例如,在Python中可以使用以下代码进行二值化处理: import cv2 # 加载图像 img = cv2.imread('image.jpg', 0) # 调用threshold函数进行二值化处理 ret, dst = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) # 打印输出图像 print(dst) 以上代码将图像加载为灰度图像,并将像素值大于127的设置为255,小于等于127的设置为0,得到了二值化的输出图像。 总结起来,OpenCVthreshold函数是用于将输入图像转换为二值图像的函数,根据阈值和类型对像素进行处理,可以实现图像的二值化处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [OpenCV图像阈值:简单阈值、自适应阈值、OTSU、TRIANGLE](https://blog.csdn.net/thequitesunshine007/article/details/107594320)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [OpenCV threshold函数详解](https://blog.csdn.net/weixin_42296411/article/details/80901080)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值