cv2.threshold()函数的作用是将一幅灰度图二值化,基本用法如下:

ret,mask = cv2.threshold(src, dst, thresh, maxval, type)
plt.imshow(mask,cmap='gray')

输出参数:

(ret,mask)ret: 输入的thresh值。
mask: 处理后的图像。
#The first is the threshold that was used and the second output is the thresholded image.

其中关于输入参数有:

(src, dst, thresh, maxval, type)src: 输入源图像(灰度图)。# The first argument is the source image, which should be a grayscale image.
thresh:阈值,用于对像素值进行分类。# The second argument is the threshold value which is used to classify the pixel values.
maxval:最大值(分配给超过阈值的像素值的最大值)。# The third argument is the maximum value which is assigned to pixel values exceeding the threshold.
type:阈值类型,OpenCV提供了由函数的第四个参数给出的不同类型的阈值。# OpenCV provides different types of thresholding which is given by the fourth parameter of the function.

其中五种type类型如下:

    cv.THRESH_BINARY     # 小于阈值的像素点置0,大于阈值的像素点置maxval; cv.THRESH_BINARY_INV    # 此时小于阈值的像素点置maxval,大于阈值的像素点置0;cv.THRESH_TRUNC            # 此时小于阈值的像素点保持原数值,大于阈值的像素点置阈值; cv.THRESH_TOZERO       # 此时小于阈值的像素点置0,大于阈值的像素点保持原数值; cv.THRESH_TOZERO_INV    # 此时小于阈值的像素点保持原数值,大于阈值的像素点置0。

关于不同的type阈值类型的测试代码:

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = 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 range(6):plt.subplot(2,3,i+1),plt.imshow(images[i],'gray',vmin=0,vmax=255)plt.title(titles[i])plt.xticks([]),plt.yticks([])
plt.show()

输出结果是;

参考网址:https://docs.opencv.org/3.4/d7/d4d/tutorial_py_thresholding.html

关于ret,mask = cv2.threshold(src, dst, thresh, maxval, type) 输入输出参数注释(图像二值化处理)相关推荐

  1. opencv 二值化 matlab,opencv-python图像二值化函数cv2.threshold函数详解及参数cv2.THRESH...

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

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

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

  3. 图像二值化 cv2.threshold

    图像二值化 cv2.threshold http://docs.opencv.org/master/d7/d4d/tutorial_py_thresholding.html (一)简单阈值 impor ...

  4. OpenCV-Python学习(10)—— OpenCV 图像二值化处理(cv.threshold)

    1. 学习目标 理解图像的分类,不同类型的图像的区别: 对图像进行二值化处理,对[ cv.threshold ]函数的理解. 2. 图像分类 2.1 不同类型图像说明 按照颜色对图像进行分类,可以分为 ...

  5. [转载] python3 opencv 图像二值化笔记(cv2.adaptiveThreshold)

    参考链接: python opencv 基础6: cv2.threshold()二值图像 前一篇研究了opencv二值化方法threshold的使用,但是这个方法也存在一定的局限性,假如有一张图存在明 ...

  6. python3 opencv 图像二值化笔记(cv2.adaptiveThreshold)

    前一篇研究了opencv二值化方法threshold的使用,但是这个方法也存在一定的局限性,假如有一张图存在明显的明暗不同的区域,如下图 可以看到左边部分因为整体偏暗,导致二值化后变成全黑,丢失了所有 ...

  7. Opencv java 二值化函数threshold (10)

    函数理解 threshold 英语的意思是门槛,门槛的意思说要么在门槛外,要么在门槛内,并且门槛具有一定的条件要求 接下来我们看在opencv中如何使用该函数 Imgproc.threshold(sr ...

  8. OpenCV二值化--cv2.threshold()函数、cv2.adaptiveThreshold()函数

    @[toc] 目录 1.cv2.threshold()函数 2.cv2.adaptiveThreshold()函数 3.3.什么时候用cv2.adaptiveThreshold()函数 一.cv2.t ...

  9. python-opencv-cv2.threshold()二值化函数详解

    文章目录 1.cv2.threshold()参数说明 2.代码示例 1.cv2.threshold()参数说明 cv2.threshold(src, thresh, maxval, type[, ds ...

最新文章

  1. 二叉树:路径之和 Path Sum
  2. VS2005中ajax安装指南[转]
  3. Android UI -- 布局介绍(布局包括FrameLayout, LinearLayout, RelativeLayout, GridLayout)
  4. new,delete总结
  5. 如何恢复ORACLE数据(冷备份)
  6. Everything的下载
  7. 学习笔记~~~~LinkedHashMap
  8. 带你了解数据库的“吸尘器”:VACUUM
  9. php 反射类 解析注释,php反射获取类和方法中的注释
  10. 计算机组成原理—存储系统总结
  11. [Mac OS] Homebrew简介及安装wine
  12. 我的世界服务器修改金币上限,我的世界联机盒子修改金币方法
  13. fdfs-文件上传信息返回详情
  14. 组策略 禁止用户策略下发到指定计算机,使用组策略禁止域中计算机安装特定设备...
  15. 六种正确清理C盘的方法,解决你的红色烦恼
  16. 多通道国产源表之VCSEL老化测试
  17. 【数据增强】用cv2旋转图像并自定义填充背景颜色(主要用到cv2.getRotationMatrix2D 和 cv2.warpAffine)
  18. 免费的PDF在线合并工具,拿走不谢!
  19. 中国工业互联网行业发展预测及行业趋势调研报告2022-2028年版
  20. 强大好用的图片编辑效果实现,支持涂鸦、编辑、马赛克,可撤销和恢复

热门文章

  1. git reset和revert
  2. 英雄联盟无法启动 因计算机中,发生了未知的dx错误,英雄联盟无法启动
  3. 2020年十大数据科学项目创意
  4. 14万天价找“游戏女陪玩”,表白被拒,男主起诉要求退还礼物费?
  5. idea自动生成代码
  6. 存储芯片厂商美光第三财季净利润8.4亿美元 仅上一财年同期两成
  7. webpack使用教程(二)
  8. 算法设计与分析基础 第七章谜题
  9. BTC-加密哈希函数
  10. 值得收藏!考研路上,曾被哪部电影激励?