#阈值分割:全局和局部阈值分割
#主要阈值的选取
import numpy as np
import cv2
src = np.array([[123,234,68],
[33,51,17],
[48,98,234],
[129,89,27],
[45,167,134]])
src[src>150] = 255
src[src<=150] = 0
#opencv提供函数
if __name__ =='__main__':
src = np.array([[123,234,68],
[33,51,17],
[48,98,234],
[129,89,27],
[45,167,134]],np.uint8)
#手动设置阈值
the = 150
maxval = 255
dst = cv2.threshold(src,the,maxval,cv2.THRESH_BINARY)
#Otsu阈值
otsuThe = 0
otsuThe,dst_otsu = cv2.threshold(src,otsuThe,maxval,cv2.THRESH_OTSU)
print(otsuThe,dst_otsu)
#TRIANGLE阈值
triThe = 0
triThe,dst_tri = cv2.threshold(src,triThe,maxval,cv2.THRESH_TRIANGLE+cv2.THRESH_BINARY_INV)
print(triThe,dst_tri)
结果
98.0 [[255 255 0]
[ 0 0 0]
[ 0 0 255]
[255 0 0]
[ 0 255 255]]
232.0 [[255 0 255]
[255 255 255]
[255 255 0]
[255 255 255]
[255 255 255]]