Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python,使用OCR从图像中读取包含两列或三列数据的图像的文本_Python_Python 2.7_Ocr_Tesseract_Python Tesseract - Fatal编程技术网

使用python,使用OCR从图像中读取包含两列或三列数据的图像的文本

使用python,使用OCR从图像中读取包含两列或三列数据的图像的文本,python,python-2.7,ocr,tesseract,python-tesseract,Python,Python 2.7,Ocr,Tesseract,Python Tesseract,在示例图像(仅作为参考,我的图像将具有相同的模式)中,一个页面具有完整的水平文本,另一个页面具有两列水平文本 如何在python中自动检测文档的模式并逐个读取另一列数据 我将Tesseract OCR与Psm 6一起使用,其中水平读取是错误的。实现这一点的一种方法是使用形态学操作和轮廓检测 对于前者,你基本上是将所有角色“流血”成一个大而粗的斑点。使用后者,您可以在图像中定位这些斑点,并提取那些看起来有趣的斑点(意思是:足够大) 使用的脚本: import cv2 import sys SC

在示例图像(仅作为参考,我的图像将具有相同的模式)中,一个页面具有完整的水平文本,另一个页面具有两列水平文本

如何在python中自动检测文档的模式并逐个读取另一列数据


我将Tesseract OCR与Psm 6一起使用,其中水平读取是错误的。

实现这一点的一种方法是使用形态学操作和轮廓检测

对于前者,你基本上是将所有角色“流血”成一个大而粗的斑点。使用后者,您可以在图像中定位这些斑点,并提取那些看起来有趣的斑点(意思是:足够大)

使用的脚本:

import cv2
import sys

SCALE = 4
AREA_THRESHOLD = 427505.0 / 2

def show_scaled(name, img):
    try:
        h, w  = img.shape
    except ValueError:
        h, w, _  = img.shape
    cv2.imshow(name, cv2.resize(img, (w // SCALE, h // SCALE)))

def main():
    img = cv2.imread(sys.argv[1])
    img = img[10:-10, 10:-10] # remove the border, it confuses contour detection
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    show_scaled("original", gray)

    # black and white, and inverted, because
    # white pixels are treated as objects in
    # contour detection
    thresholded = cv2.adaptiveThreshold(
                gray, 255,
                cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV,
                25,
                15
            )
    show_scaled('thresholded', thresholded)
    # I use a kernel that is wide enough to connect characters
    # but not text blocks, and tall enough to connect lines.
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (13, 33))
    closing = cv2.morphologyEx(thresholded, cv2.MORPH_CLOSE, kernel)

    im2, contours, hierarchy = cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    show_scaled("closing", closing)

    for contour in contours:
        convex_contour = cv2.convexHull(contour)
        area = cv2.contourArea(convex_contour)
        if area > AREA_THRESHOLD:
            cv2.drawContours(img, [convex_contour], -1, (255,0,0), 3)

    show_scaled("contours", img)
    cv2.imwrite("/tmp/contours.png", img)
    cv2.waitKey()

if __name__ == '__main__':
    main()

然后,您只需要计算轮廓的边界框,并从原始图像中剪切它。添加一点边距,然后将整个内容输入tesseract。

如何处理此问题:两列不是分别由两个等高线包围,而是作为一个整体由一个等高线包围?尝试将结构元素变薄。@deets,脚本工作得很好,但是我如何使用python裁剪文本框并从文本框中创建新图像呢?这是OpenCV和numpy的基本内容。阅读一些教程。@deets,这行代码
im2,轮廓,层次=cv2.findContours(closing,cv2.RETR\u EXTERNAL,cv2.CHAIN\u Abrox\u SIMPLE)
opencv v4中的getting me错误