一、cv2.threshold()
res, dst = cv2.threshold(src=*,thresh=*,maxval=*,type=*)
res:函数返回的阈值。
dst:阈值处理后的函数。
src:要处理的图像。
thresh:阈值。
maxval:设定像素最大值。
type:阈值函数处理方法,常见方法如下表所示:
方法 | 值 | 解释 |
THRESH_BINARY | 0 | 大于阈值取最大值,其他值取0 |
THRESH_BINARY_INV | 1 | 大于阈值取0,其他为最大值 |
THRESH_TRUNC | 2 | 大于阈值取阈值,其他值不变 |
THRESH_TOZERO | 3 | 大于阈值不变,其他值取0 |
THRESH_TOZERO_INV | 4 | 大于阈值取0,其他值不变 |
THRESH_OTSU | 8 | 使用算法自动计算阈值 |
THRESH_TRIANGLE | 16 | 使用三角形算法自动计算阈值 |
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越小,绘制的二值图像轮廓越细节。
标签:BINARY,阈值,src,Python,car,cv2,OpenCV,THRESH From: https://blog.csdn.net/2201_75538245/article/details/144769220