【问题标题】:Finding contours with lines of text in OpenCV在 OpenCV 中查找带有文本行的轮廓
【发布时间】:2018-11-19 12:30:17
【问题描述】:

我正在编写一个文本识别程序,但我在排序轮廓时遇到了问题。该程序适用于一行文本,但是当涉及到整个文本块时,我的程序在 80% 的情况下都无法检测到文本行。提取一行文本然后提取所有其他行(一次一行)的真正有效方法是什么?

我想要达到的目标:

【问题讨论】:

    标签: android c++ opencv ocr text-recognition


    【解决方案1】:

    有一系列步骤可以实现这一点:

    1. 找到对图像进行二值化的最佳阈值。我用的是大津阈值。
    2. 找到合适的形态学运算,沿水平方向形成单个区域。选择宽度大于高度的内核。
    3. 在生成的轮廓上绘制边界框

    更新

    这里是实现:

    x = 'C:/Users/Desktop/text.jpg' 
    
    img = cv2.imread(x)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  
    
    #--- performing Otsu threshold ---
    ret,thresh1 = cv2.threshold(gray, 0, 255,cv2.THRESH_OTSU|cv2.THRESH_BINARY_INV)
    cv2.imshow('thresh1', thresh1)
    

    #--- choosing the right kernel
    #--- kernel size of 3 rows (to join dots above letters 'i' and 'j')
    #--- and 10 columns to join neighboring letters in words and neighboring words
    rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 3))
    dilation = cv2.dilate(thresh1, rect_kernel, iterations = 1)
    cv2.imshow('dilation', dilation)
    

    #---Finding contours ---
    _, contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    
    im2 = img.copy()
    for cnt in contours:
            x, y, w, h = cv2.boundingRect(cnt)
            cv2.rectangle(im2, (x, y), (x + w, y + h), (0, 255, 0), 2)
    cv2.imshow('final', im2)
    

    【讨论】:

    • @Mithor 对不起,我只在 python 中有它
    • 非常漂亮。我喜欢用宽核膨胀来连接字母和单词的技巧。
    • 谢谢。节省了我很多时间
    • 有没有办法以字符串格式提取此文本
    • @Rudrashah 您可以对提取的部分执行 OCR 以获取字符串格式的结果。
    猜你喜欢
    • 1970-01-01
    • 2021-12-28
    • 2012-11-06
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    相关资源
    最近更新 更多