如何从裁剪的图像中在蓝色背景上用白色字符识别文本?

2024-06-25 22:51:35 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,我想使用鼠标事件裁剪图像,然后打印裁剪图像中的文本。我尝试了OCR脚本,但都不能为下面的图片工作。我认为原因是文本在蓝色背景上有白色字符

你能帮我做这个吗

完整图像:

The full image:

裁剪图像:

enter image description here

我尝试的一个例子是:

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)

Tags: 图像文本importimgnpcv2kernelocr
1条回答
网友
1楼 · 发布于 2024-06-25 22:51:35

[编辑]

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

Input 1

因此,以下是我原始答案中的输出

这是最新发布的图片:

Input 2

具体的土耳其语字符,特别是最后一个单词,仍然没有被正确检测到(因为我现在仍然不能使用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

[/EDIT]


我不会在这里使用cv2.adaptiveThreshold,而是使用cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV简单的cv2.threshold。因为逗号触及图像边框,所以我会通过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
                    

相关问题 更多 >