gpt4 book ai didi

python - 在python中从整个图像中检测表格部分

转载 作者:行者123 更新时间:2023-12-02 15:40:46 25 4
gpt4 key购买 nike

我有一个大小为 3500x5000 的图像,现在我只想从整个图像中检测表格部分,如果它不是直接用于 OCR 处理,则对其进行裁剪和旋转。经过所有搜索,我有了使用 https://medium.com/coinmonks/a-box-detection-algorithm-for-any-image-containing-boxes-756c15d7ed26 裁剪图像中每个单元格的想法。 ,但不知道如何裁剪图像中的表格部分。
我在这里使用的图像:
enter image description here
现在我只想要这样的部分:(手动裁剪)
enter image description here
提前致谢!..

最佳答案

您可以针对该问题使用多尺度模板匹配。


  • 应用 Canny 边缘检测器来查看角落。


  • 循环遍历给定目录中每个图像的比例。


  • 打印坐标,如果找到,将其显示在图像中。


  • 编码:

    import numpy as np
    import imutils
    import glob
    import cv2

    template = cv2.imread("apA8L.png")
    template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
    template = cv2.Canny(template, 50, 200)
    (h, w) = template.shape[:2]

    for imagePath in glob.glob("img2" + "/*.jpg"):
    image = cv2.imread(imagePath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    found = None

    for scale in np.linspace(0.2, 1.0, 20)[::-1]:
    resized = imutils.resize(gray, width=int(gray.shape[1] * scale))
    r = gray.shape[1] / float(resized.shape[1])

    if resized.shape[0] < h or resized.shape[1] < w:
    break

    edged = cv2.Canny(resized, 50, 200)
    result = cv2.matchTemplate(edged, template, cv2.TM_CCOEFF)
    (_, maxVal, _, maxLoc) = cv2.minMaxLoc(result)

    if found is None or maxVal > found[0]:
    found = (maxVal, maxLoc, r)

    (_, maxLoc, r) = found
    (startX, startY) = (int(maxLoc[0] * r), int(maxLoc[1] * r))
    (endX, endY) = (int((maxLoc[0] + w) * r), int((maxLoc[1] + h) * r))

    cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 255), 2)
    cv2.imwrite("out.png", image)
    print("Table coordinates: ({}, {}, {}, {})".format(startX, startY, endX, endY))
    表坐标:
    Table Coordinates: (352, 1915, 753, 2445)
    输出:
    enter image description here

    关于python - 在python中从整个图像中检测表格部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63395631/

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