python - Element removal in image
问题描述
Python 3, cv2 library (not a must) Trying to remove the name tag "roadhog" from this image (and leave the outline of the character as is):
(I cannot edit it manually, I got over 50K images like this one, but all look similar- outline and text)
解决方案
使用侵蚀图像(作为标记)和形态重建。
import cv2
import numpy as np
img = cv2.imread('in.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 40, 255, cv2.THRESH_BINARY)[1]
kernel = np.ones((7,7),np.uint8)
kernel2 = np.ones((3,3),np.uint8)
marker = cv2.erode(thresh,kernel,iterations = 1)
while True:
tmp=marker.copy()
marker=cv2.dilate(marker, kernel2)
marker=cv2.min(thresh, marker)
difference = cv2.subtract(marker, tmp)
if cv2.countNonZero(difference) == 0:
break
mask=cv2.bitwise_not(marker)
mask_color = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
out=cv2.bitwise_and(img, mask_color)
cv2.imwrite('out.png', out)
cv2.imshow('result', out )
推荐阅读
- python-3.x - Internet Explorer中的python selenium windows身份验证
- javascript - 仅发布 JavaScript 文件的缩小版本 (ASPNET Core)
- c# - Dotnet Core 2.2 - Azure AD 身份验证工作但角色库返回访问被拒绝
- angular - 带有动作的 Angular6 表单和对 SERVLET 的 POST 调用不起作用
- r - 仅在使用 dplyr 的另一个值之后查找一个值的第一次出现
- java - 未处理的异常:java.text.ParseException - 将字符串转换为时间戳
- blockchain - 如何在 RIDE 中从 tx.sender 获取字符串地址?
- python - 获取带有空格的 pandas df 列的 len()
- javascript - 无法将下一个 js 部署到 azure
- ios - 如何在 UIView 中定位和动画子视图?