【问题标题】:Having a hard time reading a text from png file using python使用python很难从png文件中读取文本
【发布时间】:2021-08-23 10:14:43
【问题描述】:

image

我很难从上面的这张图片中提取文本 CHUBB。我尝试了几种图像预处理技术并使用 pytesseract 提取但没有成功。

我的输出:'\x0c'

预期输出:'CHUBB'

任何帮助将不胜感激

我的尝试:

import pytesseract
img = cv2.imread('image1_1.png')

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 

thresh1 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, 

                                          cv2.THRESH_BINARY, 199, 5)

cv2.imshow('Adaptive Mean', thresh1)

# De-allocate any associated memory usage   

if cv2.waitKey(0) & 0xff == 27:  

    cv2.destroyAllWindows()
    
# Adding custom options
custom_config = r' --psm 3'
pytesseract.image_to_string(thresh1, config=custom_config)```

【问题讨论】:

  • 尝试 otsu 阈值而不是自适应阈值。
  • 我确实试过没有成功:
  • cv2.threshold(img, 120, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
  • cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
  • 你的大津阈值图像是什么样的?请出示。您是否尝试在字母上方的顶部添加一些白色填充,以使它们不接触顶部。也许这是一个问题?见 cv2.copyMakeBorder() 添加填充。

标签: python opencv image-processing ocr python-tesseract


【解决方案1】:

我认为问题在于文字 CHUBB 对图片来说太大了。 如果我们稍微减小尺寸或将其粘贴到更大的画布中,那么 pytesseract 就可以正常工作了

from PIL import Image
img = Image.open('test.png')  # load image
new_img = Image.new('RGB', (400, 400), color = 'white')  # create a larger canvas
new_img.paste(im=img, box=(100,100), mask=img)  # paste original CHUBB in the large image
text = pytesseract.image_to_string(new_img, lang='eng', config='--psm 12')  # OCR
print(text)  # CHUBB

仅供参考

for i in range(1,14):
    try:
        text = pytesseract.image_to_string(new_img, lang='eng',config=f"--psm {i}")  # OCR
        print('psm',i, text)
    except:
        pass

产量

psm 1 CHUBB

psm 3 CHUBB

psm 4 CHUBB

psm 5 0
u
J
I
U

psm 6 CHUBB

psm 7 CHUBB

psm 8 7

psm 9 CHUBB

psm 10 CHUBB

psm 11 CHUBB

psm 12 CHUBB

psm 13 7

【讨论】:

    猜你喜欢
    • 2020-05-05
    • 2019-07-14
    • 2020-07-27
    • 2013-01-02
    • 1970-01-01
    • 2020-12-24
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多