我想为OCR预处理这张图像。我希望它能够阅读所有的文本,正如你所看到的,文本有不同的颜色。我使用HSV范围来检测我想要的颜色,我只是把所有的东西都变成了白色,所以OCR软件更容易将图像转换成文本。用我当前的设置,它正确地读取了所有的文本,除了被浅绿色背景包围的文本,我相信问题是我的HSV值,我如何调整它才能读取所有的文本?
这是我的预处理代码:
def pre_process_img(img):
"""
Prepares image for OCR
:param img: PIL.Image
Image object
:return: output_img
"""
open_cv_image = np.array(img)
# Convert image to HSV in order to be able to extract what we care about using masks
img_hsv = cv2.cvtColor(open_cv_image, cv2.COLOR_RGB2HSV)
# Init mask
mask = 0
for color_name, data in ocr.COLORS.items():
if color_name not in ['white', 'dark_red', 'yellow']:
continue
lower = np.array(data['lower'])
upper = np.array(data['upper'])
# Join masks
mask += cv2.inRange(img_hsv, lower, upper)
# Set my output img to zero everywhere except my mask
# output_img = open_cv_image.copy()
img_hsv[np.where(mask == 0)] = 0
# Change image to white where we found the color we want
img_hsv[mask > 0] = (255, 255, 255)
# cv2.imshow("out", img_hsv)
# cv2.waitKey(0)
return img_hsv我的HSV范围
COLORS = {
'dark_red': {
'lower': [0, 180, 100],
'upper': [5, 240, 200]
},
'red': {
'lower': [0, 150, 100],
'upper': [10, 255, 255]
},
'white': {
'lower': [0, 0, 168],
'upper': [172, 111, 255]
},
'blue': {
'lower': [100, 150, 0],
'upper': [140, 255, 255]
},
'grey': {
'lower': [36, 50, 70],
'upper': [89, 255, 255]
},
'purple': {
'lower': [129, 50, 70],
'upper': [158, 255, 255]
},
'green': {
'lower': [36, 50, 70],
'upper': [89, 255, 255]
},
'yellow': {
'lower': [25, 50, 70],
'upper': [60, 255, 255]
},
'orange': {
'lower': [5, 50, 50],
'upper': [15, 180, 230]
}
}我知道我看起来不需要所有的颜色,但是有一些具体的例子使用所有的颜色。
这是图像

我的图像到文本功能在需要的时候
def image_to_text(img):
"""
:param img: PIL.Image
Image object
:return: text: str
Generated text
"""
# Tesseract path
pytesseract.tesseract_cmd = "C:\\Program Files\\Tesseract-OCR\\tesseract.exe"
img = pre_process_img(img)
config = '--oem 3 --psm %d' % 6
text = image_to_string(img, config=config, lang='eng')
# Dirty text
# print(text)
return text我目前的输出是
You have decided to bug § DaddyEredin's house tonight.
10 Pagond has left the game.
9 viagra has left the game.
Amember of the mafia visited 8 Blesser last night.
Amember of the mafia visited 14 jesus last night.
Amember of the mafia visited 7 Someone Especial last night.我想要的输出是
You have decided to bug § DaddyEredin's house tonight.
10 Pagond has left the game.
9 viagra has left the game.
You were attacked but someone nursed you back to health
Amember of the mafia visited 8 Blesser last night.
Amember of the mafia visited 14 jesus last night.
Amember of the mafia visited 7 Someone Especial last night.发布于 2022-02-09 14:34:06
绿色条形图和绿色文本有相同的HSV值,所以我用背景来减轻图像。结果图像中的条形图更加饱和,而文本则不饱和。
另外,从两边的图像的5个像素是水平裁剪。
通常情况下,当发生冲突时,最好对颜色进行预处理。
def pre_process_img(img):
# crop useless area
img = img.crop((5, 0, img.size[0]-10, img.size[1]))
# cut and stretch background strip
open_cv_bg = np.array(img.crop((0, 0, 1, img.size[1])).resize(img.size)).astype(float)
open_cv_image = np.array(img).astype(float)
# blend lighten image with background to saturate the green bar
diff = blend_modes.lighten_only(open_cv_image, open_cv_bg, 1).astype(np.uint8)
# Convert image to HSV in order to be able to extract what we care about using masks
img_hsv = cv2.cvtColor(diff, cv2.COLOR_RGB2HSV)
# Init mask
mask = 0
for color_name, data in COLORS.items():
if color_name not in ['white', 'green', 'yellow']:
continue
lower = np.array(data['lower'])
upper = np.array(data['upper'])
# Join masks
mask += cv2.inRange(img_hsv, lower, upper)
cv2.imshow("mask", mask)
cv2.waitKey(0)
return mask该图像与其背景混合,使绿色背景饱和。

COLORS = {
'white': {
'lower': [0, 0, 168],
'upper': [172, 111, 255]
},
'green': {
'lower': [55, 100, 50],
'upper': [65, 220, 255]
},
'yellow': {
'lower': [25, 100, 70],
'upper': [50, 255, 255]
}
}输出
a
You have decided to bug § DaddyEredin's house tonight.
10 Pagond has left the game.
9 viagra has left the game.
You were attacked but someone nursed you back to health!
Amember of the mafia visited 8 Blesser last night.
A member of the mafia visited 14 jesus last night.
A member of the mafia visited 7 Someone Especial last night.https://stackoverflow.com/questions/71005645
复制相似问题