如何使用Pytesseract提取图像中的小数 [英] How to extract decimal in image with Pytesseract
问题描述
以上是图片,我已经尝试过可以从SO或google获得的所有内容,但似乎没有任何效果.我无法在image中获得确切的值,我应该得到2.10,而总是得到210.
Above is the image ,I have tried everything I could get from SO or google ,nothing seems to work. I can not get the exact value in image , I should get 2.10 , Instead it always get 210.
并且不仅限于此图像,任何在数字1 tesseract之前具有十进制的图像都将忽略该十进制值.
And it is not limited to this image only any image which have a decimal before number 1 tesseract ignores the decimal value.
def returnAllowedAmount(self,imgpath):
th = 127
max_val = 255
img = cv2.imread(imgpath,0) #Load Image in Memory
img = cv2.resize(img, None, fx=2.5, fy=2.5, interpolation=cv2.INTER_CUBIC) #rescale Image
img = cv2.medianBlur(img, 1)
ret , img = cv2.threshold(img,th,max_val,cv2.THRESH_TOZERO)
self.showImage(img)
returnData = pytesseract.image_to_string(img,lang='eng',config='-psm 13 ' )
returnData = ''.join(p for p in returnData if p.isnumeric() or p == ".") # REMOVE $ SIGN
推荐答案
在将图像放入Pytesseract之前,进行一些清理/平滑图像的预处理会有所帮助.这是一种简单的方法
Before throwing the image into Pytesseract, some preprocessing to clean/smooth the image helps. Here's a simple approach
- 将图像转换为灰度并放大图像
- 阈值
- 执行形态学操作以清洁图像
- 反转图像
首先,我们将图像转换为灰度,然后使用 imutils
库调整大小,然后将阈值设置为获取二进制图像
First we convert the image to grayscale, resize using the imutils
library then threshold to obtain a binary image
现在,我们执行形态转换平滑图像
Now we perform morphological transformations to smooth the image
现在,我们将Pytesseract的图像反转并添加高斯模糊
Now we invert the image for Pytesseract and add a Gaussian blur
我们使用--psm 10
config标志,因为我们希望将图像视为单个字符.这是一些有用的其他配置标志
We use the --psm 10
config flag since we want to treat the image as a single character. Here's some additional configuration flags that could be useful
结果
2.10美元
$2.10
过滤后
2.10
2.10
import cv2
import pytesseract
import imutils
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
image = cv2.imread('1.png',0)
image = imutils.resize(image, width=300)
thresh = cv2.threshold(image, 150, 255, cv2.THRESH_BINARY_INV)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
result = 255 - close
result = cv2.GaussianBlur(result, (5,5), 0)
data = pytesseract.image_to_string(result, lang='eng',config='--psm 10 ')
processed_data = ''.join(char for char in data if char.isnumeric() or char == '.')
print(data)
print(processed_data)
cv2.imshow('thresh', thresh)
cv2.imshow('close', close)
cv2.imshow('result', result)
cv2.waitKey()
这篇关于如何使用Pytesseract提取图像中的小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!