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)

相关推荐
Psycho_MrZhang3 分钟前
Codex 高效开发协作手册
python
HappyAcmen15 分钟前
1.pdfplumber安装,PDF文字提取
python·pdf
弹简特16 分钟前
【零基础学Python-收尾】10-Python第三方库的安装介绍
开发语言·python
itfallrain29 分钟前
Spring 构造器循环依赖排查:@RequiredArgsConstructor + @Lazy 到底有没有生效
数据库·python·spring
嵌入式老牛34 分钟前
液晶段码(米/日字格)识别—倾斜校正
opencv·算法·仿射变换
大模型任我行40 分钟前
蚂蚁:无师自通的视觉记忆增强
人工智能·计算机视觉·语言模型·论文笔记
小草cys1 小时前
NVIDIA 驱动(550版本)成功安装后安装支持 GPU 加速的 PyTorch
人工智能·pytorch·python
SilentSamsara1 小时前
Python 微服务全链路:gRPC + 链路追踪 + 服务网格接入
开发语言·分布式·python·微服务·架构
Cloud_Shy6181 小时前
解读《Effective Python 3rd Edition》:从练气到老魔(第三章 Item 21 - 24)
开发语言·人工智能·笔记·python·迭代器模式
张高兴3 小时前
张高兴的 Hailo-10 开发指南:(二)使用 LangChain 搭建本地大模型 RAG 问答应用
python·边缘计算·hailo