OpenCV-Python实战(7)——阈值处理

一、cv2.threshold()

res, dst = cv2.threshold(src=*,thresh=*,maxval=*,type=*) 

 res:函数返回的阈值。

dst:阈值处理后的函数。

src:要处理的图像。

thresh:阈值。

maxval:设定像素最大值。

type:阈值函数处理方法,常见方法如下表所示:

方法解释
THRESH_BINARY0大于阈值取最大值,其他值取0
THRESH_BINARY_INV1大于阈值取0,其他为最大值
THRESH_TRUNC2大于阈值取阈值,其他值不变
THRESH_TOZERO3大于阈值不变,其他值取0
THRESH_TOZERO_INV4大于阈值取0,其他值不变
THRESH_OTSU8使用算法自动计算阈值
THRESH_TRIANGLE16使用三角形算法自动计算阈值

1.1 自定义阈值 

import cv2
import numpy as np

car = cv2.imread('car.png',cv2.IMREAD_GRAYSCALE )
car = car[::2,::2]

res1, dst1 = cv2.threshold(src=car,thresh=100,maxval=255,type=cv2.THRESH_BINARY)
res2, dst2 = cv2.threshold(src=car,thresh=100,maxval=255,type=cv2.THRESH_BINARY_INV)
res3, dst3 = cv2.threshold(src=car,thresh=100,maxval=255,type=cv2.THRESH_TRUNC)
res4, dst4 = cv2.threshold(src=car,thresh=100,maxval=255,type=cv2.THRESH_TOZERO)
res5, dst5 = cv2.threshold(src=car,thresh=100,maxval=255,type=cv2.THRESH_TOZERO_INV)

cv2.imshow('car',car)
cv2.imshow('THRESH_BINARY',dst1)
cv2.imshow('THRESH_BINARY_INV',dst2)
cv2.imshow('THRESH_TRUNC',dst3)
cv2.imshow('THRESH_TOZERO',dst4)
cv2.imshow('THRESH_TOZERO_INV',dst5)

cv2.waitKey(0)
cv2.destroyAllWindows()

 1.2  自动阈值

import cv2

car = cv2.imread('car.png',cv2.IMREAD_GRAYSCALE )
car = car[::2,::2]

res1, dst1 = cv2.threshold(src=car,thresh=100,maxval=255,type=cv2.THRESH_BINARY)
res2, dst2 = cv2.threshold(src=car,thresh=100,maxval=255,type=(cv2.THRESH_BINARY+cv2.THRESH_OTSU))
res3, dst3 = cv2.threshold(src=car,thresh=100,maxval=255,type=(cv2.THRESH_BINARY+cv2.THRESH_TRIANGLE))

cv2.imshow('car',car)
cv2.imshow('THRESH_BINARY',dst1)
cv2.imshow('THRESH_OTSU',dst2)
cv2.imshow('THRESH_TRIANGLE',dst3)

print("THRESH_OTSU方法的计算阈值:",res2)
print("THRESH_TRIANGLE方法的计算阈值:",res3)
cv2.waitKey(0)
cv2.destroyAllWindows()

 从结果可以看出THRESH_OTSU方法的计算阈值比较可靠,能够很好的显示物体的轮廓。

 二、cv2.adaptiveThreshold()

res, dst = cv2.adaptiveThreshold(src=*,maxval=*,adaptiveMethod=*,thresholdType=*,blockSize=,C=*)

res:函数返回的阈值。

maxval:设定像素最大值。

adaptiveMethod:

算术平均法cv2.ADAPTIVE_THRESH_MEAN_C、将周围区域的平均值当作阈值,再减去参数C
高斯加权和法cv2.ADAPTIVE_THRESH_GAUSSIAN_C利用高斯函数方法对中心点和周围区域进行加权计算当作阈值,在减去参数C

thresholdType:阈值处理方法。必须为:THRESH_BINARY 或 THRESH_BINARY_INV

blockSize:阈值计算核,3*3或5*5或7*7等。

C:常数。

import cv2

car = cv2.imread('car.png',cv2.IMREAD_GRAYSCALE )
car = car[::2,::2]

res1, dst1 = cv2.threshold(src=car,thresh=100,maxval=255,type=cv2.THRESH_BINARY)
dst2 = cv2.adaptiveThreshold(src=car,maxValue=255,adaptiveMethod=cv2.ADAPTIVE_THRESH_MEAN_C,thresholdType=cv2.THRESH_BINARY,blockSize=7,C=0)
dst3 = cv2.adaptiveThreshold(src=car,maxValue=255,adaptiveMethod=cv2.ADAPTIVE_THRESH_GAUSSIAN_C,thresholdType=cv2.THRESH_BINARY,blockSize=7,C=0)

cv2.imshow('car',car)
cv2.imshow('THRESH_BINARY',dst1)
cv2.imshow('ADAPTIVE_THRESH_MEAN_C',dst2)
cv2.imshow('ADAPTIVE_THRESH_GAUSSIAN_C',dst3)

cv2.waitKey(0)
cv2.destroyAllWindows()

可以调整 blockSize=?进行调试,blockSize越小,绘制的二值图像轮廓越细节。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小码贾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值