到目前为止我尝试过的。大多数图像都工作正常,文本为黑色,背景为白色。
from PIL import Image
import pytesseract
import nltk
import cv2
imageName = "p9.png"
img = cv2.imread(imageName,cv2.IMREAD_COLOR) #Open the image from which charectors has to be recognized
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #convert to grey to reduce detials
gray = cv2.bilateralFilter(gray, 11, 17, 17) #Blur to reduce noise
original = pytesseract.image_to_string(gray, config='')
print (original)
但下图我没有给出正确的文字。
输出:
REMIUM OKING OIL
KETTLE-RENDERED 9s MADE FROM POR siren!
fatworks €)
NET WT. 4 02 (3966)
如何解决此问题。
最佳答案
我的意思是
imageName = "p9.png"
img = cv2.imread(imageName,cv2.IMREAD_COLOR) #Open the image from which charectors has to be recognized
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #convert to grey to reduce detials
gray = cv2.bilateralFilter(gray, 11, 17, 17) #Blur to reduce noise
img = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,71,2)
#img = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
cv2.THRESH_BINARY,11,2)
original = pytesseract.image_to_string(img, config='')
尝试使用此函数的参数来找到最适合您的
cv2.adaptiveThreshold(src,maxValue,adaptiveMethod,thresholdType,blockSize,C[,dst])
。
我还在这里保留了原始教程的链接:https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html
关于python-3.x - 无法正确读取 python 中具有颜色文本的图像中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59244032/