import cv2
import numpy as np
def Eliminate_Watermarks_and_Colors(path):#'1.jpg'
img = cv2.imread(path)
for i in range(6):
img = np.clip(2.0 * img - 160, 0, 255).astype(np.uint8)#锐化保证经常计算后的img能够不越界,一致保持在[0,255]之间。
# image = cv2.imread('1.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 45, 255, cv2.THRESH_BINARY_INV)[1]
thresh = 255 - thresh
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
result = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)
cv2.imwrite(path, result)
# cv2.waitKey()
如果没有经过这一步骤,则导致如下不清晰的结果
for i in range(6):
img = np.clip(2.0 * img - 160, 0, 255).astype(np.uint8)