Python OpenCV cv2.threshold 未在图像 (jpg) 中找到水平直线 lines/rows

Python OpenCV cv2.threshold is not finding straight horizontal lines/rows in image (jpg)

我有一个包含 table 图像的 .jpg,我正尝试使用 Python.

将其提取到 Excel

我正在按照此处的示例进行操作:

https://towardsdatascience.com/a-table-detection-cell-recognition-and-text-extraction-algorithm-to-convert-tables-to-excel-files-902edcf289ec

我遇到了一个问题,水平行没有被识别。在源图像(上图)中,您可以看到水平行比垂直列亮得多,但它们在源图像中是可见的,我相信它们仍然应该被检测到。

我已经几乎用我能想到的所有方法更改了 cv2.threshold 值,但这仍然对返回的图像没有影响(见下文):

同一图像中的结果:

import cv2
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import csv

try:
    from PIL import Image
except ImportError:
    import Image
import pytesseract

# read your file
file = r'venv/images/iiCrop.jpg'
img = cv2.imread(file, 0)
img.shape
# thresholding the image to a binary image
thresh, img_bin = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
# inverting the image
img_bin = 255 - img_bin
cv2.imwrite('venv/images/cv_inverted.png', img_bin)
# Plotting the image to see the output
plotting = plt.imshow(img_bin, cmap='gray')
plt.show()

是否有明显或不太明显的地方我做错了?

您必须松开cv2.THRESH_OTSU才能手动调整阈值。您也可以使用 cv2.THRESH_BINARY_INV 来反转二进制图像。如果没有 jpeg 噪声,某些线条太亮无法检测到。

thresh, img_bin = cv2.threshold(img, 230, 255, cv2.THRESH_BINARY_INV)

我建议阅读 official tutorial 关于阈值图像的文章