【问题标题】:90 degree Skew Correction for OCR in opencv pythonopencv python中OCR的90度倾斜校正
【发布时间】:2020-07-01 06:30:40
【问题描述】:

歪斜校正在 OCR 中很常见,我的歪斜校正代码使用 houghLinesP() 并找到歪斜图像的角度工作正常。我想问的是你如何处理倾斜 90 度的图像?因为角度可能是 0 度并且 tesseract 不会提取任何文本。

那么如何处理这个问题呢?

【问题讨论】:

  • 添加你的脚本,不管你到目前为止做了什么

标签: opencv computer-vision ocr tesseract python-tesseract


【解决方案1】:

您可以使用tesseract 的osd 函数进行歪斜校正。尽管使用传统图像处理的正常图像歪斜校正可能有效,但对于扫描和低质量图像,传统方法可能会失败。所以最好使用 tesseract 提供的 osd 功能


    import pytesseract
    import cv2
    import numpy as np

    ###function to rotate image

    def rotate_bound(image, angle):
        """Rotate image with the given angle

        :param type image: input image
        :param type angle: Angle to be rotated
        :return: rotated image
        :rtype: numpy.ndarray

        """
        (h, w) = image.shape[:2]
        ### centroid
        (cX, cY) = (w // 2, h // 2)
        ### creating rotation matrix
        M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)

        cos = np.abs(M[0, 0])
        sin = np.abs(M[0, 1])
        nW = int((h * sin) + (w * cos))
        nH = int((h * cos) + (w * sin))
        M[0, 2] += (nW / 2) - cX
        M[1, 2] += (nH / 2) - cY

        return cv2.warpAffine(image, M, (nW, nH))

    ###read input image
    image=cv2.imread('path/to/image.jpg')
    ###getting orientation info
    newdata=pytesseract.image_to_osd(image)
    ###filter angle value
    angle=re.search('(?<=Rotate: )\d+', newdata).group(0)
    print('osd angle:',angle)
    ### rotating image with angle
    skew_corrected_image=rotate_bound(image,float(angle))
    ### you can add tesseract OCR call here

【讨论】:

  • 谢谢!它解决了我正在寻找的 90 度倾斜。但是,这无法找到稍微倾斜的图像的倾斜角度,但我已经有一个功能,所以谢谢
  • 乐于助人。如果您觉得它有帮助,您也可以投赞成票。谢谢:D
猜你喜欢
  • 2020-01-17
  • 2018-08-13
  • 1970-01-01
  • 1970-01-01
  • 2016-02-03
  • 1970-01-01
  • 2019-08-24
  • 2019-06-19
  • 2021-03-19
相关资源
最近更新 更多