【问题标题】:Removing White Text with Black Borders From Image从图像中删除带有黑色边框的白色文本
【发布时间】:2020-09-21 05:22:02
【问题描述】:

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

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

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

【问题讨论】:

  • 为文本周围的黑色找到一个遮罩。为白色文本查找掩码。组合面具。但 OpenCV 修复通常距离有限,最适合细划痕。它不是补丁匹配修复方法。在 Skimage/Scipy 中也是如此。
  • 据您所知,python 中有没有可用的补丁匹配方法?
  • 不,我不知道。

标签: python opencv image-processing scikit-image


【解决方案1】:

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

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)

【讨论】:

  • 你可以试试这个代码: import cv2 file = "1J1Dz.png" img = cv2.imread(file) saturation=cv2.cvtColor(img, cv2.COLOR_BGR2HSV)[:,:,1] value=cv2.cvtColor(img, cv2.COLOR_BGR2HSV)[:,:,2] s_and_v=cv2.bitwise_and(saturation, value) mask2 = cv2.threshold(s_and_v, 30, 255, cv2.THRESH_BINARY_INV)[1] new_mask2 = cv2.dilate(mask2, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7,7))) dst2 = cv2.inpaint(img, new_mask2, 15, cv2.INPAINT_TELEA) cv2.imwrite('test.png', dst2)
【解决方案2】:

以下是 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(纳维斯托克斯):

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 2020-12-27
    • 1970-01-01
    • 2014-03-01
    • 2012-09-14
    • 2023-02-02
    相关资源
    最近更新 更多