我正在尝试创建一个Python脚本来从图像中删除水印。为了做到这一点,我决定使用
cv2.inpaint
方法。以下是我删除水印所遵循的步骤:
这是我的代码:
import cv2
import numpy as np
drawing = False # true if mouse is pressed
pt1_x , pt1_y = None , None
def nothing(x):
pass
def line_drawing(event,x,y,flags,param):
global pt1_x,pt1_y,drawing
if event==cv2.EVENT_LBUTTONDOWN:
drawing=True
pt1_x,pt1_y=x,y
elif event==cv2.EVENT_MOUSEMOVE:
if drawing==True:
cv2.line(img,(pt1_x,pt1_y),(x,y),color=(0,0,255),thickness= cv2.getTrackbarPos('Thickness','Damaged Image'))
cv2.line(mask, (pt1_x, pt1_y), (x, y), color=(255,255,255), thickness=cv2.getTrackbarPos('Thickness', 'Damaged Image'))
pt1_x,pt1_y=x,y
elif event==cv2.EVENT_LBUTTONUP:
drawing=False
cv2.line(img,(pt1_x,pt1_y),(x,y),color=(0,0,255),thickness= cv2.getTrackbarPos('Thickness','Damaged Image'))
cv2.line(mask, (pt1_x, pt1_y), (x, y), color=(255,255,255), thickness=cv2.getTrackbarPos('Thickness', 'Damaged Image'))
img = cv2.imread('D:\\Watermark.jpg')
ratio = img.shape[1]/img.shape[0]
height = 600
width = height * ratio
img = cv2.resize(img, (int(width),height))
mask = np.zeros(img.shape[:-1], dtype = np.uint8)
#mask = cv2.integral(mask)
cv2.namedWindow('Mask')
cv2.namedWindow('Damaged Image')
cv2.setMouseCallback('Damaged Image',line_drawing)
cv2.createTrackbar('Thickness','Damaged Image', 10, 50, nothing)
while(1):
cv2.imshow('Damaged Image',img)
cv2.imshow('Mask', mask)
if cv2.waitKey(1) & 0xFF == 27:
break
elif cv2.waitKey(1) & 0xFF == ord('a'):
pass
elif cv2.waitKey(1) & 0xFF == ord('s'):
restored = cv2.inpaint(img, mask, 3, cv2.INPAINT_TELEA)
cv2.namedWindow('Restored Image')
cv2.imshow('Restored Image', restored)
cv2.destroyAllWindows()
这是我尝试删除水印的图像:
这是面膜:
这是输出:
为什么输出这么差?我可以做些什么来提高输出的质量?任何帮助,将不胜感激。谢谢!