Python - 从图像边界删除黑色像素

时间:2021-01-25 13:10:06

标签: python image image-processing

我对图像处理非常陌生,我正在尝试清理类似于图片 1 中源自图像边界的黑色像素的图片。

Clipped Image of a Character using PyMuPDF

图像是从 PDF 中剪下的字符,我尝试用 tesseract 处理以重新获取字符。我已经在 Stackoverflow 中搜索了答案,但只找到了摆脱黑色边框的解决方案。 我需要用白色像素覆盖角落的所有黑色像素,以便tesseract可以正确识别字符。

我无法更改用于剪切字符的边界框,因为字符在边界框的不同区域居中,如果我剪切边界框,我会剪切一些如下所示的字符

Clipped Image of Character with BoundingBox adjusted to fit before seen Image

我的第一个猜测是递归跟踪具有特定黑色阈值的像素,但我害怕在这种情况下计算时间,并且不知道从哪里开始以及如何开始,除了使用两个两个维数组,一个是像素,一个是指示我是否已经处理过该像素。

不胜感激。

编辑:多一些需要清除边缘黑色像素的案例图片:

enter image description hereenter image description hereenter image description hereenter image description hereenter image description here

编辑:代码片段以创建边框图像:

    @staticmethod
    def __get_border_image(image: Image) -> Image:
        data = numpy.asarray(image)

        border = cv2.copyMakeBorder(data, top=5, bottom=5, left=5, right=5, borderType=cv2.BORDER_CONSTANT)

        return Image.fromarray(border)

1 个答案:

答案 0 :(得分:1)

试试这个:

  • 在边缘周围人为地添加一个 1px 宽的黑色 border
  • flood-fill 带有从左上角开始的白色全黑像素
  • 移除第一步中的 1px 边框(如有必要)

添加边框的目的是让白色在图像的所有边缘“流动”,并到达接触边缘的任何黑色项目。

相关问题