代码之家  ›  专栏  ›  技术社区  ›  Mahmoud S. Ahmed

OCR,裁剪字母

  •  2
  • Mahmoud S. Ahmed  · 技术社区  · 7 年前

    我正在构建一个简单的OCR,我面临的问题是,在使用OpenCV分割字母后,无法裁剪字母。有谁能帮我一个简单的方法来裁剪这些字母吗?

    这是分段代码。

    import cv2
    import numpy as np
    
    
    mser = cv2.MSER_create()
    # original image
    # -1 loads as-is so if it will be 3 or 4 channel as the original
    image = cv2.imread('1.jpg', -1)
    # mask defaulting to black for 3-channel and transparent for 4-channel
    # (of course replace corners with yours)
    mask = np.zeros(image.shape, dtype=np.uint8)
    
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    vis = image.copy()
    
    regions = mser.detectRegions(gray)
    hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions[0]]
    
    channel_count = image.shape[2]  # i.e. 3 or 4 depending on your image
    ignore_mask_color = (255,)*channel_count
    cv2.fillConvexPoly(mask, hulls, ignore_mask_color)
    # from Masterfool: use cv2.fillConvexPoly if you know it's convex
    masked_image = cv2.bitwise_and(vis, hulls)
    cv2.imwrite('img')
    
    #for m in range(len(hulls)):
        #masked_image = cv2.bitwise_and(vis, ignore_mask_color)
        # save the result
        #cv2.imwrite('img'+m, masked_image)
    

    结果如下:
    after segmenting letters

    我需要用相同的外壳裁剪每个字母。有什么帮助吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   lucians    7 年前

    您不能裁剪并直接保存外壳,因为您可以在发布的示例中看到它们。或者,更好的是,您可以将它们裁剪并粘贴到方形/矩形画布中。但这不是你想要的这个问题的答案。

    所以,如果你有电脑写的所有文本,最好的选择是开始应用 cv2.findContours() 到图像。您还可以使用其他特定工具,但目前(以及与此问题相关的)请使用此工具。

    import cv2
    import numpy as np
    
    #import image
    image = cv2.imread('image.png')
    
    #grayscale
    gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    cv2.imshow('gray', gray)
    cv2.waitKey(0)
    
    #binary
    ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
    cv2.imshow('second', thresh)
    cv2.waitKey(0)
    
    #dilation
    kernel = np.ones((1,1), np.uint8)
    img_dilation = cv2.dilate(thresh, kernel, iterations=1)
    cv2.imshow('dilated', img_dilation)
    cv2.waitKey(0)
    
    #find contours
    im2,ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    
    #sort contours
    sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])
    
    for i, ctr in enumerate(sorted_ctrs):
        # Get bounding box
        x, y, w, h = cv2.boundingRect(ctr)
    
        # Getting ROI
        roi = image[y:y+h, x:x+w]
    
        # show ROI
        #cv2.imshow('segment no:'+str(i),roi)
        cv2.rectangle(image,(x,y),( x + w, y + h ),(0,255,0),2)
        #cv2.waitKey(0)
    
        if w > 15 and h > 15:
            cv2.imwrite('roi{}.png'.format(i), roi)
    
    cv2.imshow('marked areas',image)
    cv2.waitKey(0)
    

    您可以调整内核以实现或多或少的矩形检测。

    res