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)

相关推荐
Warson_L2 小时前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅2 小时前
海天线算法的前世今生
python·计算机视觉
韩师傅2 小时前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L2 小时前
LangGraph的MessageState and HumanMessage
python
韩师傅3 小时前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
Warson_L4 小时前
python的类&继承
python
Warson_L4 小时前
类型标注/type annotation
python
ThreeS6 小时前
手搓MiniVLA全实战教程-一步一步用pytorch解释原理与思路
人工智能·python
金銀銅鐵7 小时前
[Python] 模 n 乘法的逆元计算器
python·数学·游戏