opencv-075-图像去水印/修复 发表于 2019-04-23 | 分类于 opencv 知识点修复API: 123456789void cv::inpaint( InputArray src, InputArray ipaintMask, OutputArray dst double ipaintRadius, // 考虑周围像素范围 int flags //修复方法)基于Navier-Stokes的修复方法基于图像梯度的快速匹配方法又称(Telea法) 代码(c++,python)1234567891011121314151617181920212223242526272829303132#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;} 12345678910111213141516import cv2 as cvif __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