OpenCV—python图像修复(去除水印)
OpenCV—python图像修复(去除⽔印)
拔丝地瓜基于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_ = size(src_,None,fx=0.6, fy=0.6, interpolation = cv2.INTER_CUBIC)
mask = size(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()
测试⼀张发票图⽚(不要动歪⼼思,发票已脱敏)
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)
⼆、基于像素的反⾊中和(处理质量较⾼)
参考⾃ps去⽔印原理,通过⼀张⽩底的反⾊⽔印图来中和原图⽔印
# -*- coding: utf-8 -*-
import cv2
import numpy
src = cv2.imread('1111.png')
mask = cv2.imread('2222.png')洛克王国武斗谋士
src = size(src,None,fx=0.6, fy=0.6, interpolation = cv2.INTER_CUBIC) mask = size(mask,None,fx=0.6, fy=0.6, interpolation = cv2.INTER_CUBIC) save = s(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()
代码有问题,处理完⿊⽚,不知道哪⾥出问题了,万望⼤神不吝赐教
# -*- 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 = size(image,None, fx=0.5, fy=0.5, interpolation=cv2.INTER_CUBIC)
result = Remove_watermark(image)
result_img = ([result[0],result[1],result[2]])
cv2.imshow('img',image)
cv2.imshow('result_img',result_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
三、颜⾊库

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。