gpt4 book ai didi

python - 删除内部边框

转载 作者:行者123 更新时间:2023-12-02 17:41:43 25 4
gpt4 key购买 nike

我从表格图片中裁剪了很多图片。 OCR由于表边界的“剩余”而在文本检测方面存在一些问题。实际上,我正在寻找删除它们的方法(我只想提取文本)。以下是其中的一些示例:



谢谢!

最佳答案

此代码(基于opencv)解决了两个示例的问题。步骤如下:

  • 阈值图像
  • 从二进制对象中删除行
  • 计算比率=(对象的面积)/(边界框的面积)
  • 如果比率太小,我们认为该对象是行的组合
  • 如果比率很大,我们认为对象是单行

  • 这是python代码:
    import cv2
    import matplotlib.pylab as plt
    import numpy as np

    # load image
    img = cv2.imread('om9gN.jpg',0)

    # blur and apply otsu threshold
    img = cv2.blur(img, (3,3))
    _, img = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

    # invert image
    img = (img == 0).astype(np.uint8)


    img_new = np.zeros_like(img)

    # find contours
    _,contours,_ = cv2.findContours(img, 1, 2)

    for idx, cnt in enumerate(contours):

    # get area of contour
    temp = np.zeros_like(img)
    cv2.drawContours(temp, contours , idx, 1, -1)
    area_cnt = np.sum(temp)

    # get number of pixels of bounding box of contour
    x,y,w,h = cv2.boundingRect(cnt)
    area_box = w * h

    # get ratio of cnt-area and box-area
    ratio = float(area_cnt) / area_box

    # only draw contour if:
    # - 1.) ratio is not too big (line fills whole bounding box)
    # - 2.) ratio is not too small (combination of lines fill very
    # small ratio of bounding box)
    if 0.9 > ratio > 0.2:
    cv2.drawContours(img_new, contours , idx, 1, -1)

    plt.figure()
    plt.subplot(1,2,1)
    plt.imshow(img_new)
    plt.axis("off")
    plt.show()

    Isolated letters

    关于python - 删除内部边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43798956/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com