利用Python+OpenCV三步去除水印
一、推理原理
1.標(biāo)定噪聲的特征,使用cv2.inRange二值化標(biāo)識(shí)噪聲對(duì)圖片進(jìn)行二值化處理,具體代碼:cv2.inRange(img, np.array([200, 200, 240]), np.array([255, 255, 255])),把[200, 200, 200]~[255, 255, 255]以外的顏色處理為0
2.使用OpenCV的dilate方法,擴(kuò)展特征的區(qū)域,優(yōu)化圖片處理效果
3.使用inpaint方法,把噪聲的mask作為參數(shù),推理并修復(fù)圖片
二、推理步驟
1.從源圖片,截取右下角部分,另存為新圖片
2.識(shí)別水印,顏色值為:[200, 200, 200]~[255, 255, 255]
3.去掉水印,還原圖片
4.把源圖片、去掉水印的新圖片,進(jìn)行重疊合并
三、參考代碼
import cv2 import numpy as np from PIL import Image import os dir = os.getcwd() path = "1.jpg" newPath = "new.jpg" img=cv2.imread(path,1) hight,width,depth=img.shape[0:3] #截取 cropped = img[int(hight*0.8):hight, int(width*0.7):width] # 裁剪坐標(biāo)為[y0:y1, x0:x1] cv2.imwrite(newPath, cropped) imgSY = cv2.imread(newPath,1) #圖片二值化處理,把[200,200,200]-[250,250,250]以外的顏色變成0 thresh = cv2.inRange(imgSY,np.array([200,200,200]),np.array([250,250,250])) #創(chuàng)建形狀和尺寸的結(jié)構(gòu)元素 kernel = np.ones((3,3),np.uint8) #擴(kuò)展待修復(fù)區(qū)域 hi_mask = cv2.dilate(thresh,kernel,iterations=10) specular = cv2.inpaint(imgSY,hi_mask,5,flags=cv2.INPAINT_TELEA) cv2.imwrite(newPath, specular) #覆蓋圖片 imgSY = Image.open(newPath) img = Image.open(path) img.paste(imgSY, (int(width*0.7),int(hight*0.8),width,hight)) img.save(newPath) import cv2 import numpy as np from PIL import Image import os dir = os.getcwd() path = "1.jpg" newPath = "new.jpg" img=cv2.imread(path,1) hight,width,depth=img.shape[0:3] #截取 cropped = img[int(hight*0.8):hight, int(width*0.7):width] # 裁剪坐標(biāo)為[y0:y1, x0:x1] cv2.imwrite(newPath, cropped) imgSY = cv2.imread(newPath,1) #圖片二值化處理,把[200,200,200]-[250,250,250]以外的顏色變成0 thresh = cv2.inRange(imgSY,np.array([200,200,200]),np.array([250,250,250])) #創(chuàng)建形狀和尺寸的結(jié)構(gòu)元素 kernel = np.ones((3,3),np.uint8) #擴(kuò)展待修復(fù)區(qū)域 hi_mask = cv2.dilate(thresh,kernel,iterations=10) specular = cv2.inpaint(imgSY,hi_mask,5,flags=cv2.INPAINT_TELEA) cv2.imwrite(newPath, specular) #覆蓋圖片 imgSY = Image.open(newPath) img = Image.open(path) img.paste(imgSY, (int(width*0.7),int(hight*0.8),width,hight)) img.save(newPath)
四、效果圖
沒(méi)去水印前:

去了后:

到此這篇關(guān)于利用Python+OpenCV三步去除水印的文章就介紹到這了,更多相關(guān)Python+OpenCV去水印內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python讀取Ansible?playbooks返回信息示例解析
這篇文章主要為大家介紹了Python讀取Ansible?playbooks返回信息示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
python爬蟲(chóng)將js轉(zhuǎn)化成json實(shí)現(xiàn)示例
這篇文章主要為大家介紹了python爬蟲(chóng)將js轉(zhuǎn)化成json實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Python中enumerate()函數(shù)詳細(xì)分析(附多個(gè)Demo)
Python的enumerate()函數(shù)是一個(gè)內(nèi)置函數(shù),主要用于在遍歷循環(huán)中獲取每個(gè)元素的索引以及對(duì)應(yīng)的值,這篇文章主要介紹了Python中enumerate()函數(shù)的相關(guān)資料,需要的朋友可以參考下2024-10-10
讓python的Cookie.py模塊支持冒號(hào)做key的方法
雖然Cookie的標(biāo)準(zhǔn)是不允許:冒號(hào)出現(xiàn)在key里面的,但是我們的開(kāi)發(fā)人員是很可愛(ài)的,常常會(huì)讓我們意想不到。2010-12-12
pip matplotlib報(bào)錯(cuò)equired packages can not be built解決
這篇文章主要介紹了pip matplotlib報(bào)錯(cuò)equired packages can not be built解決,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01

