opencv+python(二值化图像)

1、全局二值化:将图像全部变成两种值,比如:0,255

threshold(src: ndarray(图像,最好是灰度图)

thresh: float,(阙值)

maxval: float,(最大值)

type: int,(操作类型)

dst: ndarray(输出图片)

复制代码
img = cv2.imread('3.jpg')
 gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    #注意该函数会返回两个值,一个是阈值,一个是处理后的图片
    thresh,dst=cv2.threshold(img,127,255,cv2.THRESH_BINARY)
    cv2.imshow("dst", dst)

    cv2.waitKey(0)

2、自适应阈值二值化

返回值只有一个

adaptiveThreshold(src: Mat | ndarray[Any, dtype[generic]] | ndarray,

maxValue: float,

adaptiveMethod: int, (指定计算阈值的方法)

thresholdType: int,

blockSize: int,(计算阈值的区域大小,只能为奇数如:3,5,7)

C: float,(计算出来的值会减去这个常数)

dst: Mat

复制代码
#调整窗口的大小
    cv2.namedWindow('dst',cv2.WINDOW_NORMAL)
    cv2.resizeWindow('dst',800,600)
    
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    dst=cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,0)
    cv2.imshow("dst", dst)

    cv2.waitKey(0)

3、 #调整窗口的大小

cv2.namedWindow('dst',cv2.WINDOW_NORMAL)

cv2.resizeWindow('dst',800,600)

相关推荐
倒霉熊dd1 小时前
Python学习(第一部分 语法与数据结构/核心基础)
大数据·python·学习·pip
仅此,1 小时前
deep agent整合 DeepSeek 记录
python·langchain·agent·deep agent sdk
ftpeak2 小时前
AI开发之LangGraph教程6~自定义状态 (Custom State)
python·ai·langchain·langgraph
m0_738120722 小时前
渗透测试——Djinn1靶场详细渗透提权过程讲解(绕过黑名单限制,命令执行反弹shell,pyc反编译,代码白盒分析,python沙盒逃逸)
开发语言·python·php
ZPC82102 小时前
CPU 核心隔离 + 线程绑核 + 实时优先级 SCHED_FIFO
人工智能·算法·计算机视觉·机器人
Ares-Wang3 小时前
AI》》欧氏距离、曼哈顿距离 切比雪夫距离 等
人工智能·python
陈eaten3 小时前
windows上协调多版本python以及虚拟环境
开发语言·windows·python·pycharm·pip·虚拟环境·py
一晌小贪欢3 小时前
告别 `datetime` 混乱:使用 Python 类型注解构建健壮的时间处理管道
开发语言·python·时间·时间类型·时间模块
嘛?2507013 小时前
Python高阶基础
python
li星野3 小时前
哈希表通关八题:从两数之和到LRU缓存,手撕高频面试题(Python + C++)
python·缓存·散列表