【发布时间】:2021-10-24 03:30:07
【问题描述】:
我尝试了几天但没有成功,我决定寻求帮助:)
我对 cv2 和 tesseract 很陌生,我正在尝试做一些我认为很容易的事情,但由于某种原因并不像我预期的那么容易。
这个image 是一个包含多个值的打印屏幕,我必须阅读并转换为 text/int,this one 是原始值。 我可以将所有人与我拥有的多个图像隔离开来,但是当我尝试转换它们时,我不能。有时,它给了我正确的价值,但他错过了 90% 的时间。
这就是我的工作:
#open the image
image = cv2.imread('image.png')
#resize it to give some help (this way i was able to get some good results)
image2 = cv2.resize(image, None, fx=1.2, fy=1.2, interpolation=cv2.INTER_CUBIC)
image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
#getting the parts of the images with the text that i want to convert
name = image2[0:100, 100:500]
stats1 = image2[110:160, 460:580]
stats2 = image2[160:210, 460:580]
stats3 = image2[220:270, 460:580]
#using pytesseract to convert from image to string
name_str = pytesseract.image_to_string(name, lang='eng',config='--psm 6')
stats1_str = pytesseract.image_to_string(ally_stats_grass, lang='eng',config='--psm 13 --oem 3 -c tessedit_char_whitelist=0123456789.%')
#print the values
print('name', name_str)
print('grass', stats1_str)
同时,我也尝试了不同的阈值和反转图像颜色的方法,也有一些扩张和侵蚀但没有成功
image2 = cv2.threshold(image2, 1, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
image2 = 255 - cv2.morphologyEx(image2, cv2.MORPH_CLOSE, kernel, iterations=1)
karnel = np.ones((1, 1), np.uint8)
image2 = cv2.dilate(image2, kernel, iterations=1)
image2 = cv2.erode(image2, kernel, iterations=1)
我只是在祈祷,希望有人能帮助我:) 感谢您的宝贵时间
【问题讨论】:
标签: python-3.x cv2 python-tesseract