如何从裁剪图像中 OCR 蓝色背景上带有白色字符的文本?

问题描述 投票:0回答:2

首先,我想使用鼠标事件裁剪图像,然后打印裁剪图像内的文本。我尝试了 OCR 脚本,但所有这些脚本都不适用于下面所附的图像。我认为原因是文本有蓝底白字。

你能帮我做这个吗?

完整图片:

裁剪图像:

我尝试过的一个例子是:

import pytesseract
import cv2
import numpy as np

pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'

img = cv2.imread('D:/frame/time 0_03_.jpg')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
adaptiveThresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 35, 30)
inverted_bin=cv2.bitwise_not(adaptiveThresh)

#Some noise reduction
kernel = np.ones((2,2),np.uint8)
processed_img = cv2.erode(inverted_bin, kernel, iterations = 1)
processed_img = cv2.dilate(processed_img, kernel, iterations = 1)
 
#Applying image_to_string method
text = pytesseract.image_to_string(processed_img)
 
print(text)
python opencv ocr python-tesseract
2个回答
0
投票

[编辑]

对于任何想知道的人,问题中的图像在发布我的答案后已更新。这就是原图:

因此,我原来的答案中有以下输出。

这是新发布的图片:

特定的土耳其语字符,尤其是最后一个单词,仍然无法正确检测(因为我现在仍然无法使用

lang='tur'
),但至少可以使用
Ö
检测到
Ü
lang='deu'
,我已经安装了:

text = pytesseract.image_to_string(mask, lang='deu').strip().replace('\n', '').replace('\f', '')
print(text)
# GÖKYÜZÜ AVCILARI ILE TEKE TEK KLASIGI

[/编辑]


我不会在这里使用

cv2.adaptiveThreshold
,而是使用
cv2.threshold
简单的
cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV
。由于逗号触及图像边框,我将通过
cv2.copyMakeBorder
添加另一个一像素宽的边框以正确捕获逗号。所以,这将是完整的代码(替换
\f
仅是由于我的
pytesseract
版本):

import cv2
import pytesseract

img = cv2.imread('n7nET.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
mask = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV)[1]
mask = cv2.copyMakeBorder(mask, 1, 1, 1, 1, cv2.BORDER_CONSTANT, 0)
text = pytesseract.image_to_string(mask).strip().replace('\n', '').replace('\f', '')
print(text)
# 2020'DE SALGINI BILDILER, YA 2021'DE?

输出对我来说似乎是正确的——当然,对于这个带有上面点的特殊(我假设是土耳其语)大写字母 I 字符来说不是正确的。不幸的是,我无法运行

pytesseract.image_to_string(..., lang='tur')
,因为它根本没有安装。也许,看看它也可以在这里找到正确的字符。

----------------------------------------
System information
----------------------------------------
Platform:      Windows-10-10.0.16299-SP0
Python:        3.9.1
PyCharm:       2021.1.1
OpenCV:        4.5.1
pytesseract:   5.0.0-alpha.20201127
----------------------------------------

0
投票

嗨,今天我使用谷歌镜头成功从蓝色图像中提取了白色文本。该照片存储在我的三星 Galaxy 手机上的照片应用程序中。

© www.soinside.com 2019 - 2024. All rights reserved.