opencv-075-图像去水印/修复

知识点

修复API:

1
2
3
4
5
6
7
8
9
void cv::inpaint(
InputArray src,
InputArray ipaintMask,
OutputArray dst
double ipaintRadius, // 考虑周围像素范围
int flags //修复方法
)
基于Navier-Stokes的修复方法
基于图像梯度的快速匹配方法又称(Telea法)

代码(c++,python)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

/*
* 图像去水印/修复
*/
int main() {
Mat src = imread("../images/wm.jpg");
//Mat src = imread("../images/master2.jpg");
if (src.empty()) {
cout << "could not load image.." << endl;
}
imshow("input", src);

Mat hsv, mask, result;
// 得到mask
cvtColor(src, hsv, COLOR_BGR2HSV);
inRange(hsv, Scalar(100, 43, 46), Scalar(124, 255, 255), mask);
Mat se = getStructuringElement(MORPH_RECT, Size(5, 5));
dilate(mask, mask, se);
imshow("mask", mask);

// 修复
inpaint(src, mask, result, 3, INPAINT_TELEA);
imshow("result", result);

waitKey(0);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import cv2 as cv

if __name__ == '__main__':
src = cv.imread("D:/images/master2.jpg")
cv.imshow("watermark image", src)
hsv = cv.cvtColor(src, cv.COLOR_BGR2HSV)
mask = cv.inRange(hsv, (100, 43, 46), (124, 255, 255))
cv.imshow("mask", mask)
cv.imwrite("D:/mask.png", mask)
se = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))
cv.dilate(mask, se, mask)
result = cv.inpaint(src, mask, 3, cv.INPAINT_TELEA)
cv.imshow("result", result)
cv.imwrite("D:/result.png", result)
cv.waitKey(0)
cv.destroyAllWindows()

结果

代码地址

github