Python 图像修复效果不佳

问题描述 投票:0回答:1

我正在尝试创建一个Python脚本来从图像中删除水印。为了做到这一点,我决定使用

cv2.inpaint
方法。以下是我删除水印所遵循的步骤:

  1. 从用户处获取图像
  2. 要求用户在水印上涂上阴影
  3. 根据用户的阴影创建遮罩
  4. 使用创建的蒙版修复图像以去除水印

这是我的代码:

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()

这是我尝试删除水印的图像:

这是面膜:

这是输出:

为什么输出这么差?我可以做些什么来提高输出的质量?任何帮助,将不胜感激。谢谢!

python python-3.x numpy opencv image-processing
1个回答
0
投票

cv2.inpaint 不足以获得良好的修复质量。我认为最近的深度学习方法(lamaMAT)是最好的。 (大口罩用MAT比较好)

lama 使用 CNN 编码器-解码器 使用 Transformer 架构的 MAT

两者都需要口罩。

如果您想要详细的文本掩码,请尝试SAMSBSTE

© www.soinside.com 2019 - 2024. All rights reserved.