首页 > 解决方案 > 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): enter image description here

(I cannot edit it manually, I got over 50K images like this one, but all look similar- outline and text)

标签: pythonimage-processingcomputer-vision

解决方案


使用侵蚀图像(作为标记)和形态重建。

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 )

查看结果:在此处输入图像描述


推荐阅读