【发布时间】:2021-07-28 07:08:21
【问题描述】:
首先,我想使用鼠标事件裁剪图像,然后打印裁剪图像内的文本。我尝试了 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)
【问题讨论】:
-
您是否尝试过将图像转换为黑白?
-
这里是:cv2.bitwise_not(adaptiveThresh)
标签: python opencv ocr python-tesseract