基于OpenCV的两种去水印方案(不具有普适性)
可以使用深度学习方法来去修复图像

文章目录

一、基于 inpaint 方法(网上的方法,处理质量较低)

算法理论:基于Telea在2004年提出的基于快速行进的修复算法(FMM算法),先处理待修复区域边缘上的像素点,然后层层向内推进,直到修复完所有的像素点
处理方式:由ui人员制作出黑底白色水印且相同位置的水印蒙版图(必须单通道灰度图),然后使用inpaint方法处理原始图像,具体使用时可把水印区放粗,这样处理效果会好点

# -*- coding: utf-8 -*-

"""
cv2.inpaint(src, inpaintMask, 3, cv2.INPAINT_TELEA)
参数:
目标修复图像;
蒙版图(定位修复区域);
选取邻域半径;
修复算法(INPAINT_TELEA:基于快速行进算法 算法效果较好
INPAINT_NS:基于流体动力学并使用了偏微分方程)
"""

import cv2

src_ = cv2.imread('1111.png')
mask = cv2.imread('2222.png', cv2.IMREAD_GRAYSCALE)
res_ = cv2.resize(src_,None,fx=0.6, fy=0.6, interpolation = cv2.INTER_CUBIC)
mask = cv2.resize(mask,None,fx=0.6, fy=0.6, interpolation = cv2.INTER_CUBIC)
dst = cv2.inpaint(res_, mask, 3, cv2.INPAINT_TELEA)

cv2.imshow('res_', res_)
cv2.imshow('mask', mask)
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

OpenCV—python 图像修复(去除水印)_像素点


测试一张发票图片(不要动歪心思,发票已脱敏)

import cv2
import numpy as np



def Remove_watermark(image):
hue_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
low_range = np.array([140, 100, 90])
high_range = np.array([185, 255, 255])
mask = cv2.inRange(hue_image, low_range, high_range)

kernel = np.ones((3, 3), np.uint8)
dilate_img = cv2.dilate(mask, kernel, iterations=1)
res = cv2.inpaint(image,dilate_img,5,flags=cv2.INPAINT_TELEA)

cv2.imshow('mask_img',mask)
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()

if __name__ == '__main__':
image = cv2.imread('C:\\Users\\xxxx\\Desktop\\piaoju/201920100013253001_30302_01_.jpg')
Remove_watermark(image)

OpenCV—python 图像修复(去除水印)_.net_02

二、基于像素的反色中和(处理质量较高)

参考自ps去水印原理,通过一张白底的反色水印图来中和原图水印

# -*- coding: utf-8 -*-
import cv2
import numpy

src = cv2.imread('1111.png')
mask = cv2.imread('2222.png')
src = cv2.resize(src,None,fx=0.6, fy=0.6, interpolation = cv2.INTER_CUBIC)
mask = cv2.resize(mask,None,fx=0.6, fy=0.6, interpolation = cv2.INTER_CUBIC)
save = numpy.zeros(src.shape, numpy.uint8) #创建一张空图像用于保存

for row in range(src.shape[0]):
for col in range(src.shape[1]):
for channel in range(src.shape[2]):
if mask[row, col, channel] == 0:
val = 0
else:
reverse_val = 255 - src[row, col, channel]
val = 255 - reverse_val * 256 / mask[row, col, channel]
if val < 0: val = 0
save[row, col, channel] = val

cv2.imshow('src', src)
cv2.imshow('mask', mask)
cv2.imshow('save', save)
cv2.waitKey(0)
cv2.destroyAllWindows()

OpenCV—python 图像修复(去除水印)_.net_03


代码有问题,处理完黑片,不知道哪里出问题了,万望大神不吝赐教

# -*- coding: utf-8 -*-
import numpy as np
from numpy import NaN
import cv2


def __make_mask__(image):
hue_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
low_range = np.array([140, 100, 90])
high_range = np.array([185, 255, 255])
th = cv2.inRange(hue_image, low_range, high_range)
index1 = th == 255

mask_img = np.zeros(image.shape, np.uint8)
mask_img[:, :] = (255, 255, 255)
mask_img[index1] = image[index1]
cv2.imshow('mask_img',mask_img)
cv2.waitKey(0)
return image,mask_img

def Remove_watermark(image):
image, mask = __make_mask__(image)
h, w = image.shape[:2]

image = [image[:, :, 0], image[:, :, 1], image[:, :, 2]]
mask = [mask[:, :, 0], mask[:, :, 1], mask[:, :, 2]]
index = [0,1,2]
array_255 = np.full((h, w), 255.0, dtype=np.float32)

result = []
for i,array,mask in zip(index,image,mask):
reverse_val = array_255-array
value = array_255-reverse_val * 256 / mask
value = np.nan_to_num(value)

value = np.where(0 < value, 0,value) # 防止像素溢出
value = np.where(value > 255, 255,value) # 防止像素溢出
value.astype(np.int16)
cv2.imshow('img'+str(i),value)
cv2.waitKey(0)
result.append(value)
return result


if __name__ == '__main__':
img_path = 'C:\\Users\\xxxxx\\Desktop\\piaoju/201920100013253001_30302_01_.jpg'

image = cv2.imread(img_path)
image = cv2.resize(image, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_CUBIC)
result = Remove_watermark(image)
result_img = cv2.merge([result[0],result[1],result[2]])
cv2.imshow('img',image)
cv2.imshow('result_img',result_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

三、颜色库

OpenCV—python 图像修复(去除水印)_.net_04

鸣谢

https://baijiahao.baidu.com/s?id=1624984669133154216&wfr=spider&for=pc