我目前正在尝试从小的屏幕截图中检测数字。但是,我发现准确度很差。我一直在使用OpenCV,将图像捕获为RGB并转换为灰度,然后使用全局值执行阈值处理(我发现自适应方法效果不佳)。
以下是其中一个数字的示例灰度,然后是图像脱粒保持的示例(数字范围可以是1-99)。请注意,图像的初始屏幕截图很小,因此被放大了。
任何有关如何使用OpenCV或完全不同的系统提高准确性的建议都将受到赞赏。下面包含一些代码,该函数传递给数字RGB的屏幕快照。
def getNumber(image):
image = cv2.resize(image, (0, 0), fx=3, fy=3)
img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh, image_bin = cv2.threshold(img, 125, 255, cv2.THRESH_BINARY)
image_final = PIL.Image.fromarray(image_bin)
txt = pytesseract.image_to_string(
image_final, config='--psm 13 --oem 3 -c tessedit_char_whitelist=0123456789')
return txt
答案 0 :(得分:2)
这就是我可以改进的地方,使用otsu treshold比将文本与背景分开要比给定任意值更有效。 Tesseract在白色背景上使用黑色文字效果更好,并且我还添加了填充,因为tesseract难以识别字符,如果它们离边框太近。
这是最终图片final_image,pytesseract设法读取为“ 46”
def getNumber(image):
img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Otsu Tresholding automatically find best threshold value
_, binary_image = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU)
# invert the image if the text is white and background is black
count_white = numpy.sum(binary_image > 0)
count_black = numpy.sum(binary_image == 0)
if count_black > count_white:
binary_image = 255 - binary_image
# padding
final_image = cv2.copyMakeBorder(image, 10, 10, 10, 10, cv2.BORDER_CONSTANT, value=(255, 255, 255))
txt = pytesseract.image_to_string(
final_image, config='--psm 13 --oem 3 -c tessedit_char_whitelist=0123456789')
return txt
编辑:请注意,您不需要此行:
image_final = PIL.Image.fromarray(image_bin)
因为您可以将numpy数组格式的图像传递给pytesseractr(在cv2中使用),并且Tesseract的准确性仅会下降到35像素以下的字符(而且更大,实际上35px的高度是最佳高度),所以我没有调整其大小