在OpenCV中查找带有文本行的轮廓

时间:2018-06-09 19:18:31

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

我正在写一个文本识别程序,我有一个关于soritng轮廓的问题。该程序适用于一行文本,但是当涉及整个文本块时,我的程序不会像80%的时间那样检测文本行。提取一行文本然后是所有其他行(一次一行)的真正有效方法是什么?

我想要实现的目标:

enter image description here

1 个答案:

答案 0 :(得分:4)

实现这一目标有一系列步骤:

  1. 找到将图像二值化的最佳阈值。我使用了Otsu阈值。
  2. 找到合适的形态学操作,沿水平方向形成单个区域。选择宽度大于高度的内核。
  3. 在生成的轮廓上绘制边界框
  4. 更新

    以下是实施:

    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)
    

    enter image description here

    #--- 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)
    

    enter image description here

    #---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)
    

    enter image description here