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 | ndarrayAny, 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)

相关推荐
luckdewei2 小时前
FastAPI 资产管理系统实战:复杂 ORM 关联、Alembic 迁移与 N+1 查询优化
python
aqi008 小时前
15天学会AI应用开发(八)使用向量数据库实现RAG功能
人工智能·python·大模型·ai编程·ai应用
Csvn9 小时前
`functools.lru_cache` —— 一行代码搞定缓存加速
后端·python
金銀銅鐵1 天前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup111 天前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi001 天前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵1 天前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf1 天前
Agent 流程编排
后端·python·agent