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)

相关推荐
Flittly1 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(3)TodoWrite (待办写入)
python·agent
千寻girling6 小时前
一份不可多得的 《 Django 》 零基础入门教程
后端·python·面试
databook9 小时前
探索视觉的边界:用 Manim 重现有趣的知觉错觉
python·动效
明月_清风10 小时前
Python 性能微观世界:列表推导式 vs for 循环
后端·python
明月_清风10 小时前
Python 性能翻身仗:从 O(n) 到 O(1) 的工程实践
后端·python
helloweilei1 天前
python 抽象基类
python
用户8356290780511 天前
Python 实现 PPT 转 HTML
后端·python
CoovallyAIHub1 天前
语音AI Agent编排框架!Pipecat斩获10K+ Star,60+集成开箱即用,亚秒级对话延迟接近真人反应速度!
深度学习·算法·计算机视觉
zone77391 天前
004:RAG 入门-LangChain读取PDF
后端·python·面试
zone77391 天前
005:RAG 入门-LangChain读取表格数据
后端·python·agent