OpenCV—python 图像修复(去除水印)
opencv
OpenCV: 开源计算机视觉库
项目地址:https://gitcode.com/gh_mirrors/opencv31/opencv

·
基于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()
测试一张发票图片(不要动歪心思,发票已脱敏)
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
OpenCV: 开源计算机视觉库
项目地址:https://gitcode.com/gh_mirrors/opencv31/opencv
二、基于像素的反色中和(处理质量较高)
参考自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()
代码有问题,处理完黑片,不知道哪里出问题了,万望大神不吝赐教
# -*- 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()
三、颜色库
鸣谢
https://my.oschina.net/u/2400083/blog/732321
https://www.cnblogs.com/lgh344902118/p/7928054.html
https://baijiahao.baidu.com/s?id=1624984669133154216&wfr=spider&for=pc
https://blog.csdn.net/Maximun/article/details/85064274
推荐内容
阅读全文
AI总结




OpenCV: 开源计算机视觉库
最近提交(Master分支:7 个月前 )
6ef57463
Migrated IPP impl for flip and transpose to HAL 4 天前
c1d71d53
imgproc: disable SIMD for compareHist(INTERSECT) if f64 is unsupported #27220
Close https://github.com/opencv/opencv/issues/24757
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
5 天前
更多推荐
相关推荐
查看更多
opencv

OpenCV: 开源计算机视觉库
opencv

opencv

R bindings for OpenCV
热门开源项目
活动日历
查看更多
直播时间 2025-04-09 14:34:18

樱花限定季|G-Star校园行&华中师范大学专场
直播时间 2025-04-07 14:51:20

樱花限定季|G-Star校园行&华中农业大学专场
直播时间 2025-03-26 14:30:09

开源工业物联实战!
直播时间 2025-03-25 14:30:17

Heygem.ai数字人超4000颗星火燎原!
直播时间 2025-03-13 18:32:35

全栈自研企业级AI平台:Java核心技术×私有化部署实战
所有评论(0)