OpenCV图像二值化,Python
原图:
二值化处理(只有黑和白):
import cv2
if __name__=="__main__":
image = cv2.imread("pic.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#超过阈值thresh,值为255,未超过为0。
ret1, p1 = cv2.threshold(src=gray, thresh=127, maxval=255, type=cv2.THRESH_BINARY)
print(ret1)
ret2, p2 = cv2.threshold(src=gray, thresh=127, maxval=255, type=cv2.THRESH_BINARY_INV)
print(ret2)
cv2.imshow('p1', p1)
cv2.imshow('p2', p2)
cv2.waitKey(0)
cv2.destroyAllWindows()
结果: