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)