我有一个.jpg,其中包含一张表格图片,我正在尝试使用Python将其提取到Excel。
我从这里跟随一个例子:
https://towardsdatascience.com/a-table-detection-cell-recognition-and-text-extraction-algorithm-to-convert-tables-to-excel-files-902edcf289ec
不过,我遇到了一个问题,即没有确定水平行。在源图像(上)中,您可以看到水平行比垂直列要浅得多,但是它们在源中可见,我相信仍然应该检测到它们。
我几乎可以想到的所有方式都更改了cv2.threshold值,但这仍然对返回的图像没有影响(请参见下文):
cv2.THRESH_OTSU)
cv2.THRESH_BINARY | cv2.THRESH_OTSU)
结果在同一张图片中:
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
关于python - Python OpenCV cv2.threshold无法在图像(jpg)中找到水平直线/行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63115214/