Image Thresholding

发布时间 - 2025-07-15 00:00:00    点击率:

大家好,又见面了,我是你们的朋友全栈君。

Simple Thresholding

The function cv.threshold is used to apply the thresholding. The first argument is the source image, which should be a grayscale image. The second argument is the threshold value which is used to classify the pixel values. The third argument is the maximum value which is assigned to pixel values exceeding the threshold. OpenCV provides different types of thresholding which is given by the fourth parameter of the function.

The method returns two outputs. The first is the threshold that was used and the second output is the thresholded image.

代码语言:javascript代码运行次数:0运行复制
import cv2 as cvimport numpy as npfrom matplotlib import pyplot as pltimg = cv.imread('gradient.png',0)ret,thresh1 = cv.threshold(img,127,255,cv.THRESH_BINARY)ret,thresh2 = cv.threshold(img,127,255,cv.THRESH_BINARY_INV)ret,thresh3 = cv.threshold(img,127,255,cv.THRESH_TRUNC)ret,thresh4 = cv.threshold(img,127,255,cv.THRESH_TOZERO)ret,thresh5 = cv.threshold(img,127,255,cv.THRESH_TOZERO_INV)titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]for i in xrange(6):    plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')    plt.title(titles[i])    plt.xticks([]),plt.yticks([])plt.show()
Adaptive Thresholding

In the previous section, we used one global value as a threshold. But this might not be good in all cases, e.g. if an image has different lighting conditions in different areas. In that case, adaptive thresholding can help. Here, the algorithm determines the threshold for a pixel based on a small region around it.

In addition to the parameters described above, the method cv.adaptiveThreshold takes three input parameters:

The adaptiveMethod decides how the threshold value is calculated:

cv.ADAPTIVE_THRESH_MEAN_C: The threshold value is the mean of the neighbourhood area minus the constant C.cv.ADAPTIVE_THRESH_GAUSSIAN_C: The threshold value is a gaussian-weighted sum of the neighbourhood values minus the constant C.

The blockSize determines the size of the neighbourhood area and C is a constant that is subtracted from the mean or weighted sum of the neighbourhood pixels.

代码语言:javascript代码运行次数:0运行复制
import cv2 as cvimport numpy as npfrom matplotlib import pyplot as pltimg = cv.imread('sudoku.png',0)img = cv.medianBlur(img,5)ret,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)th2 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY,11,2)th3 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,11,2)titles = ['Original Image', 'Global Thresholding (v = 127)', 'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']images = [img, th1, th2, th3]for i in xrange(4):    plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')    plt.title(titles[i])    plt.xticks([]),plt.yticks([])plt.show()
Otsu’s Binarization

Consider an image with only two distinct image values (bimodal image), where the histogram would only consist of two peaks. A good threshold would be in the middle of those two values. Similarly, Otsu’s method determines an optimal global threshold value from the image histogram.

In order to do so, the cv.threshold() function is used, where cv.THRESH_OTSU is passed as an extra flag. The threshold value can be chosen arbitrary. The algorithm then finds the optimal threshold value which is returned as the first output.

Check out the example below. The input image is a noisy image. In the first case, global thresholding with a value of 127 is applied. In the second case, Otsu’s thresholding is applied directly. In the third case, the image is first filtered with a 5×5 gaussian kernel to remove the noise, then Otsu thresholding is applied.

代码语言:javascript代码运行次数:0运行复制
img = cv.imread('noisy2.png',0)# global thresholdingret1,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)# Otsu's thresholdingret2,th2 = cv.threshold(img,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)# Otsu's thresholding after Gaussian filteringblur = cv.GaussianBlur(img,(5,5),0)ret3,th3 = cv.threshold(blur,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/132295.html原文链接:https://javaforall.cn


# linux  # red  # JavaScript  # html  # if  # for  #   # function  # this  # input  # opencv  # https  # 我是  # 你们的  # 大家好  # 转载请注明  # 又见  # clip  # currentcolor  # rule  # evenodd  # token 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: 如何撰写建站申请书?关键要点有哪些?  如何在建站之星网店版论坛获取技术支持?  Laravel怎么实现搜索功能_Laravel使用Eloquent实现模糊查询与多条件搜索【实例】  Laravel怎么实现支付功能_Laravel集成支付宝微信支付  免费视频制作网站,更新又快又好的免费电影网站?  Python文件异常处理策略_健壮性说明【指导】  如何在 Pandas 中基于一列条件计算另一列的分组均值  浅述节点的创建及常见功能的实现  Laravel如何使用Blade模板引擎?(完整语法和示例)  Laravel路由怎么定义_Laravel核心路由系统完全入门指南  公司门户网站制作公司有哪些,怎样使用wordpress制作一个企业网站?  如何在宝塔面板中修改默认建站目录?  标题:Vue + Vuex + JWT 身份认证的正确实践与常见误区解析  Laravel怎么实现一对多关联查询_Laravel Eloquent模型关系定义与预加载【实战】  IOS倒计时设置UIButton标题title的抖动问题  Internet Explorer官网直接进入 IE浏览器在线体验版网址  利用JavaScript实现拖拽改变元素大小  公司门户网站制作流程,华为官网怎么做?  浅谈javascript alert和confirm的美化  Laravel Vite是做什么的_Laravel前端资源打包工具Vite配置与使用  教你用AI润色文章,让你的文字表达更专业  Python图片处理进阶教程_Pillow滤镜与图像增强  青岛网站建设如何选择本地服务器?  Windows10电脑怎么设置虚拟光驱_Win10右键装载ISO镜像文件  如何在橙子建站中快速调整背景颜色?  javascript如何操作浏览器历史记录_怎样实现无刷新导航  电视网站制作tvbox接口,云海电视怎样自定义添加电视源?  HTML5建模怎么导出为FBX格式_FBX格式兼容性及导出步骤【指南】  猪八戒网站制作视频,开发一个猪八戒网站,大约需要多少?或者自己请程序员,需要什么程序员,多少程序员能完成?  Laravel怎么判断请求类型_Laravel Request isMethod用法  香港服务器租用每月最低只需15元?  详解MySQL数据库的安装与密码配置  如何在IIS中配置站点IP、端口及主机头?  企业网站制作这些问题要关注  Laravel如何实现多语言支持_Laravel本地化与国际化(i18n)配置教程  Laravel怎么连接多个数据库_Laravel多数据库连接配置  Laravel辅助函数有哪些_Laravel Helpers常用助手函数大全  Laravel怎么实现观察者模式Observer_Laravel模型事件监听与解耦开发【指南】  如何制作一个表白网站视频,关于勇敢表白的小标题?  Laravel Facade的原理是什么_深入理解Laravel门面及其工作机制  Linux安全能力提升路径_长期防护思维说明【指导】  Android自定义控件实现温度旋转按钮效果  宙斯浏览器文件分类查看教程 快速筛选视频文档与图片方法  高防网站服务器:DDoS防御与BGP线路的AI智能防护方案  Win11怎么设置默认图片查看器_Windows11照片应用关联设置  如何用好域名打造高点击率的自主建站?  Laravel软删除怎么实现_Laravel Eloquent SoftDeletes功能使用教程  如何彻底删除建站之星生成的Banner?  Laravel如何自定义分页视图?(Pagination示例)  Laravel怎么处理异常_Laravel自定义异常处理与错误页面教程