6.1 阈值处理-- threshold函数
《OpenCV 轻松入门 面向Python》 学习笔记
阈值处理
- 1. 二值化阈值处理(cv2.THRESH_BINARY)
- 2. 反二值化阈值处理(cv2.THRESH_BINARY_INV)
- 3. 截断阈值化处理(cv2.THRESH_TRUNC)
- 4. 超阈值零处理(cv2.THRESH_TOZERO_INV)
- 5. 低阈值零处理(cv2.THRESH_TOZERO)
函数原型:
retval, dst = cv2.threshold(src, thresh, maxval, type)
参数:
- retval:返回的阈值
- dst:阈值分割结果图像
- src:原图像, 可以是多通道的
- thresh:要设定的阈值
- maxval:当type类型是THRESH_BINARY时,需要设定的最大值
- type:阈值分割的方式
type 类型有:
下面就按照type的类型一个一个的举例
1. 二值化阈值处理(cv2.THRESH_BINARY)
设定阈值 thresh=127
设定maxval=255
图像中大于阈值127的像素,值替换为maxval=255
图像中小于阈值127的像素,值替换为0
import cv2
import numpy as npimage = np.random.randint(0, 256, (4, 5), dtype=np.uint8)
t, rst = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)print("image=", image)
print("\nthreshold=", t)
print("\nrst=", rst)# 输出为:
# image= [[ 4 109 59 135 170]
# [ 14 125 164 250 93]
# [134 26 15 158 34]
# [212 159 28 146 90]]
#
# threshold= 127.0
#
# rst= [[ 0 0 0 255 255]
# [ 0 0 255 255 0]
# [255 0 0 255 0]
# [255 255 0 255 0]]
2. 反二值化阈值处理(cv2.THRESH_BINARY_INV)
反二值化阈值处理的结果也是仅有两个值的二值图像,与二值化预处理的区别在于,大于阈值的像素,值替换为0; 小于阈值的像素,值替换为255
设定阈值 thresh=127
设定maxval=255
图像中小于阈值127的像素,值替换为maxval=255
图像中大于阈值127的像素,值替换为0
import cv2
import numpy as npimage = np.random.randint(0, 256, (4, 5), dtype=np.uint8)
t, rst = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY_INV)print("image=", image)
print("\nthreshold=", t)
print("\nrst=", rst)# 输出为:
# image= [[244 27 239 125 18]
# [ 44 47 242 101 134]
# [242 58 63 246 113]
# [ 34 24 187 78 50]]
#
# threshold= 127.0
#
# rst= [[ 0 255 0 255 255]
# [255 255 0 255 0]
# [ 0 255 255 0 255]
# [255 255 0 255 255]]
3. 截断阈值化处理(cv2.THRESH_TRUNC)
截断阈值化处理会将图像中大于阈值的像素点的值设定为等于阈值,小于阈值的像素点保持不变。
import cv2
import numpy as npimage = np.random.randint(0, 256, (4, 5), dtype=np.uint8)
t, rst = cv2.threshold(image, 127, 255, cv2.THRESH_TRUNC)print("image=", image)
print("\nthreshold=", t)
print("\nrst=", rst)# 输出为:
# image= [[175 197 149 17 91]
# [107 196 150 124 166]
# [242 0 158 219 145]
# [133 89 30 214 212]]
#
# threshold= 127.0
#
# rst= [[127 127 127 17 91]
# [107 127 127 124 127]
# [127 0 127 127 127]
# [127 89 30 127 127]]
4. 超阈值零处理(cv2.THRESH_TOZERO_INV)
截断阈值化处理会将图像中大于阈值的像素点的值设定为0,小于阈值的像素点保持不变。
import cv2
import numpy as npimage = np.random.randint(0, 256, (4, 5), dtype=np.uint8)
t, rst = cv2.threshold(image, 127, 255, cv2.THRESH_TOZERO_INV)print("image=", image)
print("\nthreshold=", t)
print("\nrst=", rst)# 输出为:
# image= [[135 144 46 178 228]
# [183 218 141 105 60]
# [143 32 5 21 4]
# [128 131 73 66 175]]
#
# threshold= 127.0
#
# rst= [[ 0 0 46 0 0]
# [ 0 0 0 105 60]
# [ 0 32 5 21 4]
# [ 0 0 73 66 0]]
5. 低阈值零处理(cv2.THRESH_TOZERO)
截断阈值化处理会将图像中小于阈值的像素点的值设定为0,大于阈值的像素点保持不变。
import cv2
import numpy as npimage = np.random.randint(0, 256, (4, 5), dtype=np.uint8)
t, rst = cv2.threshold(image, 127, 255, cv2.THRESH_TOZERO)print("image=", image)
print("\nt=", t)
print("\nrst=", rst)# 输出为:
# image= [[ 66 192 240 149 105]
# [ 45 45 17 166 178]
# [ 71 119 147 185 106]
# [ 7 160 137 193 114]]
#
# t= 127.0
#
# rst= [[ 0 192 240 149 0]
# [ 0 0 0 166 178]
# [ 0 0 147 185 0]
# [ 0 160 137 193 0]]
6.1 阈值处理-- threshold函数相关推荐
- opencv阈值处理-threshold函数、自适应阈值处理和Ostu处理
阈值处理是指剔除图像内像素值高于一定值或者低于一定值的像素点.例如设置阈值为125,则大于125的像素点的值设为255,小于125的设为0. 一.threshold函数 open cv3.0使用cv2 ...
- 【笔记】opencv阈值处理 threshold函数 cv2.THRESH_BINARY ,cv2.THRESH_TRUNC) cv2.adaptiveThreshold()
像素是在模拟图像数字化时对连续空间进行离散化得到的.每个像素具有整数行(高)和列(宽)位置坐标,同时每个像素都具有整数灰度值或颜色值. threshold函数 返回的第一个参数为阈值,第二个为结果图像 ...
- otsu java,opencv阈值处理--threshold函数、自适应阈值处理、Otsu处理(大津法)
threshold函数 retval, dst = cv2.threshold(src, thresh, maxval, type) ''' retval:返回的阈值:dst:阈值分割结果图像 src ...
- 图像阈值处理cv2.threshold()函数(python)
内容 cv2.threshold()函数:在opencv中比较常用,但一开始不是很理解是什么意思. 下面是官方文档中给的解释 Python: cv2.threshold(src, thresh, ma ...
- python基于模型的预测概率和标签信息可视化ROC曲线、编写自定义函数计算约登值、寻找最佳阈值(threshold、cutoff)、可视化ROC曲线并在曲线中标记最佳阈值及其数值标签
python基于模型的预测概率和标签信息可视化ROC曲线.编写自定义函数计算约登值.寻找最佳阈值(threshold.cutoff).可视化ROC曲线并在曲线中标记最佳阈值及其数值标签 目录
- python编写自定义函数计算约登值(约登指数、Youden Index)、寻找最佳阈值(threshold、cutoff)、可视化ROC曲线并在曲线中标记最佳阈值及其数值标签
python编写自定义函数计算约登值(约登指数.Youden Index).寻找最佳阈值(threshold.cutoff).可视化ROC曲线并在曲线中标记最佳阈值及其数值标签 目录
- opencv 阈值分割 — threshold()
OpenCV阈值分割函数:threshold() 函数原型: double threshold(InputArray src, OutputArray dst, double thresh, doub ...
- 计算机视觉开源库OpenCV之threshold()函数详解
cv2.threshold()函数作用:去掉噪,例如过滤很小或很大像素值的图像点. cv2.threshold()函数python版原型: retval, dst = cv.threshold(src ...
- threshold函数
threshold函数 函数的作用: 对图像中的像素进行阈值处理,进行分割,常用于二值化处理 函数调用形式: C++:double threshold(InputArray src, OutputAr ...
- opencv python cv2.threshold()函数报错 TypeError: Expected cv::UMat for argument 'mat'
错误信息: 解决办法: 原来cv2.threshold()函数有俩返回值,我这只用了一个接收... 改成俩就好了: 参考文章1:图像阈值处理cv2.threshold()函数(python) 参考文章 ...
最新文章
- TheBrain v11.0.84中文版
- cnetos7 vncserver安装与配置
- 数据挖掘十大经典算法之——PageRank 算法
- web.xml详细介绍
- Codeforces Beta Round #97 (Div. 1) C. Zero-One 数学
- Windows下使用taskkill 命令批量结束进程
- python吃香吗_python编程为何这么吃香
- Python、Go、JavaScript、Rust 将长盛 5 年!
- java header file_javah 生成header file 报错 问题解决
- 用Matlab分享一个软件低通滤波算法
- 机器人仿真搭建(以ABB为例)
- 向98年的华为学习:没有高管办公室的青铜器软件
- shell脚本练习题(编程题)。
- 实践练习二(必选):手动部署 OceanBase 单副本集群
- 音频的相关基础知识,这里有
- React类式组件基础内容补充
- 定义一个类,提供显示圆的周长的方法,提供显示圆的面积的方法
- Java性能优化推荐书!java原生开发是什么意思
- 异或、异或和 的性质与应用
- 双系统下ubuntu16.04备份和还原、彻底删除和重装(包含迁移) 亲跳多坑!!
热门文章
- 什么叫pin脚的pad_超详细的 摄像头PIN脚功能作用
- 个人博客登录注册部分
- GIS等级考试知识集锦
- ionic在app内部打开pdf文件
- python可视化数据分析交互作用_R数据分析:双分类变量的交互作用作图
- C#导出数据—使用Word模板
- An effective intrusion-resilient mechanism for PLCs against data tampering attacks
- AT89C51单片机制作简易密码锁
- 矩阵相乘求导(转载)
- r语言如何计算t分布临界值_医学统计与R语言:这个Calibration plot有点色!