简单阈值
对每一个像素都应用相同的阈值。如果像素值小于阈值,则将其设置为0,否则设置为最大值。
函数:
retval, dst=cv.threshold(src, thresh, maxval, type[, dst])对每个像素使用固定的阈值
参数:
src
输入图像 ,灰度图(多通道, 8-bit or 32-bit floating point).
dst
与src具有相同大小、类型和通道数的输出数组。
thresh
阈值
maxval
当像素值超过了阈值(或者小于阈值,根据type来决定),所赋予的值
type
返回值:
retVal:使用的阈值,在Otsu‘s中会用到
dst: 经过阈值处理的图像
阈值类型:
举例:
importcv2 as cvimportnumpy as npfrom matplotlib importpyplot as plt
img= cv.imread('4.jpg', 0)
ret, thresh1= cv.threshold(img, 127, 255, cv.THRESH_BINARY)
ret, thresh2= cv.threshold(img, 127, 255, cv.THRESH_BINARY_INV)
ret, thresh3= cv.threshold(img, 127, 255, cv.THRESH_TRUN