This article mainly introduces you to the relevant information about the python image repair program, which can be used to remove watermarks in images. It is mainly implemented using the OpenCV framework. The article introduces it in detail through the example code. What is needed Friends can refer to it, let’s take a look below
Picture repair program-can be used for watermark removal
In real life , we may encounter some beautiful or precious pictures that are disturbed by noise, such as creases in old photos, dust or stains on the lens, or some pictures that we want to use but have annoying watermarks, then Is there a way to eliminate these noises?
The answer is yes, it is still the excellent OpenCV framework that we have used countless times.
OpenCV
At present, OpenCV has gradually become a general basic research and product development platform. The name OpenCV contains both Open and Computer Vision. In fact, Open refers to Open Source (open source, that is, open source code), and Computer Vision refers to computer vision. The development of OpenCV has an important impact on software development. If you want to know more, you can refer to this article: http://www.jb51.net/article/127911.htm
Effect preview
Picture repair principle
How does OpenCV implement it? It’s simple. In other words, the developer calibrates the characteristics of the noise, and uses the color characteristics around the noise to infer the color of the picture that should be repaired, thereby achieving picture repair.
Program implements analysis
#Calibration noise characteristics, using cv2.inRange binarization to identify noise Binarize the image, specific code: cv2.inRange(img, np.array([240, 240, 240]), np.array([255, 255, 255])), [240, 240, Colors other than 240]~[255, 255, 255] are processed as 0;
Use OpenCV’s dilate method to expand the feature area and optimize the image processing effect;
Use the inpaint method and use the noise mask as a parameter to reason and repair the image;
Complete code
#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()
Related recommendations:
Methods for batch processing of dat files and scientific calculations based on python
The above is the detailed content of Python-based picture repair program (implementing watermark removal). For more information, please follow other related articles on the PHP Chinese website!