1.简单的阀值如上章所示,但是需要经验值确定阀值,很多情况不合适
2.采用一种自适应阀值处理图像
import cv2 import numpy as np img= cv2.imread('bookpage.jpg') grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #自适应阀值 gaus = cv2.adaptiveThreshold(grayscaled,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,115,1) cv2.imshow('after',gaus) cv2.imshow('img',img) cv2.waitKey(0) cv2.destroyAllWindows()
3.另一种版本的门限是 Otsu's threshold,在这里效果并不好
retval2,otsu = cv2.threshold(grayscaled,125,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)