有的时候,有些图片内容上的文字或者图像需要全部盖住,我们就必须人工找出来,批量去除。太多太烦的时候就可以使用下面的代码批量去除。
简单易用!
例如你截图了某个微信聊天记录想盖掉某个对话人,或者你的某个图片中出现了某个人民要全部盖调,这就是一个简单而且高效的方法
import numpy as np
import cv2
img=cv2.imread('c:/opencv/chatlist.png')
img_gary=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
while True:
r=cv2.selectROI('select', img,False)
# 没有任何选择的情况下退出
if r[2]==0 :break
imgselect=img[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]
imgselect=cv2.cvtColor(imgselect, cv2.COLOR_BGR2GRAY)
h,w=imgselect.shape[:2]
res=cv2.matchTemplate(img_gary, imgselect , cv2.TM_CCOEFF_NORMED)
per=0.8
loc=np.where(res>=per )
i=0
for pt in zip(*loc[::-1 ]):
bottom_right=(pt[0]+w,pt[1]+h)
cv2.rectangle(img, pt, bottom_right, (255,255,0),-1)
cv2.imwrite('c:/opencv/chatlist_new.jpg', img)
cv2.waitKey(0)
cv2.destroyAllWindows()