From fa84954ba1cbba1f1e767bcd89fe4183af169632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=A3=8A?= <295628068@qq.com> Date: Sun, 3 Jun 2018 21:41:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E5=BD=A2=E4=BF=AE=E5=A4=8D=E6=96=87?= =?UTF-8?q?=E6=A1=A3=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/inpaint.md | 50 ++++++++++++++++++++++++++++++++++++++++ faceai/opencv/inpaint.py | 9 ++++++-- 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 doc/inpaint.md diff --git a/doc/inpaint.md b/doc/inpaint.md new file mode 100644 index 00000000..996359a0 --- /dev/null +++ b/doc/inpaint.md @@ -0,0 +1,50 @@ +# 图片修复程序-可用于水印去除 # + +在现实的生活中,我们可能会遇到一些美好的或是珍贵的图片被噪声干扰,比如旧照片的折痕,比如镜头上的灰尘或污渍,更或者是某些我们想为我所用但有讨厌水印,那么有没有一种办法可以消除这些噪声呢? + +答案是肯定的,依然是被我们用了无数次的OpenCV这款优秀的框架。 + +# 效果预览 # +![](http://icdn.apigo.cn/inpaint.png?1) + +# 图片修复原理 # +那OpenCV究竟是怎么实现的,简单的来说就是开发者标定噪声的特征,在使用噪声周围的颜色特征推理出应该修复的图片的颜色,从而实现图片修复的。 + +# 程序实现解析 # +1. 标定噪声的特征,使用cv2.inRange二值化标识噪声对图片进行二值化处理,具体代码:cv2.inRange(img, np.array([240, 240, 240]), np.array([255, 255, 255])),把[240, 240, 240]~[255, 255, 255]以外的颜色处理为0; +1. 使用OpenCV的dilate方法,扩展特征的区域,优化图片处理效果; +2. 使用inpaint方法,把噪声的mask作为参数,推理并修复图片; + +# 完整代码 # +``` +#coding=utf-8 +#图片修复 + +import cv2 +import numpy as np + +path = "img/inpaint.png" + +img = cv2.imread(path) +hight, width, depth = img.shape[0:3] + +#图片二值化处理,把[240, 240, 240]~[255, 255, 255]以外的颜色变成0 +thresh = cv2.inRange(img, np.array([240, 240, 240]), np.array([255, 255, 255])) + +#创建形状和尺寸的结构元素 +kernel = np.ones((3, 3), np.uint8) + +#扩张待修复区域 +hi_mask = cv2.dilate(thresh, kernel, iterations=1) +specular = cv2.inpaint(img, hi_mask, 5, flags=cv2.INPAINT_TELEA) + +cv2.namedWindow("Image", 0) +cv2.resizeWindow("Image", int(width / 2), int(hight / 2)) +cv2.imshow("Image", img) + +cv2.namedWindow("newImage", 0) +cv2.resizeWindow("newImage", int(width / 2), int(hight / 2)) +cv2.imshow("newImage", specular) +cv2.waitKey(0) +cv2.destroyAllWindows() +``` diff --git a/faceai/opencv/inpaint.py b/faceai/opencv/inpaint.py index 6a616cd2..22f773d5 100644 --- a/faceai/opencv/inpaint.py +++ b/faceai/opencv/inpaint.py @@ -9,9 +9,14 @@ img = cv2.imread(path) hight, width, depth = img.shape[0:3] +#图片二值化处理,把[240, 240, 240]~[255, 255, 255]以外的颜色变成0 thresh = cv2.inRange(img, np.array([240, 240, 240]), np.array([255, 255, 255])) -kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) -hi_mask = cv2.dilate(thresh, kernel, iterations=3) + +#创建形状和尺寸的结构元素 +kernel = np.ones((3, 3), np.uint8) + +#扩张待修复区域 +hi_mask = cv2.dilate(thresh, kernel, iterations=1) specular = cv2.inpaint(img, hi_mask, 5, flags=cv2.INPAINT_TELEA) cv2.namedWindow("Image", 0)