【发布时间】:2023-03-18 01:11:02
【问题描述】:
我有以下图片
lower = np.array([175, 125, 45], dtype="uint8")
upper = np.array([255, 255, 255], dtype="uint8")
mask = cv2.inRange(image, lower, upper)
img = cv2.bitwise_and(image, image, mask=mask)
plt.figure()
plt.imshow(img)
plt.axis('off')
plt.show()
现在如果我尝试像这样转换成灰度:
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
我明白了:
我想提取上面的数字。
建议:
gray = 255 - gray
emp = np.full_like(gray, 255)
emp -= gray
emp[emp==0] = 255
emp[emp<100] = 0
gauss = cv2.GaussianBlur(emp, (3,3), 1)
gauss[gauss<220] = 0
plt.imshow(gauss)
给出图像:
然后在任何图像上使用 pytesseract:
data = pytesseract.image_to_string(img, config='outputbase digits')
给予:
'\x0c'
另一个建议的解决方案是:
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
thr = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV)[1]
txt = pytesseract.image_to_string(thr)
plt.imshow(thr)
这给了
'\x0c'
不是很满意...请问有人有更好的解决方案吗?
谢谢!
【问题讨论】:
-
在 OCR 之前添加 img = PIL.ImageOps.invert(img)
-
我试过了:img = Image.fromarray(img), img = ImageOps.invert(img), data = pytesseract.image_to_string(img),得到同样的结果...
-
@NicolasRey 您正在执行图像处理的整个过程,然后您将处理前的 raw 图像传递给
pytesseract:data = pytesseract.image_to_string(img, config='outputbase digits')替换img为gauss!!!
标签: python-3.x opencv image-processing python-tesseract