【问题标题】:Detect texts from image with opencv使用opencv从图像中检测文本
【发布时间】:2021-08-30 17:07:48
【问题描述】:

我尝试从图像中检测文本及其位置,但始终没有得到任何结果,比我使用此线程中提到的脚本但仍然需要改进,或者是否有另一种方法可以检测任何方向的文本。

Detect white rectangle on black & white image and crop (OpenCV)

import cv2
import pytesseract
import numpy as np

pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files/Tesseract-OCR/tesseract'
#Read input image
img = cv2.imread('image attached')

#convert from BGR to HSV color space
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.bitwise_not(gray)
#apply threshold
thresh = cv2.threshold(gray, 8, 255, cv2.THRESH_BINARY)[1]
#thresh = cv2.bitwise_not(thresh)
# find contours and get one with area about 180*35
# draw all contours in green and accepted ones in red
contours = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
#area_thresh = 0
#min_area = 0.95*180*35
#max_area = 1.05*180*35
result = img.copy()
for c in contours:
    area = cv2.contourArea(c)
    cv2.drawContours(result, [c], -1, (0, 255, 0), 1)
    #if area > min_area and area < max_area:
            #cv2.drawContours(result, [c], -1, (0, 0, 255), 1)

text = pytesseract.image_to_string(thresh)
print(text)
# show images
#cv2.imshow("GRAY", gray)
cv2.imshow("THRESH", thresh)
cv2.imshow("RESULT", result)
cv2.waitKey(0)

图片:

【问题讨论】:

    标签: python opencv ocr


    【解决方案1】:

    您需要更多的预处理才能获得最佳结果。在此之前,我建议调整阈值,因为目前,大部分图像都被洗掉了。改用标准:

    thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)[1]
    
    

    我会先将图像旋转 90 度,检测矩形周围的边界框,然后在每个矩形上运行 tesseract。

    【讨论】:

    • 谢谢你,我发现这是完美的工作,但 cpu 慢:EasyOCR
    猜你喜欢
    • 2021-10-04
    • 2016-03-28
    • 2014-08-14
    • 2017-01-02
    • 1970-01-01
    • 2020-11-19
    • 2016-10-12
    • 1970-01-01
    • 2013-03-23
    相关资源
    最近更新 更多