import cv2
img=cv2.imread('C:/Users/xpp/Desktop/Lena.png')#原始图像
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#将彩色图片转换为灰度图片
t,rst=cv2.threshold(gray,127,255,cv2.THRESH_TOZERO_INV)#超阈值零处理
cv2.imshow("img",img)
cv2.imshow("rst",rst)
cv2.waitKey()
cv2.destroyAllWindows()
算法:超阈值零处理是将图像中大于阈值的像素值设为0(黑色),小于或等于阈值的像素值保持不变。超阈值零处理应用在边缘提取、图像分割、目标识别等领域。
超阈值零处理方式示意图:
例子:
设定阈值为130,即大于130的像素值设为0(黑色),小于或等于130的像素值保持不变。
retval, dst=cv2.threshold(src, thresh, maxval, type)
文献:Otsu, N. . (1979). A thresholding selection method from gray-level histogram. IEEE Trans.syst.man. & Cybern, 9(1), 62-66. Sezgin, M. , & Sankur, B. . (2004). Survey over image thresholding techniques and quantitative performance evaluation. Journal of electronic imaging, 13(1), p.146-168.
注意:超阈值零处理的图像是彩色图像还是灰度图像。通常情况下,超阈值依靠人工经验来自定义。
本文分享自 图像处理与模式识别研究所 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!