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

时间:2020-09-21 05:22:02

标签: python opencv image-processing scikit-image

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

enter image description here

我尝试了一些利用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)

enter image description here

enter image description here

enter image description here

看来,修补程序正试图用黑色填充,因为它被标识为在感兴趣区域周围。我想做的是完全去除白色文本和黑色边框,或者其次尝试用周围颜色中比黑色更多的信息填充白色。

2 个答案:

答案 0 :(得分:2)

这是我能想到的最好的解决方案,但仍对具有更多经验的其他人开放,如果有人有想法,向我展示一种更好的方法。

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)

enter image description here

答案 1 :(得分:1)

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

输入:

enter image description here

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()

阈值和形态清除结果:

enter image description here

结果1(Telea):

enter image description here

结果2(Navier Stokes):

enter image description here