使用自适应阈值处理图像

使用自适应阈值处理图像

import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('picture\\head_show.jpg', 0)
thresh1 = cv.adaptiveThreshold(img, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 119, 5)
thresh2 = cv.adaptiveThreshold(img, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 119, 5)
titles = ['img', 'MEAN_C', 'GAUSSIAN_C']
images = [img, thresh1, thresh2]
for i in range(3):
    plt.subplot(1, 3, i+1), plt.imshow(images[i]), plt.title(titles[i])
plt.show()

cv.adaptivThreshold()参数如下:
adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst]) -> dst
需要注意的是thresholdType里cv.THRESH_TRUNC的类型不支持,会报错。
同时blocSize为像素周围指定多少距离的区域,为3, 5, 9…等除1外的奇数,即满足blockSize % 2 == 1 && blockSize > 1
在全局阈值的方法cv.threshold中需要返回两个值ret、thresh
但在自适应阈值的方法cv.adaptiveThreshold中只返回一个值,加上ret会报错,同时输入图像需为灰度图像

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值