http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html
https://zhuanlan.zhihu.com/p/29594637
http://www.bubuko.com/infodetail-2243617.html
图像阀值处理
cv2.threshold()
cv2.THRESH_BINARY #二值化处理,低于阈值的像素点灰度值置为0;高于阈值的值置为参数3
cv2.THRESH_BINARY_INV #大于阈值的像素点灰度值置为0;小于阈值置为参数3
cv2.THRESH_TRUNC #小于阈值的像素点灰度值不变,大于阈值的像素点置为该阈值
cv2.THRESH_TOZERO #小于阈值的像素点灰度值不变,大于阈值的像素点置为0,其中参数3任取
cv2.THRESH_TOZERO_INV 大于阈值的像素点灰度值不变,小于阈值的像素点置为0,其中参数3任取
img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #转为灰度图
>>> import numpy as np
>>> from matplotlib import pyplot as plt
>>> ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
>>> ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
>>> ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
>>> ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
>>> ret,thresh5 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)
>>> titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
>>> images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
>>> for i in xrange(6):
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
image.png
网友评论