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)

相关推荐
q12345678909826 分钟前
FNN sin predict
开发语言·python
先做个垃圾出来………39 分钟前
Python字节串“b“前缀
开发语言·python
dreams_dream1 小时前
什么是迭代器和生成器
python
悠闲蜗牛�2 小时前
深入浅出Spring Boot 3.x:新特性全解析与实战指南
开发语言·python
xinhuanjieyi2 小时前
python获取租房70页信息,为了安全隐去了真实网址
开发语言·python
gzroy2 小时前
量化金融实践-海龟交易法
python·金融
sg_knight2 小时前
适配器模式(Adapter)
python·设计模式·适配器模式·adapter
52Hz1183 小时前
力扣20.有效的括号、155.最小栈
python·算法·leetcode
吾在学习路3 小时前
SAMCT: Segment Any CT Allowing Labor-Free Task-Indicator Prompts
深度学习·计算机视觉