简体   繁体   English

如何使用 pytesseract 从图像的一部分读取文本

[英]How to read text from only a portion of the image with pytesseract

I'm trying to read the 213 from this image but i cant even get pytesseract to read everything Here is my best effort code:我正在尝试从这张图片中读取 213,但我什至无法让 pytesseract 读取所有内容这是我尽力而为的代码:

import cv2
import pytesseract

img = cv2.imread('gamepictures/text.png')  # Load the image
img = cv2.cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # convert to grey
img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 15)
txt = pytesseract.image_to_string(img, config='--psm 6')
print(txt)
cv2.imshow("", img)
cv2.waitKey(0)

I have been trying to change the treshholding algorithm i even tried with canny, but i can't get it to work.我一直在尝试更改我什至尝试过 canny 的 treshholding 算法,但我无法让它工作。 So my questions are how can i read everything?所以我的问题是我怎样才能阅读所有内容?

And how can i only read the 213我怎么能只读 213

image图片

Something like this works:这样的事情有效:

import cv2
import pytesseract

img = cv2.imread('gamepictures/text.png')  # Load the image
img = img[98:190,6:149,:]
img = cv2.cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # convert to grey
img = cv2.GaussianBlur(img, (5, 5), 3)
img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 7, -2)

txt = pytesseract.image_to_string(img, config='--psm 10 -c tessedit_char_whitelist=0123456789')
print(img.shape)
print(txt)
cv2.imshow("", img)
cv2.waitKey(0)

Basically I just sliced the image and played around with the parameters a bit.基本上我只是将图像切片并稍微调整一下参数。 The GaussianBlur is there to make the image more continuous. GaussianBlur 的作用是使图像更加连续。

The -c tessedit_char_whitelist=0123456789 is optional and just makes sure that only numbers are read. -c tessedit_char_whitelist=0123456789是可选的,只是确保只读取数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM