从图像中删除带有黑色边框的白色文本

Removing White Text with Black Borders From Image

我正在尝试从具有黑色边框和白色填充的图像中删除文本。以下图为例

我尝试了一些使用 opencv 和 skimage inpaint 的选项

import cv2
from skimage.restoration import inpaint
img = cv2.imread('Documents/test_image.png')
mask = cv2.threshold(img, 210, 255, cv2.THRESH_BINARY)[1][:,:,0]
dst = cv2.inpaint(img, mask, 7, cv2.INPAINT_TELEA)
image_result = inpaint.inpaint_biharmonic(img, mask,
                                          multichannel=True)
cv2.imshow('image',img)
cv2.imshow('mask',mask)
cv2.imshow('dst',dst)
cv2.imshow('image_result',image_result)
cv2.waitKey(0)

似乎修复只是试图用黑色填充,因为它在感兴趣的区域周围识别出黑色。我想做的是完全删除白色文本和黑色边框,或者尝试用周围颜色的更多信息填充白色,而不仅仅是黑色。

这是我能想出的最佳解决方案,仍然向有更多经验的其他人开放,如果有人有想法,可以向我展示更好的方法。

mask = cv2.threshold(img, 245, 255, cv2.THRESH_BINARY)[1][:,:,0]
new_mask = cv2.dilate(mask, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10,10)))
dst = cv2.inpaint(img, new_mask, 7, cv2.INPAINT_TELEA)

下面是Python/OpenCV中的两种修复方法。请注意,我使用饱和度通道来创建阈值,因为原则上白色和黑色的饱和度为零。

输入:

import cv2
import numpy as np

# read input
img = cv2.imread('white_black_text.png')

# convert to hsv and extract saturation
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
sat = hsv[:,:,1]

# threshold and invert
thresh = cv2.threshold(sat, 10, 255, cv2.THRESH_BINARY)[1]
thresh = 255 - thresh

# apply morphology dilate
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15,15))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, kernel)

# do inpainting
result1 = cv2.inpaint(img,thresh,11,cv2.INPAINT_TELEA)
result2 = cv2.inpaint(img,thresh,11,cv2.INPAINT_NS)

# save results
cv2.imwrite('white_black_text_threshold.png', thresh)
cv2.imwrite('white_black_text_inpainted1.png', result1)
cv2.imwrite('white_black_text_inpainted2.png', result1)

# show results
cv2.imshow('thresh',thresh)
cv2.imshow('result1',result1)
cv2.imshow('result2',result2)
cv2.waitKey(0)
cv2.destroyAllWindows()

阈值和形态清理结果:

结果 1(Telea):

结果 2(纳维尔斯托克斯):