python - Python OpenCV cv2.threshold无法在图像(jpg)中找到水平直线/行

标签 python opencv image-thresholding

我有一个.jpg,其中包含一张表格图片,我正在尝试使用Python将其提取到Excel。
enter image description here
我从这里跟随一个例子:
https://towardsdatascience.com/a-table-detection-cell-recognition-and-text-extraction-algorithm-to-convert-tables-to-excel-files-902edcf289ec
不过,我遇到了一个问题,即没有确定水平行。在源图像(上)中,您可以看到水平行比垂直列要浅得多,但是它们在源中可见,我相信仍然应该检测到它们。
我几乎可以想到的所有方式都更改了cv2.threshold值,但这仍然对返回的图像没有影响(请参见下文):

  • thresh,img_bin = cv2.threshold(img,128,255,cv2.THRESH_BINARY |
    cv2.THRESH_OTSU)
  • thresh,img_bin = cv2.threshold(img,0,256,
    cv2.THRESH_BINARY | cv2.THRESH_OTSU)

  • 结果在同一张图片中:
    enter image description here
    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)
    
    result
    我建议阅读有关阈值图像的official tutorial

    关于python - Python OpenCV cv2.threshold无法在图像(jpg)中找到水平直线/行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63115214/

    相关文章:

    Python 网页抓取 - 尝试提取文本

    python - 在 OS X 上安装 memcached 以与 django (python) 一起使用

    python - OpenCVadaptiveThreshold函数返回的图像中的每个像素的值为255

    python - 二值化后如何从图像中去除粗糙的线条伪影

    python - 适应不同雷电条件的OpenCV阈值

    python - 应用于两个列表时如何使用 numpy 的 polyfit?

    python - 为什么 TravisCI 找不到我的许可证文件?

    c++ - opencv : C++ 每3秒绘制和显示矩形

    python - Numpy:获取与掩码大小相同的矩形区域

    c++ - 视频序列的运动估计