代码之家  ›  专栏  ›  技术社区  ›  Naga kiran

如何在opencv中改变轮廓形成的区域(阈值)

  •  0
  • Naga kiran  · 技术社区  · 6 年前

    我正在尝试建立OCR来从图像中提取文本,我正在使用轮廓来形成文本字符的边界,

    经过几次改变cv2.threshold的尝试,我得到了形成文本字符边界的最佳轮廓拟合。

    #files = os.listdir(r'letters/harry.jpeg',0)
    file = r'/home/naga/Documents/Naga/Machine Learning/Data_extract/letters/Harry/Harry Potter and the Sorcerer s Stone-page-006.jpg'
    im1 = cv2.imread(file,0)
    im = cv2.imread(file)
    
    # ret,thresh1 = cv2.threshold(im1,180,278,cv2.THRESH_BINARY)
    # _,contours, hierarchy = cv2.findContours(thresh1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    
    ret,thresh1 = cv2.threshold(im1,180,278,cv2.THRESH_BINARY)
    kernel = np.ones((5,5),np.uint8)
    dilated = cv2.dilate(im1,kernel,iterations = 1)
    _,contours, hierarchy = cv2.findContours(dilated,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    
    for cnt in contours:
        x,y,w,h = cv2.boundingRect(cnt)
        #bound the images
        cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),1)
    
    cv2.namedWindow('BindingBox', cv2.WINDOW_NORMAL)
    cv2.imwrite('output2/BindingBox4.jpg',im)
    

    Contours on text with Opencv

    我对opencv还不熟悉,我一直都在坚持 cv2 threshold 但我不明白怎么用它。请给你的输入形成轮廓的话。

    original pic

    1 回复  |  直到 6 年前
        1
  •  2
  •   yapws87    6 年前

    一个简单的解决方案是在运行findcontour函数之前放大阈值图像的结果。

    ret,thresh1 = cv2.threshold(im1,180,255,cv2.THRESH_BINARY_INV)
    kernel = np.ones((5,5),np.uint8)
    dilated = cv2.dilate(thresh1,kernel,iterations = 2)
    contours, hierarchy = cv2.findContours(dilated,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    

    如果不是所有文本都组合成一个单词,则可以增加迭代次数。如果您不确定在这里使用的值,那么这需要一些尝试和错误。

    阅读形态学过程,以便更好地理解该主题。这是一个有用的工具,有基本的图像处理。