python - 使用 pytesseract 可以做什么来改善我的 OCR 结果?

标签 python opencv ocr tesseract python-tesseract

我正在尝试使用 OpenCV 和 Python-tesseract 应用 OCR 将以下图像转换为文本: Original image .

但是 tesseract 到目前为止还无法正确读取图像。 上面写着:uleswylly Bie7 Srp a7 相反。

在将图像提供给超正方体之前,我已采取以下步骤对图像进行预处理:

  1. 首先我放大图像:
# Image scaling
def set_image_dpi(img):
    # Get current dimensions of the image
    height, width = img.shape[:2]

    # Define scale factor
    scale_factor = 6

    # Calculate new dimensions
    new_height = int(height * scale_factor)
    new_width = int(width * scale_factor)

    # Resize image
    return cv2.resize(img, (new_width, new_height))

图像结果:result1.png

  • 标准化图像:
  • # Normalization
    norm_img = np.zeros((img.shape[0], img.shape[1]))
    img = cv2.normalize(img, norm_img, 0, 255, cv2.NORM_MINMAX)
    

    图像结果:result2.png

  • 然后我消除一些噪音:
  • # Remove noise
    def remove_noise(img):
        return cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 15)
    

    图像结果:result3.png

  • 获取灰度图像:
  • # Get grayscale
    def get_grayscale(img):
        return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    

    图像结果:result4.png

  • 应用阈值:
  • # Thresholding
    def thresholding(img):
        return cv2.threshold(img, 150, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) [1]
    

    图像结果:result5.png

  • 反转图像颜色:
  • # Invert the image
    def invert(img):
        return cv2.bitwise_not(img)
    

    图像结果:result6.png

  • 最后我将图像传递给 pytesseract:
  • # Pass preprocessed image to pytesseract
    text = pytesseract.image_to_string(img)
    print("Text found: " + text)
    
    pytesseract 输出: “uleswylly Bie7 Srp a7

    我想改进我的预处理,以便 pytesseract 能够真正读取图像?任何帮助将不胜感激!

    提前致谢,

    斯廷纳特

    最佳答案

    问题有点挑战性,但又不过度拟合问题的解决方案...

    假设文本是明亮的、无色的并且被彩色像素包围。 我们还可以假设背景相对均匀。

    我们可以从 result3.png 开始并使用以下阶段:

    • 使用左上角像素的颜色添加填充。
      填充用于为 floodFill 做准备(必需的,因为某些彩色像素触及图像边缘)。
    • 用浅蓝色填充背景。
      请注意,所选颜色有点过度拟合,因为饱和度级别需要接近红色像素的级别。
    • 从 BGR 颜色空间转换为 HSV 颜色空间,并提取饱和度 channel 。
    • 应用阈值(使用 cv2.THRESH_OTSU 进行自动阈值设置)。
    • pytesseract.image_to_string 应用于阈值图像。

    代码示例:

    import cv2
    import numpy as np
    import pytesseract
    pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'  # May be required when using Windows
    
    img = cv2.imread('result3.png')  # Read result3.png
    
    # Add padding with the color of the top left pixel
    pad_color = img[0, 0, :]
    padded_img = np.full((img.shape[0]+10, img.shape[1]+10, 3), pad_color, np.uint8)
    padded_img[5:-5, 5:-5, :] = img
    
    cv2.floodFill(padded_img, None, (0, 0), (255, 100, 100), loDiff=(10, 10, 10), upDiff=(10, 10, 10))  # Fill the background with blue color.
    cv2.imwrite('result7.png', padded_img)
    
    # Convert from BGR to HSV color space, and extract the saturation channel.
    hsv = cv2.cvtColor(padded_img, cv2.COLOR_BGR2HSV)
    s = hsv[:, :, 1]
    cv2.imwrite('result8.png', s)
    
    # Apply thresholding (use `cv2.THRESH_OTSU` for automatic thresholding)
    thresh = cv2.threshold(s, 0, 255, cv2.THRESH_OTSU)[1]
    cv2.imwrite('result9.png', thresh)
    
    # Pass preprocessed image to PyTesseract
    text = pytesseract.image_to_string(thresh, config="--psm 6")
    print("Text found: " + text)
    

    输出:
    找到文本:Jules -Lv:175 -P.17


    result7.png(洪水填充后):
    enter image description here

    result8.png(提取饱和 channel 后):
    enter image description here

    result9.png(阈值处理后):
    enter image description here

    关于python - 使用 pytesseract 可以做什么来改善我的 OCR 结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76038508/

    相关文章:

    opencv - 棋盘识别质量度量的最佳实践

    python - 通过边界框从图像中提取选定的文本

    java - 如何关闭tess4j中的字典?

    java - 如何使用opencv编辑图像以使用OCR读取文本

    python - 为什么用 python3 用正确的逻辑计算数字中的数字会出现错误的答案?

    Python:按字段拆分对象列表的更好方法?

    python - 创建并填充 PySpark 数据框,其中列作为 period_range

    c++ - 使用 LIBELAS 和后过滤算法的视差图不正确

    python - 在 Windows 7 下从 python 启动时找不到 R 包

    c++ - 如何将 OpenCV cv::Mat 转换为 QImage