0

我需要处理表格,我决定在 python 中使用 openCV。但是,在第一阶段,我遇到了一个问题,即程序找不到我要查找的行。请参阅下面的示例代码。

import cv2
import numpy as np

img = cv2.imread('./sssr/narodnoe_hozyaystvo_sssr_za_1913-1955_gg_page-0111.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, threshold_image = cv2.threshold(img, 127, 255, 0)
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
lines = cv2.HoughLines(edges, 1, np.pi / 180, 300)

for line in lines:
    for r, theta in line:
        a = np.cos(theta)
        b = np.sin(theta)
        x0 = a * r
        y0 = b * r
        x1 = int(x0 + 1000 * (-b))
        y1 = int(y0 + 1000 * (a))
        x2 = int(x0 - 1000 * (-b))
        y2 = int(y0 - 1000 * (a))
        cv2.line(threshold_image, (x1, y1), (x2, y2), (0, 0, 255), 2)

cv2.imwrite('linesDetected.jpg', threshold_image)

结果是:

HoughLines 变换的结果

该程序找到了许多我不需要的行。它无法确定表头的垂直边框。

4

0 回答 0